-->
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.  [ 10 posts ] 
Author Message
 Post subject: Easy question - Trying to understand better associations
PostPosted: Fri Sep 09, 2005 8:08 am 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Hello everyone,

This is a very easy question, but I must ask it. I have read hibernate's first steps, but i still didn't really understand the associations.

Let's say I have 2 classes: Order and Items. Each class Order has many class Items. So it's a one to many association, right?
So, to implement it... I must create a property like "orderId" in the Items class.. and it's bean must have a setOrderId and getOrderId... just normal, right? So, though I associated them in the mapping file, the mapping does nothing to help me, I must do it all.. right? If I must do everything, why do I have to associate? What does hibernate do for me?

Ok, let's say I have in my class Order a property called "orderName".. what do I do if I want to select an Item, and instead of showing it's orderId I wanna show the orderName? I know I'd normally do it with a subquery, but how do I do it using hibernate? Could you give me a query example?


Top
 Profile  
 
 Post subject: Re: Easy question - Trying to understand better associations
PostPosted: Fri Sep 09, 2005 8:33 am 
Newbie

Joined: Fri Jul 30, 2004 9:02 am
Posts: 9
Location: Ukraine
moonlight wrote:
Let's say I have 2 classes: Order and Items. Each class Order has many class Items. So it's a one to many association, right?
So, to implement it... I must create a property like "orderId" in the Items class.. and it's bean must have a setOrderId and getOrderId... just normal, right? So, though I associated them in the mapping file, the mapping does nothing to help me, I must do it all.. right? If I must do everything, why do I have to associate? What does hibernate do for me?

Hi, you should do this in case you want inverse association. In case you have Order and Items, it is not nessesary to add property orderId for Item, hibernate will add one more columt to item table. You should define this columt in collection mapping:
Code:
  <set name="items">
      <key column="orderId" />
      <one-to-many  class="Item />
    </set>

Quote:
Ok, let's say I have in my class Order a property called "orderName".. what do I do if I want to select an Item, and instead of showing it's orderId I wanna show the orderName? I know I'd normally do it with a subquery, but how do I do it using hibernate? Could you give me a query example?

In such situation you should use inverse assotiation. Its means that in Item mapping should be mapping like this:
Code:
<many-to-one property="order" class="Order" column="orderId"/>

and in Item add new property order of Order class.
this is all.

_________________
--
Let's do a life easier


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 8:44 am 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Let me just check if I really understood. I don't have to add getters and setters for OrderId in my Items bean? Hibernate will "find out" which value to put there, just because of the mapping?

In the second case.. the inverse association should be placed in the mapping file of Order or of Item? It will never show OrderId and always show Order, or it could show both?

Thanks very much


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 1:46 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
I've just tried to do it as the friend above said... but taking out the getters and setters didn't work (after all, how would hibernate find out?)

Can someone explain it better?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 2:03 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Oh I must add some more information. Let's say I have I CUSTOMERS table. And I have their addresses filled in... each address has a city, and I have a CITY table where the addresses come from. So, a customer only belongs to a city, but a city has many customers. How should I map this in CUSTOMERS.hbm.xml?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 4:36 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Your Java classes will have direct associations, just like you would expect for an object-oriented approach. Your Item class will have an Order member, and getOrder() and setOrder() methods (not get/setOrderId()). Your Order class could also have a Set of Items.

Your Item mapping file is what tells hibernate to store an order_id in the item table for the Item's Order member. You declare a <many-to-one> element, with class="Order" and column="order_id" properties (just like xobo explained). That tells Hibernate that order_id is the foreign key to the order table.

Your Order mapping file can have an inverse one-to-many that tells Hibernate how to look up the order's items (again, just like xobo explained). This doesn't affect the database schema, but makes Hibernate do the work for you to get all Items for an Order.

Intead of trying to create classes and mapping files and get them to work together, I find it helpful to just define the mapping file and use the Hibernate Tools to generate the Java classes (and database schema). That way you know the Java class is set up to work with your mapping file. If the Java file doesn't look like you expected, then you go back to your mapping file and figure out what you did wrong.

Go through some of the examples from the docs to get a better understanding. Good luck.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:20 am 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Well, unfortunately afaik there's no Hibernate Tools for me, Netbeans user. But it doesn't matter, cause I got I better understanding now and will try.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 10:25 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
The tools have Ant tasks, so you don't need Eclipse to use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 1:24 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Thank you very much... I'll try the tools.

I'll ask one last question about this subject, concerning this part:

I wrote:
Ok, let's say I have in my class Order a property called "orderName".. what do I do if I want to select an Item, and instead of showing it's orderId I wanna show the orderName? I know I'd normally do it with a subquery, but how do I do it using hibernate? Could you give me a query example?

xobo wrote:
In such situation you should use inverse assotiation. Its means that in Item mapping should be mapping like this:

<many-to-one property="order" class="Order" column="orderId"/>

and in Item add new property order of Order class.
this is all.


so if I wanna show (only for display matters) the orderName instead of orderId, I'll put the mapping that way? It didn't work here.. it seems like it's replacing the ID when I do it. Maybe it's the way I added the property order in Item...should it have any specific notation?

thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 3:27 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Sorry, not sure I understand your question. Your Item object will have an Order object, so you can use whatever you want from the order for display (item.getOrder().getOrderName() or item.getOrder().getXXX()).


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