-->
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.  [ 4 posts ] 
Author Message
 Post subject: Joins with Hibernate Criteria API
PostPosted: Tue Jul 24, 2007 10:10 am 
Newbie

Joined: Thu Jun 28, 2007 7:08 am
Posts: 4
Can any body explain how to do a left outer join with the oracle 'ON' condition in Hibernate Criteria API.

Here is the sql query for which I need to get the results.

select asset.assetid from asset
left outer join assetonagreement on asset.ASSETID = assetonagreement.ASSETID

_________________
Durga


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 7:25 am 
Beginner
Beginner

Joined: Wed Oct 04, 2006 8:57 am
Posts: 25
the best information I could find on this was from this thread http://forum.hibernate.org/viewtopic.php?t=967820

looks like you can only do it in hql.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 8:25 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

List list=sess.createCriteria(asset .class,"a").createCriteria("assetonagreement","m",Criteria.LEFT_JOIN).list();


is this what you want

Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 5:48 am 
Beginner
Beginner

Joined: Wed Oct 04, 2006 8:57 am
Posts: 25
I found a way to simulate what we are looking for (I am using NHibernate but I presume something similar will work for hibernate)

This method creates a condition with the restrictions you require and then also allows for the case that no result exists.

Code:
        public static ICriteria CreateLeftOuterJoin(ICriteria criteria, string associationPath, string alias, string key, params ICriterion[] restrictions)
        {
            ICriteria joinedCriteria = criteria.CreateCriteria(associationPath, alias, JoinType.LeftOuterJoin);

            Conjunction conjunction = Expression.Conjunction();
            conjunction.Add(Expression.IsNotNull(key));

            foreach (ICriterion restriction in restrictions)
            {
                conjunction.Add(restriction);
            }

            joinedCriteria.Add(Expression.Disjunction()
                             .Add(conjunction)
                             .Add(Expression.IsNull(key))
                );
            return joinedCriteria;
        }


and then you use like this

Code:
CreateLeftOuterJoin(criteria, "parent.Children", "child","child.Id",
                    Expression.Eq("child.Age", 12));


Sorry it's c#, I assume it wouldn't look to different in java though. The idea is to create the condition that you want, and then allow that condition or null.

The key parameter lets you specify a value to check for null on, I assume any property on the object will do.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.