-->
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.  [ 9 posts ] 
Author Message
 Post subject: n:m create Query
PostPosted: Wed May 31, 2006 9:50 am 
Newbie

Joined: Wed May 31, 2006 9:31 am
Posts: 9
Location: Südtirol
Hi!
I have a (for an advanced user shurley easy) question. I have two classes User and Project. Every User has a list of Projects. They are n:m mapped. To find all Users of a specific Project is no Problem, I do this with
list = session.createQuery("from User1 where projectList.id="+p.getId()).list(); where p is a given Project. But how can I get all Projects of a User?

Thx


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 10:17 am 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
just wondering, but are you mapping it via a middleman m:n table? if you map it both ways in the hibernate you should be able to do the same query in the opposite direction


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 5:39 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Try this,

lazy="false" in the set or bag property of the Projects.

You will have the values.

lazy (optional - defaults to proxy): By default, single point associations are proxied. lazy="no-proxy" specifies that the property should be fetched lazily when the instance variable is first accessed (requires build-time bytecode instrumentation). lazy="false" specifies that the association will always be eagerly fetched

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 5:42 am 
Newbie

Joined: Wed May 31, 2006 9:31 am
Posts: 9
Location: Südtirol
Yes shure I could do this, but I'm working in a team and we decided to do it with a many to many relationship.
Thx anyway for the reply


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 5:43 am 
Newbie

Joined: Wed May 31, 2006 9:31 am
Posts: 9
Location: Südtirol
Thx I'm going to try this!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 6:10 am 
Newbie

Joined: Wed May 31, 2006 9:31 am
Posts: 9
Location: Südtirol
I have set my bag in User to lazy="false"; But how can I now create a query who gives me a list of all projects of a user? I can't access by the auto-generated class User_Project, this gives me an error, and in the class Project I have no reference to an User...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:05 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Set lazy="false". Use the Criteria API as below

Criteria criteria = session.createCriteria(User.class);
criteria.add(Expression.in("userId", userId));
// Assuming userId is not PK
List userList = criteria.list();

If you searching by PK
User user = (User)session.get(User.class, userId);

Above user object will have the projects in it.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:46 am 
Newbie

Joined: Mon Sep 13, 2004 8:20 am
Posts: 7
Location: Poland
Or if you don't want to have lazy="false" which with eception of rare cases shouldn't be used, do:

User user = (User)session.get(User.class, userId);

Hibernate.initialize(user.getProjectss());


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:50 am 
Newbie

Joined: Wed May 31, 2006 9:31 am
Posts: 9
Location: Südtirol
Thx, it was a little bit a stupid question, I recognized. I didn't see the simplest way,
Thank you both


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