Flylib.com

Books Software

 
 
 

8.4 Searches


8.4 Searches

Searching with both persistence systems looks similar to JDO. They both use object-based query languages and leverage a similar API set.

8.4.1 Castor Searches

Castor uses the Object Query Language (OQL). OQL queries are similar to standard ANSI SQL but use object names in place of column fields. Example 8-7 details the implementation of a book search with Castor.

Example 8-7. Searching for a book by title with Castor
public Book findBookByTitle(String title) throws Exception {
     JDO jdo = new JDO("alternativepersistencedb");
     jdo.loadConfiguration("database.xml");
     Database db = jdo.getDatabase( );
     db.begin( );
     OQLQuery query = db.getOQLQuery("SELECT b FROM book.Book b WHERE        
         title=");
     query.bind("Alternative Persistence Systems");
     QueryResults results = query.execute( );
     // assume the first book found is the desired book
     Book book = (Book) results.next( );          
     db.commit( );
     db.close( );
     return book;
}

As before, all configuration information needs to be loaded and ready before any database operations can be performed. Once the connection has been established, the OQLQuery and QueryResults classes are used for searching. The most important line here is the getOQLQuery( ) method. This particular search looks for all entities that have a title value equal to the title attribute supplied by the calling method. If no results exist when next( ) is called, a NoSuchElementException will be thrown.

8.4.2 Hibernate Searches

Searching with Hibernate is almost identical to that with Castor. Example 8-8 details the implementation of searching with Hibernate.

Example 8-8. Searching for a book by title with Hibernate
public Book findBookByTitle(String title) throws Exception {
     Datastore ds = Hibernate.createDatastore( );
     ds.storeFile("hibernate.xml");
     SessionFactory sessionFactory = ds.buildSessionFactory( );
     Session session = sessionFactory.openSession( );
     Book book = null;
     List results = session.find("from o in class book.Book where title = 
          ?", title, Hibernate.STRING);     
     if (results.isEmpty( )) {
          throw new Exception ("Entity not found: " + title);
     } else {
          book =  results.get(0);
     }
     session.close( );
     return book;
}

The difference with Hibernate is that rather than throwing an exception when no entities are found, the framework supplies the method isEmpty( ) to determine if your search yielded no results.


8.5 Beyond the Basics

A full discussion of the details of each alternative framework is well beyond the scope of this book. Furthermore, Castor and Hibernate are not your only alternatives. You should now, however, have an appreciation of how these two frameworks operate , some things to look for, and the role of alternative persistence frameworks in database programming.


Part III: Tutorials

The first two sections of the book cover a variety of technologies. You should be familiar with many of the them, but you may not be familiar with all of them. As an EJB programmer, you probably know J2EE technologies like EJB and JNDI well, but you may not be familiar with JDO. This section provides a JDO tutorial just for you. Because you know EJB and JNDI so well, however, you will find no use for the J2EE tutorial.

In short, not every tutorial chapter is for every reader of this book. Some advanced readers, in fact, will have no use for any of the tutorial chapters. I do expect most readers will find the need to reference at least one of these chapters before tackling a chapter earlier in the book. If you have no experience in JDO, for example, you probably want to look at the JDO tutorial before reading the chapter on JDO persistence. If you have no EJB experience, read the J2EE chapter before tackling either of the EJB persistence chapters. Finally, you need to understand the material in the JDBC and SQL tutorials to appreciate just about any chapter in Part I or Part II.