-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate 3 updates Table content automatically
PostPosted: Tue Nov 04, 2008 11:22 pm 
Newbie

Joined: Tue Nov 04, 2008 10:56 pm
Posts: 2
Hi

I am developing a project using hibernate to store data in MySQL, the table is designed to store a tree (primary key becomes foreign key in another column).

there is a design rule that any node contains descendants are not allowed to hold the total amount, it should return the summation of the descendants when getTotal() is called.

it works fine when I insert, delete a node from tree, but when I do summation from one node, I found that the ancestor's total column has been modifed to the summation of its all descendants by hibernate. this is not allowed and I am so curious what caused this.

Can any one help me with this problem? thanks very much

This is the XML I used for the table


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.jinfrent.artist.model">
<class name="itemList" table="ITEM_IST">
<id name="listID" column="LIST_ID"/>
<property name="name" column="NAME" type="string" not-null="true"/>
<property name="ancestorID" column="ANCESTOR_ID" type="string"/>
<property name="total" column="TOTAL" type="double"/>
<property name="status" column="STATUS" not-null="true"/>
<set name = "descendants" inverse="true" lazy="false">
<key column = "ANCESTOR_ID"/>
<one-to-many class="itemList"/>
</set>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 5:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate will call your itemList.getTotal() method for all loaded objects and save that value in the database. The getTotal() method should return the same value that was passed to setTotal(). If you need a method that sums the total of all 'descendants' you should call this something else. Eg. getSummedTotal().


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 5:59 am 
Newbie

Joined: Tue Nov 04, 2008 10:56 pm
Posts: 2
nordborg wrote:
Hibernate will call your itemList.getTotal() method for all loaded objects and save that value in the database. The getTotal() method should return the same value that was passed to setTotal(). If you need a method that sums the total of all 'descendants' you should call this something else. Eg. getSummedTotal().


Hi nordborg ,

Thanks for your help, I wonder if you can share some reference about this automatic staff in Hibernate with me? so I can go and further understand how to use it.
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 6:31 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is a core Hibernate feature that is called 'Automatic dirty checking'. In short it means that Hibernate remembers the values that was used to initialize entities with all setSomething() methods. When the session is flushed Hibernate will call all getSomething() methods to see if the value has changed and update the database with the new values if it has. Since Hibernate is calling set setSomething() and getSomething() methods quite often it is recommended that the methods are kept simple. The best is to just store/return the value.

I can't find any document describing this except a short note on http://www.hibernate.org/345.html


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