This blog for Hibernate Generic DAO Project has been moved to WordPress Bye bye Blogger!

Thursday, May 28, 2009

What's new in 0.5.0

We just released version 0.5.0.

This release focuses on just one new feature, support for JPA. Both the search and DAO portions of the framework are now fully compatible with both standard Hibernate and with JPA.

This made for the biggest structural changes we've had so far and some changes in the way the framework is installed and configured. For more details on this, look to the wiki (InstallationAndConfiguration and FrameworkArchitecture).

In order to work with various JPA providers, the framework uses a MetadataUtil interface that must be implemented for the desired provider. At this point, the only implemented provider is Hibernate Entity Manager, but perhaps we will see a few others contributed to the project over time.

Monday, May 18, 2009

Release 0.4.3

We recently released a new version of Hibernate Generic DAO. We added find-by-example functionality and a "distinct" option to the search. We also formally separated the search and DAO portions so that search can be used independently.


The distinct option can be set on a search in order to filter out duplicate results. As with plain HQL or SQL, only use this option if your search requires it because it does affect performance.

"Distinct" can only be used on queries that return a single field. Here are some examples:

search(new Search().setDistinct(true)); //would work. Only the root entity is returned for each result.
(new Search().setDistinct(true).addField("firstName"); //would work. Only the single "firstName" value is returned for each result.
(new Search().setDistinct(true).addField("firstName").addField("lastName")); //would NOT work. Two fields are returned for each result.

Find By Example

The framework implements find-by-example by processing an example entity and producing a Filter object. The resulting Filter would be added to a search, and then the search would be processed. This method provides for maximum flexibility because the developer can alter the Filter in any way after it's been created (security checks, transformations, etc.) and can add it to the search in any way (using junction, disjuction, logical operators, etc.).

There are two methods for making a Filter form an example entity. These methods can be found on all the DAOs and on the SearchFacade.

public Filter getFilterFromExample(Object example);
public Filter getFilterFromExample(Object example, ExampleOptions options);

The first one uses the default options and the second lets the developer specify a set of options. These options include:

· Ignore case.

· Use exact string matching, starts with, ends with or contains.

· Exclude null-valued properties

· Exclude zero-valued properties

· Specify a list of properties to exclude

Here then is the simplest example of making a find by example method with no options:

public List findByExample(Person person) {

return Search().addFilter(dao.getFilterFromExample(person)));


A more complex example would be:

public List findSubordinatesByExample(Person manager, Person example) {

ExampleOptions options = new ExampleOptions();



Search search = new Search();

search.addFilterEqual("manager", manager);

search.addFilter(dao.getFilterFromExample(example, options));



Using Search Alone

The search functionality of the framework can now be used without the DAOs. To make this possible we separated the framework code into two JARs and introduced a new SearchFacade interface.

The first JAR is trg-search.jar. This contains all the search functionality as well as the SearchFacade. The second is trg-dao.jar. You can use trg-search.jar by itself, but trg-dao.jar requires trg-search.jar. trg-dao.jarcontains the base DAO implementation as well as the “standard” and “original” generic and general DAOs.

The new SearchFacade interface has basically all the same search methods found on the DAOs, except without all the other DAO methods. Here is the interface (see also the javadoc:

public interface SearchFacade {

public List search(ISearch search);

public List search(Class searchClass, ISearch search);

public int count(ISearch search);

public int count(Class searchClass, ISearch search);

public SearchResult searchAndCount(ISearch search);

public SearchResult searchAndCount(Class searchClass, ISearch search);

public Object searchUnique(ISearch search);

public Object searchUnique(Class searchClass, ISearch search);

public Filter getFilterFromExample(Object example);

public Filter getFilterFromExample(Object example, ExampleOptions options);


HibernateSearchFacade implements the SearchFacade interface. It needs to be configured by setting the sessionFactory property, either as a constructor argument or through the setSessionFactory() method. It uses SessionFactory.getCurrentSession() to get a Session when performing queries. This behavior can be overridden by extending HibernateSearchFacade and overriding the getSession() method. See also the "Hibernate session strategy" section in the "Details and Tips" on the UserGuide.