public Object getObjectByQuery ( Query query ) throws PersistenceBrokerException { Object result = null ; if ( query instanceof QueryByIdentity ) { // example obj may be an entity or an Identity Object obj = query . getExampleObject ( ) ; if ( obj instanceof Identity ) { Identity oid = ( Identity ) obj ; result = getObjectByIdentity ( oid ) ; } else { // TODO: This workaround doesn't allow 'null' for PK fields if ( ! serviceBrokerHelper ( ) . hasNullPKField ( getClassDescriptor ( obj . getClass ( ) ) , obj ) ) { Identity oid = serviceIdentity ( ) . buildIdentity ( obj ) ; result = getObjectByIdentity ( oid ) ; } } } else { Class itemClass = query . getSearchClass ( ) ; ClassDescriptor cld = getClassDescriptor ( itemClass ) ; /* use OJB intern Iterator, thus we are able to close used resources instantly */ OJBIterator it = getIteratorFromQuery ( query , cld ) ; /* arminw: patch by Andre Clute, instead of taking the first found result try to get the first found none null result. He wrote: I have a situation where an item with a certain criteria is in my database twice -- once deleted, and then a non-deleted version of it. When I do a PB.getObjectByQuery(), the RsIterator get's both results from the database, but the first row is the deleted row, so my RowReader filters it out, and do not get the right result. */ try { while ( result == null && it . hasNext ( ) ) { result = it . next ( ) ; } } // make sure that we close the used resources finally { if ( it != null ) it . releaseDbResources ( ) ; } } return result ; }
retrieve an Object by query I . e perform a SELECT ... FROM ... WHERE ... in an RDBMS