-->
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: Duplicate childs in database when parent is merged twice
PostPosted: Fri May 30, 2008 1:30 pm 
Newbie

Joined: Wed May 14, 2008 8:36 am
Posts: 6
Hi

I am using Hibernate 3.2.6 and i have defined OneToMany mapping from Person to Car as follows.
Code:
@Entity
@Table (name = "kperson")
public class Person
{

    @Id
    @Column(name = "id")
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    Integer myId;
   
   
    @CollectionOfElements(fetch = FetchType.EAGER)
    @OneToMany(cascade = { CascadeType.ALL})
    @JoinTable(name="kperson_cars",
               joinColumns={@JoinColumn(name="person_id")},
               inverseJoinColumns={@JoinColumn(name="car_id")})
    @Cascade(value = {org.hibernate.annotations.CascadeType.SAVE_UPDATE,
                      org.hibernate.annotations.CascadeType.MERGE})
    @Fetch(FetchMode.JOIN)
    Set<Car> myCars;
}


Code:
@Entity
@Table (name = "kcar")
public class Car
{
    @Id
    @Column(name = "id")
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    Integer myId;
   
    @Column(name = "name")
    String myName;
   
    public Car()
    {
    }
   
    public Car(String name)
    {
       myName = name;
    }
   
    public boolean equals(Object o)
    {
       if (!(o instanceof Car))
          return false;
       
       Car other = (Car) o;
       return myName.equals(other.myName);
    }
   
    public int hashCode()
    {
       return myName.hashCode();
    }
}


Now on executing the below code, i am getting 2 cars with same name (Test#2) into kcar table. Can any one tell me how to fix this problem?

Code:

    public static void main(String...strings)
    {
       ArrayList<Car> carTempList = new ArrayList<Car>();
        HibernateUtil.init("....");
        Person p = HibernateDAO.findUniqueByCriteria(Person.class,
                                                                             Restrictions.idEq(1));
        for (Car c : p.myCars) {
           carTempList.add(c);
        }

        Car aCar = new Car();
        aCar.myName = "TEST#2";
        p.myCars.add(aCar);
        carTempList.add(aCar);

        Transaction t = HibernateDAO.getSession().beginTransaction();
        HibernateDAO.merge(p);
        t.commit();

        // get newly inserted car from internally maintained list.
        Car sameCar = carTempList.get(carTempList.size() - 1);
        p.myCars.remove(aCar);
        p.myCars.add(sameCar);

        t = HibernateDAO.getSession().beginTransaction();
        HibernateDAO.merge(p);
        t.commit();
    }


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 1:35 pm 
Newbie

Joined: Wed May 14, 2008 8:36 am
Posts: 6
small correction, i had CascadeType.ALL at both places in Person-> Car mapping


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 31, 2008 10:52 am 
Newbie

Joined: Wed May 14, 2008 8:36 am
Posts: 6
I'll explain the problem in brief to save time,

On calling merge first time, hibernate seems to changing java object of new car added to person. Before calling second merge, we get the same object from a list not known to hibernate, so hibernate treats this as a completely new object and adds it again to database. But since equals, hashcode are defined properly why hibernate is treating the sameCar as new object is my question. Is this expected or a bug?


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