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(newSearch().setDistinct(true));//would work. Only the root entity is returned for each result. search(newSearch().setDistinct(true).addField("firstName");//would work. Only the single "firstName" value is returned for each result. search(newSearch().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 theSearchFacade.
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 newSearchFacadeinterface.
The first JAR istrg-search.jar. This contains all the search functionality as well as theSearchFacade. The second istrg-dao.jar. You can usetrg-search.jarby itself, buttrg-dao.jarrequirestrg-search.jar.trg-dao.jarcontains the base DAO implementation as well as the “standard” and “original” generic and general DAOs.
publicFilter getFilterFromExample(Object example, ExampleOptions options);
HibernateSearchFacadeimplements theSearchFacadeinterface. It needs to be configured by setting thesessionFactoryproperty, either as a constructor argument or through thesetSessionFactory()method. It usesSessionFactory.getCurrentSession()to get aSessionwhen performing queries. This behavior can be overridden by extendingHibernateSearchFacadeand overriding thegetSession()method. See also the "Hibernate session strategy" section in the "Details and Tips" on theUserGuide.