ViliDuarte1
ViliDuarte1

Reputation: 15

Confusing about dynamic-update and select-before-update in detached object in HIbernate

Hi i litle bit confused about this what in typed in title, becasue i work with this first time, Ok right to the point i have Dealer mapping and DealerData both in bi-direction

<hibernate-mapping>
<class name="sk.bantip.hotel.server.dao.book.DealerData" table="book_dealerData" >
    <id column="idBook_dealerData" name="id" type="java.lang.Long">
        <generator class="identity" />
    </id>
    <property name="name" not-null="true" />
    <property name="registration" not-null="true"/>
    <property name="taxNumber" not-null="true" />
    <property name="timestamp" not-null="true"  />
    <property name="authorUserId" column="idAuthor" not-null="true" update="false"/>
    <many-to-one name="dealer" column="idBook_dealer"
        not-null="true" class="sk.bantip.hotel.server.dao.book.Dealer" update="false"/>
    <property name="channel" not-null="false" update="false">
        <type name="sk.bantip.core.enums.GenericEnumUserType">
            <param name="enumClass">sk.bantip.hotel.server.dao.book.Channel</param>
            <param name="identifierMethod">getValue</param>
            <param name="valueOfMethod">getByCode</param>
        </type>
    </property>
    <property name="street" not-null="false"/>      
    <property name="number" not-null="false" />
    <property name="zip" not-null="false" />
    <property name="city" not-null="false" />
    <property name="country" not-null="false" />
    <property name="telephone" not-null="false" />
    <property name="email" not-null="false" />
</class>

and here is a Dealer mapping

<hibernate-mapping>
<class name="sk.bantip.hotel.server.dao.book.Dealer" table="book_dealer"  >
    <id column="idBook_dealer" name="id" type="java.lang.Long">
        <generator class="identity" />
    </id>
    <many-to-one name="domain" column="idMain_domain"
        not-null="true" class="sk.bantip.hotel.server.dao.main.Domain"  />
    <many-to-one name="activeData" column="idBook_dealerData"
        class="sk.bantip.hotel.server.dao.book.DealerData" cascade="save-update"/>
    <set name="historyData" inverse="true">
        <key column="idBook_dealer" />
        <one-to-many class="sk.bantip.hotel.server.dao.book.DealerData" />
    </set>
    <property name="status" not-null="false" />
</class>

problem is when i try update e.g single not-null="false" value than occur a stacktrace that som other not-null="true" value cannot be null, so thats means hibernate try update also unmodified fields,so i try dynamic-update="true" but looks not works still try rewrite unmodified values. somewhere i read that this not work in detached object, only in session,

I not sure whether i use detached object or session by my code because i use some DAO methods which i dont write myself, for that i confused about this, but also find select-before-update=true but dont know this, can you explain me whether is useful in this case?

so i requierd from hibernate to update value in this state(e.g. for 'name'):

Hibernate: 
update
    book_dealerData 
set
    name=?,       
where
    idBook_dealerData=?

but SQL modify all of them like this:

Hibernate: 
update
    book_dealerData 
set
    name=?,
    registration=?,
    taxNumber=?,
    timestamp=?,
    street=?,
    number=?,
    zip=?,
    city=?,
    country=?,
    telephone=?,
    email=? 
where
    idBook_dealerData=?

If anybody knows about this more, please left a post i'll be grateful also i can give more information (update methods,etc.)if be needed, Thank you so far cheers

Upvotes: 0

Views: 3658

Answers (1)

Mariusz
Mariusz

Reputation: 2727

  1. dynamic-update="true" should be used in conjunction with select-before-update=true
  2. Also, if If you use Session.merge(), According to Hibernate doc, version/timestamp columns should be used as a optimistic locking strategy in order for Session.merge() to handle correctly modifications made to detached instances
  3. If a Hibernate Session gets closed (for example you display a web page), the persistent instance of an entity (one that has a representation in the database and a identifier value) will become a detached instance (one that isn't attached to a Session anymore - but can still be modified and reattached to a new Session in subsequent request).

Hope some of this helps.

Upvotes: 3

Related Questions