-->
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.  [ 5 posts ] 
Author Message
 Post subject: createAlias/createCriteria with additional restriction
PostPosted: Fri Jun 11, 2010 8:16 am 
Newbie

Joined: Wed Oct 14, 2009 4:53 am
Posts: 4
Hi everbody

Using Hibernate 3 with XML mappings.

Say, there is a "Project" object referencing "Information" objects like

<set name="listOfInformation" table="information">
<key column="project_id"/>
<one-to-many class="Information"/>
</set>

Information looks like
..
<property column="language" name="language" type="string"/>
<property column="title" name="title" type="string"/>
..

Projects may have n Information children, but only one for a given language. Language and project form a unique key in information-table.

In most cases I need the complete bunch of Information children, but sometimes I want to find, say the project title in a certain language.
Now, I need to dynamically set up a Criteria query resulting in a Object[]-Array where I'm not getting an entity, thus eliminating accessing entity methods that could help me.

Question:
Is there any way of setting up a Criteria query resulting in a SQL like

select p.field1, p.field2, i.title
from project
left join information on (p.project_id=i.project_id and i.language='EN');

?
This is what I meant by "with additional restriction" in headline.

Thank for your time trying to understand and help me.
Best regards,
Claus.


Top
 Profile  
 
 Post subject: Re: createAlias/createCriteria with additional restriction
PostPosted: Tue Jun 15, 2010 4:19 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Yes, it was added in 3.5 for the Criteria and I submitted a patch for the DetachedCriteria side of things. (I'm not sure if it got into a release as of yet).

public Criteria createAlias(String associationPath, String alias, int joinType, Criterion withClause)

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: Re: createAlias/createCriteria with additional restriction
PostPosted: Tue Jun 22, 2010 4:22 am 
Newbie

Joined: Wed Oct 14, 2009 4:53 am
Posts: 4
Thank you, VampBoy. I would have to switch over to 3.5. In the meanwhile I found an alternative solution which offers even more flexibility. But it's good to know, that there would be that option, too.

Kind regards and many thanks
Claus.


Top
 Profile  
 
 Post subject: Re: createAlias/createCriteria with additional restriction
PostPosted: Tue Jun 29, 2010 5:59 pm 
Newbie

Joined: Tue Jun 29, 2010 5:52 pm
Posts: 1
Does anyone can give me an example of this method. I have written like this:

for sql:
FROM Promo
LEFT JOIN Users ON Users.ID = Promo.ID AND Users.MemberID=$this->memberID

I wrote:
.createAlias("users", "dpu", Criteria.LEFT_JOIN, Restrictions.eq("dpu.memberId", user.getMemberId()))

is that correct? it seems not because an exception is occured like NoSuchMethodError..


Top
 Profile  
 
 Post subject: Re: createAlias/createCriteria with additional restriction
PostPosted: Thu Jul 01, 2010 3:58 am 
Newbie

Joined: Thu Jul 01, 2010 3:51 am
Posts: 3
Try this:
Code:
.createAlias("users", "dpu", Criteria.LEFT_JOIN, Restrictions.eq("memberId", user.getMemberId()))


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