So, going back to a very old topic - dot separated association paths on Criteria/ createAlias etc.
Hibernate: 3.1.2
For those unfamiliar with this topic, check
http://forum.hibernate.org/viewtopic.ph ... teria+path
or
http://forum.hibernate.org/viewtopic.ph ... teria+path
I wanted this functionality - and have written a rough helper class to help me solve this problem, I describe the approach below.
The criteria api is extremely handy for search pages in a GUI.
It makes it very easy to programmatically build searches and order results on a single table, but it doesn't work so well when you are searching across associations that are more then a table away.
What I wanted was this:
on my generic DAO class, a method such as:
Code:
/**
* @param orderBy keys are dot separated property paths, with Boolean values for ascending
* @param restrictions keys are dot separated property paths, with restriction objects as the values
*/
List search(Class clazz, Map orderBy, Map restrictions) {
//
}
For example to use this, you could do a search such as:
Code:
(Sale -> Customer -> Address -> City ) and
(Sale -> Product -> Supplier)
Map ordering = new HashMap(1);
ordering.put("customer.address.city.postcode",Boolean.FALSE);
Map restrictions = new HashMap(1);
ordering.put("product.supplier.name","acme pty ltd"); // i only use "equals" restrictions atm..
List sales = salesDAO.search(Sale.class, ordering, restrictions);
What the implementation needs to do is search through all of the dot property paths, and recursively do Criteria.createAlias(). Then it needs to add the order by or restrictions on each appropriate alias.
I did this with a tree - i.e. create a tree such as:
sale -> customer -> address -> city -> postcode
........-> product -> supplier -> name
I then traverse the tree, first doing a createAlias for each node (starting at the root of course), and then checking for orderings or restrictions in the maps at each node, using the full path as the key.
This makes for a fairly handy API for using the criteria.
At the moment I just have a rough implementation (the tree uses recursion). I thought someone may be interested in the approach.
Potentially a well performing implementation would be added into Hibernate's SessionImpl or somewhere..
If this has already be done, or there is a better way, I'll be both embarrassed and grateful to hear of it.