Priuli-Filter is a project to facilitate the development of a system with ORM frameworks, allowing the module to search for entities of any system can be made in a generic way, besides making it easier and more dynamic creation of ‘Selects’
How it works?
Having all the information required to load an object (entity), simply use the features of the filter to get these fields filled in and turn it into an object Criteria (Hibernate) or CriteriaBuilder (JPA2). The filter dynamically creates a Criteria \ CriteriaBuilder with all the restrictions found in an entity (Scanner) or implementing a Filter. Much like the Example.create () in Hibernate.
What should I know?
The filter is very simple to use, to conduct a search of an ‘entity’ is necessary to inform the filter, which fields should be used as search criteria, the filter created will inform the search criteria for ORM (Hibernate, Jpa2. ..)
Are three classes of design that make up the filter, they are:
The Filter interface is a Map, it extends Map <String, Object>, and is used to store the information you entered as a condition for a WHERE query.
The interface can be used in several cases where there is need for some kind of filter.
This class has methods that make up a Filter to a Hibernate Criteria object. The data are processed in a Filter Restrictions and added to a Hibernate Criteria.
This class does something similar to Example.create (), it covers the attributes of an object and obtains the values filled in to create some restrictions that will be used for a sql query. The difference is that this class can get the id of second object level that the ‘Example of Hibernate’ does not.
And the code?
A small and simple example to understand how the filter. First download the latest version of http://sourceforge.net/projects/priuli-filter/ sourceforge project, you can also see source code in svn, and the versions available.
Now, imagine the following entities, Person and Phone, where Person can have a one phone, and phone can have n number of Person.
In its implementation of the DAO class, create a method that will return a list of people, from a the criteria of an implementation of a ‘Filter’, would look something like this:
HibernateUtils.buildCriteria(filter,session,persistentClass) Filter will transform into a hibernate Criteria object containing all the restrictions that were in the filter.
Now we need to create the filter, for this we use class FactoryFilter.createFilter (), this class simply creates implementations of the interface ‘Filter’ can be used with hibernate or jpa..
We imagine that at some point we need to search in database, all persons who have the name ‘Steve’.
SELECT * FROM PERSON WHERE NM_PERSON = ‘Steve’
Now imagine that there is a system screen, the user choose what he wants to filter, and JSF injecting the Entity containing the information that the user typed. In this scenario the filter can solve well:
What you write and controller to inject the object, the ScannerJPAEntity will automatically add in a Filter, which will create the proper query with the right conditions. In the above we are always asking you to sort by name Order By Desc
Now imagine, if you need to create a new field in the Entity ‘Person’. just add this field in the query screen of your system, you simply create the field in the entity and creates on the screen, the method of filter you no longer have to move because the Filter detects the fields by reflection of the entity. This is a good advantage because you less maintenance in code.
The example in this article ends here, the full sample can be obtained at this link: