-->
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: 1-many relationship: Redundant update of inverse (many) end
PostPosted: Wed Sep 07, 2005 10:25 am 
Beginner
Beginner

Joined: Wed Sep 07, 2005 9:57 am
Posts: 20
Hibernate version: 2.1.7

Mapping documents:
<hibernate-mapping package="myPackage.solicitation">
<class name="Solicitation" table="SOLICITATION" lazy="true">
<meta attribute="scope-field">protected</meta>
<meta attribute="implement-equals">true</meta>
<id name="id" column="SOLICITATION_ID" type="long" unsaved-value="0">
<meta attribute="scope-set">protected</meta>
<meta attribute="use-in-tostring">true</meta>
<generator class="sequence">
<param name="sequence">SOLICITATION_SEQ</param>
</generator>
</id>
<version name="version" column="VERSION" access="net.sf.hibernate.property.DirectPropertyAccessor">
<meta attribute="scope-field">protected</meta>
<meta attribute="scope-set">protected</meta>
<meta attribute="scope-get">protected</meta>
</version>
...
<set name="orders" lazy="true" inverse="true" where="MOD_FLAG != 2">
<meta attribute="scope-set">protected</meta>
<key column="SOLICITATION_ID" />
<one-to-many class="myPackage.Order" />
</set>
...
</class>
</hibernate-mapping>



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="myPackage.order">
<class name="Order" table="ORDERS">
<meta attribute="scope-field">protected</meta>
<meta attribute="implement-equals">true</meta>
<id name="id" column="ORDER_ID" type="long" unsaved-value="0">
<meta attribute="scope-set">protected</meta>
<meta attribute="use-in-tostring">true</meta>
<generator class="sequence">
<param name="sequence">ORDER_SEQ</param>
</generator>
</id>
<version name="version" column="VERSION" access="net.sf.hibernate.property.DirectPropertyAccessor">
<meta attribute="scope-field">protected</meta>
<meta attribute="scope-set">protected</meta>
<meta attribute="scope-get">protected</meta>
</version>
...
<many-to-one name="solicitation" column="SOLICITATION_ID" class="myPackage.Solicitation">
<meta attribute="use-in-tostring">true</meta>
</many-to-one>
...
</class>
</hibernate-mapping>


Code:
...
Solicitation solicitation = new SolicitationDAO().getById("1");
Order order = new Order();
order.setOrdered(new Date());
solicitation.addOrder(order);
BaseDAO.save(order);
...

public class Solicitation {
...
public void addOrder(Order order) {
this.orders.add(order);
order.setSolicitation(this);
}
...
}

Name and version of the database you are using: Oracle 8

The generated SQL (show_sql=true):
insert into ORDERS (VERSION, ADDRESS_TYPE_ID, ENCODING_ID, PURCHASE_ORDER_ID, SOLICITATION_ID, STATE_ID, ID_USERINFO, VIDEO_MEDIA_ID, COUNTRY_KEY, SOLICITOR_ID, ID_PREFIX, ODS_CUSTOMER_ID, TRACKING_NO, SECONDARY_STATE, SECONDARY_ERROR_TEXT, QUANTITY, UNIT_PRICE, UNIT_PRICE_CURRENCY, ADDRESSEE, ADDRESSEE_SUPPLEMENT, STREET, ZIP, CITY, COUNTY, ORDERED, SHIPPED, SHIPPING, COURIER, COMMENT_FIELD, COMMENT_PO, MOD_FLAG, MOD_STAMP, MOD_USER, ORDER_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

update SOLICITATION set VERSION=?, PRIORITY_ID=?, SOLICITOR_ID=?, NAME=?, CREATOR_ID=?, DEADLINE=?, COMMENT_FIELD=?, CREATED=?, EMAILED=?, MOD_FLAG=?, MOD_STAMP=?, MOD_USER=? where SOLICITATION_ID=? and VERSION=?


Hi,

I have trouble with a bidirectional 1-many relationship. Basically there is an update on the inverse many end which I would not expect to happen.

The many end is the class Solicitation (set as the inverse ending of the relationship), the one end is the class Order.

-I load a Solicitation object from the database
-I create a new Order object
-I add the order to the solicitation and set the solicitation on the order
-I save the order
-In the log file I can see the Insert-statement for the order but also an update for the solicitation - even though nothing changed here.

Why is this update on the solicitation executed? Anybody any idea? I appreciate it.

Thanks,
Ronald


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 1:19 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
Try setting dynamic-update and dynamic-insert to true in the solicitation class mapping. Since you changed the solicitation object, hibernate might not know what attributes have changed unless you set these two attributes to true.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 4:28 am 
Beginner
Beginner

Joined: Wed Sep 07, 2005 9:57 am
Posts: 20
Nope - did not help.

Still executed an update on the soliciation-table - even though only the version was updated ;-)

update SOLICITATION set VERSION=? where SOLICITATION_ID=? and VERSION=?


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.