-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate deletes and reinserts the entire collection
PostPosted: Fri Aug 01, 2014 6:05 am 
Newbie

Joined: Fri Aug 01, 2014 5:54 am
Posts: 13
Location: Brazil
I have the following tables:
Image

The following classes:
Code:
public class Parent {
    private int id;
    private Set<Child> children;

    // getters and setters
}

public class Child {
    private int id;
    private String attr;

    // getters and setters

    public int hashCode() {
        return this.getId();
    }

    public boolean equals(Object obj) {
        return obj instanceof Child && this.getId() == ((Child)obj).getId();
    }
}

And the following Hibernate mapping:
Code:
<class name="Parent" table="parent">
    <id name="id" column="id"/>

    <set name="children" table="child">
        <key column="id_parent"/>
        <composite-element class="Child">
            <property name="id" column="id"/>
            <property name="attr" column="attr"/>
        </composite-element>
    </set>     
</class>

Whenever I change attr attribute from a Child or add/remove a Child to/from the collection and commit the transaction, the entire collection is deleted and the remaining children are reinserted with the new attribute values.
Is this because I'm using an entity (Parent) and a component class (Child) instead of using two entities in a one-to-many relationship?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.