-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problem with a select - how to reference an entity?
PostPosted: Fri Aug 07, 2009 7:38 am 
Newbie

Joined: Fri Aug 07, 2009 6:25 am
Posts: 2
Hi, this is my first post in this forum. I'm new with hibernate. I'm using hibernate with jpa and annotations for mapping through classes. Now, I have two class: Order and User. A user can do one or many orders so there is a one-to-many connection from User to Order. This connection is represented by a private field in the Order class called user whose type is User. In other words, this user field is the foreign key.

Now, I need a select which allow to extract all the orders of a particular user. How can I do this? I tried a query like this:
select new List(idOrder, articlename) from Order as od where od.user.idUser='1'

This query doesn't work... I think there is some problem in the reference to User entity through user field (od.user.idUser). How does the reference to entity field work?

Thanks to all people that will help me :-)


Top
 Profile  
 
 Post subject: Re: Problem with a select - how to reference an entity?
PostPosted: Mon Aug 10, 2009 7:26 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I'd stop doing the SQL.

Just get the user.

Then, just say user.getOrders();

Hibernate will do the query for you, getting you all the associated orders. That's how Hibernate makes object-relational mapping easy!

Plus, it will all happen within one transaction. Efficient!

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Problem with a select - how to reference an entity?
PostPosted: Wed Aug 19, 2009 12:00 pm 
Newbie

Joined: Fri Aug 07, 2009 6:25 am
Posts: 2
Thank you very much!

Best regards


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