-->
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.  [ 10 posts ] 
Author Message
 Post subject: Hibernate recreates the collection instead of updating it
PostPosted: Tue Dec 13, 2016 5:00 am 
Newbie

Joined: Tue Dec 13, 2016 4:52 am
Posts: 7
Hello,

I have a problem with a bidirectional relationship.

I use cascade "all-delete-orphan" to persist changes in sub-objects when modifying object parent.

1. If I insert a new parent, the sub-objects are correctly inserted in DB

2. If I delete a parent, the sub-objects are correctly deleted from DB

3. On the opposite, when I try to update an object parent, Hibernate always try to insert the list of
sub-objects instead of updating them (even if they already existed in DB)
It is like it always interprets that the sub-objects are new.


In the beginning I thought that the problem could be related with the bidirectional relationship (below mapping), but
as it works for the inserts and deletions, now I am thinking that is not the point.

Parent:

Code:
<id name="bodyId" column="BODY_UID">
    <generator class="sequence">
        <param name="sequence">SEQ_BODY_UID</param>
    </generator>
</id>

<bag name="descriptions" inverse="true" fetch="join" cascade="all-delete-orphan"
    lazy="true">
    <key>
        <column name="BODY_UID" />
    </key>
    <one-to-many class="ec.ep.codict.codeorgane.data.entity.BodyDesc" />
</bag>


Child:

Code:
     
<id name="descId" column="BODY_DESC_UID">
    <generator class="sequence">
        <param name="sequence">SEQ_BODY_DESC_UID</param>
    </generator>
</id>

<many-to-one name="organe" column="BODY_UID" not-null="true" />


I really would appreciate any light.
Thank you.


Top
 Profile  
 
 Post subject: Re: Insert instead of update
PostPosted: Tue Dec 13, 2016 8:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I suppose that you are updating the list be overwriting it entirely, right?

Code:
parent.setDescriptions(newDescriptions);


That might cause your issue.

Instead, you need to:

- remove the child entities that are no longer needed
- add new child entities when necessary
- update the content of the child entities that are to remain in the DB

When you add/remove child entities, you have to synchronize both ends of the association as explained in this article.


Top
 Profile  
 
 Post subject: Re: Hibernate recreates the collection instead of updating it
PostPosted: Tue Dec 13, 2016 10:13 am 
Newbie

Joined: Tue Dec 13, 2016 4:52 am
Posts: 7
Hello again,

Thank you for your answer but I think that I am doing the correct process:

Code:
BodyResult bodyToUpdate = _service.getBodyByUid(bodyUid);
bodyToUpdate.setOrder(-111L);
bodyToUpdate.getDescriptions().get(0).setLatinFullName("HELLO WORLD!");


As you can see, I only modify the 1st item of the sub-objects collection.

How is possible Hibernate interprets the sub-items are new if they arrive to the method to persist with their IDs?

Thank you.


Top
 Profile  
 
 Post subject: Re: Hibernate recreates the collection instead of updating it
PostPosted: Tue Dec 13, 2016 10:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Can you add the SQL queries that Hibernate executes?


Top
 Profile  
 
 Post subject: Re: Hibernate recreates the collection instead of updating it
PostPosted: Tue Dec 13, 2016 11:01 am 
Newbie

Joined: Tue Dec 13, 2016 4:52 am
Posts: 7
Hello,

1 - First of all, the query (simplified as it is quite big) to retrieve the object (parent and children) to be modified:

Code:
select
        body0_.BODY_UID as BODY1_7_1_,
        body0_.BODY_MEP_CODE as BODY2_7_1_,
        ............................................
        descriptio1_.BODY_UID as BODY5_7_3_,
        descriptio1_.BODY_DESC_UID as BODY1_3_,
        descriptio1_.BODY_DESC_UID as BODY1_8_0_,
        descriptio1_.BODY_UPDATE_DATE as BODY2_8_0_,
        descriptio1_.BODY_UPDATE_USER as BODY3_8_0_,
        .............................................
    from
        T_BODY body0_,
        T_BODY_DESC descriptio1_
    where
        body0_.BODY_UID=descriptio1_.BODY_UID(+)
        and body0_.BODY_UID=?


(*) I've just paid attention in one thing: Why it queries twice the same column: descriptio1_.BODY_DESC_UID?


2 - The instructions to update the DB:

Code:
select
         SEQ_BODY_DESC_UID.nextval
    from
        dual
      
Hibernate:
    insert into T_BODY_DESC
        (BODY_UPDATE_DATE, BODY_UPDATE_USER, BODY_LANG_ISO_CODE, BODY_UID, BODY_SHORT_NAME, BODY_FULL_NAME, BODY_SEARCH_NAME, BODY_MNEMO_CODE, BODY_CODE, BODY_LATIN_CODE, BODY_LATIN_SHORT_NAME, BODY_LATIN_FULL_NAME, BODY_LATIN_SEARCH_NAME, BODY_LONG_NAME, BODY_DESC_UID)
    values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)


You can see it tries to execute an "insert" instead of an update, and in that moment the exception by constraint violated is thrown as two field that must be different for each row are repeated:

Code:
ORA-00001: unique constraint (BODY_DESC2_PK) violated


Top
 Profile  
 
 Post subject: Re: Hibernate recreates the collection instead of updating it
PostPosted: Tue Dec 13, 2016 12:31 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Maybe you're using an old version of Hibernate, or maybe there's something else down the stack that you're doing that's causing this issue.

Try running this example from our documentation and you'll see that it's working as expected.

Try debugging it and see what's causing the EntityInsertAction to be triggered. It's difficult to say without seeing the code.


Top
 Profile  
 
 Post subject: Re: Hibernate recreates the collection instead of updating it
PostPosted: Tue Dec 13, 2016 12:40 pm 
Newbie

Joined: Tue Dec 13, 2016 4:52 am
Posts: 7
Yes, I know it should work, as I did it for another tables.

I do not understand how it tries to insert if the id field is populated in the sub-entities.


Top
 Profile  
 
 Post subject: Re: Hibernate recreates the collection instead of updating it
PostPosted: Tue Dec 13, 2016 6:52 pm 
Newbie

Joined: Tue Dec 13, 2016 4:52 am
Posts: 7
Hello,

I stil have not be able to solve the problem and this is breaking our planning.

Any idea?


Thank you very much


Top
 Profile  
 
 Post subject: Re: Hibernate recreates the collection instead of updating it
PostPosted: Wed Dec 14, 2016 2:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I have no idea. Send us a replicating test case that demonstrates this behavior since I haven't ever seen it.


Top
 Profile  
 
 Post subject: Re: Hibernate recreates the collection instead of updating it
PostPosted: Thu Dec 15, 2016 1:01 pm 
Newbie

Joined: Tue Dec 13, 2016 4:52 am
Posts: 7
Hello,

The problem was caused by this property I had mapped:

Code:
<timestamp name="updateDate" access="field" column="BODY_UPDATE_DATE" />



Thank you for your help.


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