@ SuppressWarnings ( "unchecked" ) private static void readDB ( String dbName ) { PersistenceManager pm = ZooJdoHelper . openDB ( dbName ) ; pm . currentTransaction ( ) . begin ( ) ; //Extents are one way to get objects from a database: System . out . println ( "Person extent: " ) ; Extent < Person > ext = pm . getExtent ( Person . class ) ; for ( Person p : ext ) { System . out . println ( "Person found: " + p . getName ( ) ) ; } ext . closeAll ( ) ; //Queries are more powerful: System . out . println ( "Queries: " ) ; Query query = pm . newQuery ( Person . class , "name == 'Bart'" ) ; Collection < Person > barts = ( Collection < Person > ) query . execute ( ) ; for ( Person p : barts ) { System . out . println ( "Person found called 'Bart': " + p . getName ( ) ) ; } query . closeAll ( ) ; //Once an object is loaded, normal method calls can be used to traverse the object graph. Person bart = barts . iterator ( ) . next ( ) ; System . out . println ( bart . getName ( ) + " has " + bart . getFriends ( ) . size ( ) + " friend(s):" ) ; for ( Person p : bart . getFriends ( ) ) { System . out . println ( p . getName ( ) + " is a friend of " + bart . getName ( ) ) ; } pm . currentTransaction ( ) . commit ( ) ; closeDB ( pm ) ; }
Read data from a database . Extents are fast but allow filtering only on the class . Queries are a bit more powerful than Extents .