-->
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: Mapping properties from a Many-To-One...
PostPosted: Tue Jul 18, 2006 11:54 pm 
Newbie

Joined: Tue Mar 22, 2005 6:01 pm
Posts: 13
Location: Sydney, Australia
Hi all,

I'm hopeful that this can be done in Hibernate, i'm just not sure how it would be mapped.

I have two tables PRODUCT and ORDER_LINE

PRODUCT
-------
product_id (pk)
product_name


ORDER_LINE
----------
order_line_id (pk)
product_id (fk)
product_name
qty

Naturally i have two classes Product and OrderLine and OrderLine has a Many-To-One relationship with Product.

What i want is that when i save OrderLine i also want the productName property of the Product object to be written into the product_name column of the ORDER_LINE table.

No idea how to map this though in hibernate. Any ideas from anyone?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 12:38 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
Hmmm, there's probably a business reason why you want redundant product_name data in both tables. Since you have a fk relationship to Product, then you have product_name from the Product table.

But, to answer your question:
Two scenarios - insert and update

If you are inserting/creating an OrderLine and assigning an existing Product then assign OrderLine.productName like this (pseudo-code):

Code:
setProductName(theNameOfYourProductInstance.getProductName());


If you are updating an existing OrderLine then you can use your hierarchy and reach into product like this:

Code:
setProductName(orderline.product.getProductName());


Quote:
Don't reach too far or you'll break all kinds of pragmatic programming rules :-).


I don't think you can map something like this because product_name is not part of the relational hierarchy. <overkill>You could probably use a db trigger to do it </overkill>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 12:56 am 
Newbie

Joined: Tue Mar 22, 2005 6:01 pm
Posts: 13
Location: Sydney, Australia
Yeah jruffin,

I know there are workarounds like having a productName property in the OrderLine class or using a db trigger.

I was really hoping for a way to map this though...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 2:58 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
It could be done the following way: -

Code:
hbm
--------

<class name="OrderLine" table="OrderLine">
...
    <property name="productName" column="product_name"/>
...
</class>



OrderLine class
------------------

String productName;
Product product;

public void setProduct(Product product) {
this.product = product;
}

public void setProductName(String productName) {
   this.productName = productName;
}

public String getProductName() {
   if (product != null) {
       return product.getName();
   } // else return null or what was set initially? Depends on requirements.

   return this.productName;
}


The idea is to map the product_name as a normal property and then add the code in get/set of Product and productName to keep it in sync. You would not have to call get/set of productName explicitly. When Hibernate calls the get/set of productName, its value would be fetched from product.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:07 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
In the setProduct method you could set the productName also, but I don't think it is required.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:22 am 
Newbie

Joined: Tue Mar 22, 2005 6:01 pm
Posts: 13
Location: Sydney, Australia
Thanks jayesh but that's not really a mapping solution. I don't want to a productName property in my OrderLine class... to be honest i'd rather use a db trigger as i like to keep my code clean and following OO principles.

So is everyone pretty much saying that it CAN'T be done using a pure hibernate mapping solution?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:47 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
Your original post is a little ambiguous... In it you have productName under table OrderLine... in later posts you mention you don't want productName in OrderLine. I may not be following you correctly.

If you don't want productName in OrderLine and you already have productName in Product - I don't understand what's left...

What's the question at hand?
Quote:
What i want is that when i save OrderLine i also want the productName property of the Product object to be written into the product_name column of the ORDER_LINE table.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:56 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
Oh, you want to store in table OrderLine.productName the value from table Product.productName but not have java object OrderLine contain a productName attribute and/or accessors? And you'd like this controlled by a mapping file?

Not.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 4:20 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
You could try and use <join>.


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.