-->
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: Version column forcing update
PostPosted: Thu Dec 08, 2005 5:51 am 
Newbie

Joined: Sat Apr 10, 2004 12:14 am
Posts: 7
Using Hibernate 3.0.5, MySql 5.0...

Can someone enlighten me regarding the following issue....

As soon as I added a version column to a parent class (PurchaseOrder that has a collection of PurchaseOrderLine objects) the session starting forcing an update to the PurchaseOrder header record -- EVEN if no property values changed. This was working perfectly BEFORE I added the version column.

I presume it is an issue with the one-to-many association with lines (mapped "all-delete-orphan" because I am successfully using version columns elsewhere in my application. However, this is the first place where I am using a version column in a class mapped with a one-to-many association.

The PurchaseOrder object is being populated in the web tier outside an active session but it is being reattached/updated via the merge() method on the session inside a business tier method. Again, I've tested that it is the addition of the version column mapping that is changing the behavior and forcing the update. When I remove the version column, the session is not flushing a "dirty" PurchaseOrder when the property values didn't change. For reference, the relevant portion of my mapping file:

Code:
    <class name="com.mydomain.PurchaseOrder" table="purchase_order" >
        <id name="id" column="id">
            <generator class="identity"/>
        </id>
        <version name="version" column="version"/>
        <many-to-one name="supplier" column="supplier_id" not-null="true"/>
        <many-to-one name="location" column="location_id" not-null="true"/>
        <many-to-one name="buyingCompany" column="buying_company_id" not-null="true"/>
        <many-to-one name="targetOwner" column="target_owner_id" not-null="true"/>
        <many-to-one name="shippingAddress" column="shipping_address_id" not-null="true"/>
        <many-to-one name="shippingMethod" column="shipping_method_id" not-null="true"/>
        <many-to-one name="paymentTerms" column="payment_terms_id" not-null="true"/>
        <property name="currency" column="currency" type="currency" not-null="true"/>
        <property name="freightOnBoard" column="freight_on_board" type="freightOnBoard" not-null="true"/>
        <property name="purchaseOrderNumber" column="purchase_order_number" not-null="true"/>
        <property name="status" column="purchase_order_status" type="purchaseOrderStatus" not-null="true"/>
        <property name="priority" column="purchase_order_priority" type="purchaseOrderPriority" not-null="true"/>
        <property name="note" column="note"/>
        <property name="approvalDate" column="approval_date"/>
        <property name="approvalUser" column="approval_user"/>
        <property name="transmittedAt" column="transmitted_at"/>
        <set name="lines" cascade="all-delete-orphan" inverse="true">
            <key column="purchase_order_id"/>
            <one-to-many class="com.mydomain.PurchaseOrderLine" />
        </set>
        <property name="lineCount" access="field" formula="(select count(*) from purchase_order_line l where l.purchase_order_id = id)"/>
        <property name="itemsCount" access="field" formula="(select sum(l.quantity) from purchase_order_line l where l.purchase_order_id = id)"/>
        <property name="total" access="field" formula="(select sum(l.extended_purchase_price) from purchase_order_line l where l.purchase_order_id = id)"/>
        <property name="updatedAt" column="updated_at"/>
        <property name="createdAt" column="created_at"/>
        <property name="updatedUser" column="updated_user"/>
        <property name="createdUser" column="created_user"/>
    </class>
    <class name="com.mydomain.PurchaseOrderLine" table="purchase_order_line">
        <id name="id" column="id">
            <generator class="identity"/>
        </id>
        <many-to-one name="purchaseOrder" column="purchase_order_id" />
        <many-to-one name="purchaseInfo" column="purchase_info_id" />
        <property name="quantity" access="field" column="quantity"/>
        <property name="purchaseType" column="purchase_type" type="purchaseType"/>
        <property name="unitPurchasePrice" access="field" column="unit_purchase_price"/>
        <property name="extendedPurchasePrice" access="field" column="extended_purchase_price"/>
        <set name="purchaseRequests" cascade="save-update" inverse="true">
            <key column="purchase_order_line_id"/>
            <one-to-many class="com.mydomain.PurchaseRequest" />
        </set>
        <property name="dueDate" column="due_date"/>
        <property name="updatedAt" column="updated_at"/>
        <property name="createdAt" column="created_at"/>
        <property name="updatedUser" column="updated_user"/>
        <property name="createdUser" column="created_user"/>
    </class>


Feel free to point out specific references to Hibernate in Action as I may have missed something that specifically addresses this issue.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 6:36 am 
Newbie

Joined: Mon Dec 12, 2005 4:26 pm
Posts: 1
I'm running into the same problem with a version column on a parent class that has a mapped one-to-many association (cascade all-delete-orphan) with a child class.

It definitely seems to be the one-to-many association that is causing the problem, because if I remove the association mapping, the versioned header doesn't incorrectly get marked as dirty if no changes have been made to the property values.

Have you made any progress on this issue?

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 7:32 am 
Newbie

Joined: Sat Apr 10, 2004 12:14 am
Posts: 7
Yes, I also confirmed that it is the child one-to-many relationship that is the culprit. If I remove the lines mapping, everything works as expected. Unfortunately, I have not made any progress on solving the problem and none of the "experts" have replied to my original post.

Further testing did also confirm that it is not an issue of a detached/re-attached object graph. I confirmed this by adding a servlet filter that opens a session and puts it in thread local (a common pattern, I believe) so that all persistent object manipulation is using the same hibernate session. Still, I am seeing the parent object flush an update to the database even though no properties are being changed.

What is even more strange is that I introduced an entity interceptor to discover what properties hibernate thinks are dirty. Here is a snippet from my onFlushDirty method in the interceptor:

Code:
        for (int i = 0; i < propertyNames.length; i++) {
            if (previousState[i] != currentState[i]) {
                log.debug("The state of property " + propertyNames[i] + " has changed [" + previousState[i] + " -> " + currentState[i] + "]");
            }
        }


Here is an excerpt from a log file related to the above code.


    06:12:30,611 DEBUG HibernateAuditableInterceptor:178 - Logged update to com.mydomain.PurchaseOrder
    06:12:30,611 DEBUG HibernateAuditableInterceptor:178 - The state of property version has changed [1 -> 1]
    06:12:30,611 DEBUG HibernateAuditableInterceptor:178 - The state of property lineCount has changed [1 -> 1]
    06:12:30,631 DEBUG HibernateAuditableInterceptor:178 - The state of property itemsCount has changed [5 -> 5]
    06:12:30,681 DEBUG HibernateAuditableInterceptor:178 - The state of property updatedAt has changed [2005-12-12 20:27:57.0 -> 2005-12-12 20:27:57.0]
    06:12:30,681 DEBUG HibernateAuditableInterceptor:178 - The state of property createdAt has changed [2005-12-12 20:27:41.0 -> 2005-12-12 20:27:41.0]


Why would the "state" of these properties be showing a change? Note that there are many more properties than just these so why would these properties show up as dirty? Remember, if I simply remove the version column mapping, then nothing gets flushed (unless, of course, it changes). Something is really weird here.....


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.