Chapter 3. Performing Queries

In the previous chapter, we created various types of persistent objects and mapped them to Lucene search indexes in various ways. However, we have basically used the same keyword query in all the versions of the example application so far.

In this chapter, we will explore other search query types offered by the Hibernate Search DSL, as well as important features such as sorting and pagination that are common to all of them.

Mapping API versus query API

So far, we discussed API alternatives for mapping classes to the database with Hibernate ORM. You can map your classes with XML or annotations, using JPA or the traditional API, and Hibernate Search will work fine so long as you are aware of some minor differences.

However, when we talk about which API a Hibernate application uses, there are two parts to the answer. Not only is there more than one approach for mapping classes to the database, there are also options for how to query the database at runtime. Hibernate ORM has its traditional API, based on the SessionFactory and Session classes. It also offers an implementation of the corresponding JPA standards, built around EntityManagerFactory and EntityManager.

You might have noticed that in the sample code so far, we've been mapping classes to the database with JPA annotations and using the traditional Hibernate Session class to query them. This may seem confusing at first, but the mapping and the query APIs are essentially interchangeable. You can mix and match!

So which approach should you use in the Hibernate Search projects? There are advantages to sticking with common standards as much as possible. Once you are experienced with JPA, those skills transfer when you work on other projects that use different JPA implementations.

On the other hand, Hibernate ORM's traditional API is more powerful than the generic JPA standards. Also, Hibernate Search is an extension of Hibernate ORM. You can't migrate a project to a different JPA implementation without first finding some other search strategy altogether.

Note

So in a nutshell, there is a strong argument for using JPA standards whenever they are adequate. However, Hibernate Search requires Hibernate ORM anyway, so there's no sense in being too dogmatic. Throughout this book, most of the example code will use JPA annotations for mapping classes, and use the traditional Hibernate Session class for queries.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset