Version of the parent object does not change when its child object's state changes

北战南征 提交于 2020-01-04 15:54:54

问题


I am using Hibernate3 I have a simple one to many relationship(Parent object has as set of child objects) if the child objects are added/removed, the version of the parent object is updated where as if the state of the child object is changed, the version of the parent is not getting updated.

Here is the mapping - Category.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="net.codejava.hibernate">
<class name="Category" table="CATEGORY">
    <id name="id" column="CATEGORY_ID">
        <generator class="native"/>
    </id>
    <property name="name" column="NAME" />
    <version name="version" type="integer" column="version" unsaved-value="null" />
    <set name="products" inverse="true" cascade="all-delete-orphan">
        <key column="CATEGORY_ID" not-null="true" />
        <one-to-many class="Product"/>
    </set>
</class> 

Product.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="net.codejava.hibernate">
<class name="Product" table="PRODUCT">
    <id name="id" column="PRODUCT_ID">
        <generator class="native"/>
    </id>
    <version name="version" type="integer" column="version" unsaved-value="null" />
    <property name="name" column="NAME" />
    <property name="description" column="DESCRIPTION" />
    <property name="price" column="PRICE" type="float" />

    <many-to-one name="category" class="Category"
        column="CATEGORY_ID" not-null="true"/>
</class> 

When the Product changes, Product.version is updated properly but the Category.version remains the same.

I assume this is a cross cutting concern and there has to be a plausible solution for this. I did a lot of searching and could not find one. Please help me out


回答1:


Yes, this is just a limitation of the way hibernate works, I'm afraid. The only solution is to change it on both sides when you need to make a change.

You could also refresh the parent, but bear in mind that that will hit the database.




回答2:


It's probably a subjective opinion, but to me it seems logical this way. However, one common way of handling this is to have something like lastUpdated field on parent entity, which you would set each time before calling update on it. This can be done in @PrePersist and/or @PreUpdate, and it would ensure that entity version changes whenever you update it, regardless of what changes are made to it or its relations.



来源:https://stackoverflow.com/questions/27250271/version-of-the-parent-object-does-not-change-when-its-child-objects-state-chang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!