-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: more on criteria and dotted association paths
PostPosted: Sat Mar 18, 2006 10:42 am 
Newbie

Joined: Sat Mar 18, 2006 10:02 am
Posts: 4
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.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 20, 2006 5:40 pm 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Hi hughmadden.

This seems very nice!

I'm facing the same problem with some queries.

Would you mind share the code so I can have a look?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 20, 2006 8:51 pm 
Newbie

Joined: Sat Mar 18, 2006 10:02 am
Posts: 4
re sharing the code, if I have time tonight I'll see about tidying it up and posting it.

Best Regards.


Top
 Profile  
 
 Post subject: sample code for dotted association paths (criteria)
PostPosted: Tue Mar 21, 2006 8:37 am 
Newbie

Joined: Sat Mar 18, 2006 10:02 am
Posts: 4
Heya,
as described in this thread, I've posted some working code.
http://www.openstrategy.com.au/dotpathcriteria.tar.gz

In my base DAO, I have a method such as:

Code:
   public List getObjects(final Class clazz, final String orderBy,
         final boolean ascending, final Map parameters) {
      HibernateTemplate template = getHibernateTemplate();
      return template.executeFind(new HibernateCallback() {
         public Object doInHibernate(Session session)
               throws HibernateException, SQLException {
            Set orders = new HashSet(1);
            orders.add(orderBy);
            return DotPathCriteriaBuilder.buildCriteria(clazz, session, ascending, orders, parameters).list();
         }
      });

   }


This allows you to do stuff like:
Code:
Map parms = new HashMap(2);
parms.put("b.e.someBooleanProp",Boolean.TRUE);
parms.put("b.f.g.someStringProp","blah");
List searchResults = yourDAO.getObjects(A.class, "b.c.someProperty", true, parms);


This is a pretty quick hack - weaknesses include:
-only Restrictions.eq support atm (should be easy to extend for all however)
-If you supply multiple order by clauses, best to check the generated sql to see in what order the order by clauses are generated (i only use one as in the above example)
-It probably performs pretty badly.. If you need high performance you'll want to optimise.

Have fun,
Hugh


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 9:47 am 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Hi hughmadden!

Thank you very much for your code!

I will try it today when I get back to my job.

We have some search screens which your code can help a lot! :)

I will let you know what I got.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 3:12 pm 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Hi hughmadden!

I'm having problems downloading your code.

Would you mind send me directly?

fxjr at mpdft dot gov dot br

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 5:52 pm 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Hi hughmadden!

I could get your code to work!

But I receive a lot of duplicates :)

Do you get them too?

I had to use a new TreeSet(listfromgetobjects) in order to them to remove the duplicates. But this also implied that I had to implement Comparable interface.

Is this the strategy you are doing?

I liked very much your code. I will see how we could use it here in my job.


I'd like to see this code or something like that on Hibernate criteria support. It would easy very much development.

Thank you very much.


Top
 Profile  
 
 Post subject: duplicates
PostPosted: Thu Mar 23, 2006 9:48 pm 
Newbie

Joined: Sat Mar 18, 2006 10:02 am
Posts: 4
I guess the duplicates is because of the left outer joins?

I didn't notice the duplicates in my own tests. With left outer joins I use the hibernate.show_sql and check the generated sql to ensure it is returning what I would expect it to do.

Anyway, glad to hear it is (somewhat) working for you.

I'd be tempted to check your output SQL and relationships before doing the sort with treeset - presumably this will cause your order by's to fail, which is half the point :)

cheers,
Hugh


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 4:05 pm 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Hi hughmadden!

Yes, I think this may be because of outer joins.

I'm still doing some tests here. I will check if I'm doing something wrong and let you know what I find.

Again, thank you very much for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 2:58 pm 
Newbie

Joined: Thu Dec 21, 2006 2:56 pm
Posts: 1
Hi, hughmadden could you repost your helper tool if possible?
thanks


Top
 Profile  
 
 Post subject: Dotpathcriteria code by hughmadden
PostPosted: Sat Mar 24, 2007 2:25 am 
Newbie

Joined: Tue May 30, 2006 8:54 pm
Posts: 5
Dear hughmadden
If you could share your dotpathcriteria code, that will help me too. I have similar requirement.

Regards
Ulag


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.