-->
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 Updates Sub Entity Objects when Save Top Object
PostPosted: Fri Sep 25, 2009 4:33 pm 
Newbie

Joined: Thu Sep 24, 2009 10:32 am
Posts: 3
Hi there. Let's suppose there is a top level entiry object called ClaimCase. In each claim case you can have 0+ instances of Party. Party can be subclassed, examples are Claimant, Employer and EmployerClaimant.

Also, each ClaimCase can have 0+ instance of Requirement and each instance of Requirement can be 0+ instance of Activity.

The hibernate mapping would look something like this:

Code:
<?xml version="1.0"?>
<!-- Created with Liquid XML Studio - FREE Community Edition 7.1.4.1284 (http://www.liquid-technologies.com) -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"[]>
<hibernate-mapping package="com.standard.ulterap8.claimintake.dao.impl">
   <class name="Party" abstract="true" table="PARTY" optimistic-lock="version" polymorphism="implicit">
      <meta attribute="implements">com.xxx.xxx.xxx.xxx.Auditable</meta>
      <id name="partyId" type="integer">
         <meta attribute="scope_set">protected</meta>
         <meta attribute="use-in-tostring">true</meta>
         <column name="PARTY_ID" not-null="true">
         </column>
         <generator class="sequence">
         </generator>
      </id>
      <discriminator column="PARTY_TYPE" type="string" />
      <version name="version" type="integer" access="field" column="OBJ_VERSION" unsaved-value="negative" />
      <property name="createdTimestamp" column="CREATE_TIMESTAMP" type="com.ipd.common.dao.HibernateUTC$TimestampType" not-null="true">
         <meta attribute="use-in-equals">true</meta>
      </property>
      <!-- Deleted Properties --->
      <subclass name="Caller" discriminator-value="Caller">
         <!-- Deleted Properties --->
      </subclass>
   </class>
   <subclass abstract="true" name="PartyProfile" extends="Party">
      <!-- Deleted Properties --->
      <many-to-one name="claimCase" class="ClaimCase" column="CASE_ID" foreign-key="PRTY_CLMCASE_FK" />
      <subclass name="Claimant" discriminator-value="Claimant">
         <!-- deleted properties --->
      </subclass>
      <subclass name="Employer" discriminator-value="Employer">
         <!-- deleted properties --->
      </subclass>
      <subclass name="EmployerClaimant" discriminator-value="EmployerClaimant">
         <!-- deleted properties --->
      </subclass>
   </subclass>
   <class name="Requirement" table="REQUIREMENT" optimistic-lock="version">
      <meta attribute="implements">com.xxx.xxx.xxx.xxx.Auditable</meta>
      <id name="requirementID" type="integer">
         <meta attribute="scope_set">protected</meta>
         <meta attribute="use-in-tostring">true</meta>
         <column name="REQ_ID" not-null="true">
         </column>
         <generator class="sequence">
         </generator>
      </id>
      <version name="version" type="integer" access="field" column="OBJ_VERSION" unsaved-value="negative" />
      <property name="configID" type="integer" column="REQ_CFG_ID" not-null="true">
         <meta attribute="use-in-equals">true</meta>
      </property>
      <property name="name" type="string" length="200" column="REQ_NM" not-null="true" />
      <!-- deleted properties --->
      <property name="createdTimestamp" column="CREATE_TIMESTAMP" type="com.ipd.common.dao.HibernateUTC$TimestampType" not-null="true">
         <meta attribute="use-in-equals">true</meta>
      </property>
      <many-to-one name="claimCase" class="ClaimCase" column="CASE_ID" not-null="true" foreign-key="REQUIREMENT_CLMCASE_FK" />
      <set name="activities" inverse="true" cascade="all, delete-orphan" lazy="false" order-by="SORT_KEY asc">
         <key column="REQ_ID" />
         <one-to-many class="Activity" />
      </set>
   </class>
   <class name="Activity" table="ACTIVITY" optimistic-lock="version">
      <meta attribute="implements">com.xxx.xxx.xxx.xxx.Auditable</meta>
      <id name="activityID" type="integer">
         <meta attribute="scope_set">protected</meta>
         <meta attribute="use-in-tostring">true</meta>
         <column name="ACTIVITY_ID" not-null="true">
         </column>
         <generator class="sequence">
         </generator>
      </id>
      <version name="version" type="integer" access="field" column="OBJ_VERSION" unsaved-value="negative" />
      <property name="createdTimestamp" column="CREATE_TIMESTAMP" type="com.ipd.common.dao.HibernateUTC$TimestampType" not-null="true">
         <meta attribute="use-in-equals">true</meta>
      </property>
      <property name="dependencyReqID" type="integer" column="DEP_REQ_ID" not-null="true" />
      <property name="configID" type="integer" column="ACT_CFG_ID" not-null="true">
         <meta attribute="use-in-equals">true</meta>
      </property>
      <!-- deleted properties --->
      <many-to-one name="requirement" class="Requirement" column="REQ_ID" not-null="true" foreign-key="ACTIVITY_REQ_FK" />
   </class>
   <class name="ClaimCase" table="CLAIMCASE" optimistic-lock="version">
      <meta attribute="implements">com.xxx.xxx.xxx.xxx.Auditable</meta>
      <meta attribute="implement-equals">true</meta>
      <meta attribute="generated-class">com.xxx.xxx.xxx.xxx.xxx.ClaimCaseBase</meta>
      <meta attribute="scope-field">protected</meta>
      <id name="ciwId" type="integer">
         <meta attribute="scope_set">protected</meta>
         <meta attribute="use-in-tostring">true</meta>
         <column name="CASE_ID" not-null="true">
         </column>
         <generator class="sequence" />
      </id>
      <version name="version" access="field" column="OBJ_VERSION" type="integer">
      </version>
      <property name="createdTimestamp" type="com.ipd.common.dao.HibernateUTC$TimestampType" column="CREATE_TIMESTAMP" not-null="true">
         <meta attribute="use-in-equals">true</meta>
      </property>
      <!-- deleted properties --->
      <property name="sectorCode" type="string" length="7" column="SECTOR_CD" not-null="false" />
      <one-to-one name="insuredInfo" class="InsuredInfo" property-ref="claimCase" cascade="all" />
      <set name="intakeActivities" inverse="true" cascade="all, delete-orphan" lazy="false" order-by="CREATE_TIMESTAMP asc">
         <key column="CASE_ID" />
         <one-to-many class="IntakeActivity" />
      </set>
      <set name="parties" inverse="true" cascade="all, delete-orphan" lazy="false">
         <meta attribute="scope_set">protected</meta>
         <key column="CASE_ID" />
         <one-to-many class="PartyProfile" />
      </set>
      <set name="requirements" inverse="true" cascade="all, delete-orphan" lazy="false" order-by="SORT_KEY">
         <key column="CASE_ID" />
         <one-to-many class="Requirement" />
      </set>
      <property name="lastChangeDate" type="com.ipd.common.dao.HibernateUTC$TimestampType" column="LAST_CHG_DATE" not-null="false" />
      <property name="lastChangeUser" type="string" length="100" column="LAST_CHG_USER" not-null="false" />
   </class>
</hibernate-mapping>


I've noticed that when we save by using session.persist(), even if I changed only one thing in ClaimCase (topmost object) or in Activity (bottomost object), the entire object gets updated. (Meaning that the ClaimCase, Employer, Claimant, EmployerClaimant, Requirement and Activities for the claim case are updated ... because when I look at the database, I discovered all the version numbers are changed. Wondering if anyone else experience the same thing and is there a way to tell Hibernate to "smart" update?


Top
 Profile  
 
 Post subject: Re: Hibernate Updates Sub Entity Objects when Save Top Object
PostPosted: Sun Sep 27, 2009 7:47 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi radar001,

what kind of object do you pass to session.persist()?

According to your mapping you cascade all operations of your ClaimCase to the associated entities.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Hibernate Updates Sub Entity Objects when Save Top Object
PostPosted: Mon Sep 28, 2009 4:13 pm 
Newbie

Joined: Thu Sep 24, 2009 10:32 am
Posts: 3
Hello Froestel,

Thanks for replying. So the cascade="all" attribute is the cause of the "unnecessary" updates? I wonder if there is another option is I do not use cascade="all"?

Thanks,

radar001


Top
 Profile  
 
 Post subject: Re: Hibernate Updates Sub Entity Objects when Save Top Object
PostPosted: Mon Sep 28, 2009 5:15 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi radar001,

radar001 wrote:
...I wonder if there is another option is I do not use cascade="all"?...


did you mean as I do not or if I do not?

You mapped all your sets as far as I can see
Code:
cascade="all, delete-orphan"
So you apparently are using cascade all.

Cascading can be tuned on a very granular level, refer to the Hibernate docs, i.e. Sec. 6 http://docs.jboss.org/hibernate/stable/core/reference/en/html/collections.html

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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.