2

I use Hibernate as orm solution for my project and i don't understand what's happening. I have a entity user :

@Entity
@Table(name="users")
public class User {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
...

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "users_projects", joinColumns = {
            @JoinColumn(name = "userid") 
            },
            inverseJoinColumns = {
            @JoinColumn(name = "projectid") 
            })
    private List<Project> projects;
... +getters and setters
}

I have the project entity. I am surprised about what happening : If a user have some projects and he want to add another using user interface. Hibernate delete first all projects in database for the current user. Then insert all project in database. Why hibernate delete all projects and insert them after instead of adding just the new one. Here is the Hibernate log :

Hibernate: select this_.code as code1_1_0_, this_.name as name2_1_0_ from country this_
Hibernate: insert into projects (besoins, countryCode, description, title) values (?, ?, ?, ?)
Hibernate: select last_insert_id()
Hibernate: update users set activated=?, email=?, lastError=?, password=?, profil_id =?, role=? where id=?
Hibernate: update profil set countryCode=?, countryName=?, description=?, firstname=?, lastName=?, phone=?, title=? where id=?
Hibernate: update projects set besoins=?, countryCode=?, description=?, title=? where id=?
Hibernate: update projects set besoins=?, countryCode=?, description=?, title=? where id=?
Hibernate: update projects set besoins=?, countryCode=?, description=?, title=? where id=?
Hibernate: update projects set besoins=?, countryCode=?, description=?, title=? where id=?
Hibernate: delete from users_projects where userid=?
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Pracede
  • 4,226
  • 16
  • 65
  • 110
  • 1
    It seems that you are not merging properly. You have any deletes from the child collection ? Also, I suggest you to have proper `hashCode()` and `equals()` and If possible try to use `Set` instead of `List`. – RP- Feb 27 '14 at 18:29
  • Yes, it could be your merge issues. Check the users_project table before update nd after update, check if they combination values changed. also, Serialize all the entities. – Zeus Feb 27 '14 at 19:59
  • Possible duplicate of [Why does Hibernate try to delete when I try to update/insert?](http://stackoverflow.com/questions/179259/why-does-hibernate-try-to-delete-when-i-try-to-update-insert?rq=1) – Mark Rotteveel Apr 03 '14 at 10:18

1 Answers1

0

Just a supposition :

Your entity have been detached after being retrieved and merged before the update. actually hibernate haven't so tracked the change on the collection and will so manage it as a bag

Gab
  • 7,869
  • 4
  • 37
  • 68
  • happened to me when we had a setter on the Set<> that used a copy constructor caused hibernate to delete and recreate the relationships. – ybudweiser Nov 10 '22 at 14:36