|
Hi,
I have the following problem: I have a many-to-many relationship between Users and Applications.
I create an application. Get the user.getApplications(). add the appliction to the list. call user.setApplications(). And merge the user. This works great, the application is stored in the database and also in the relation-table (user_applications).
Now when i do a entityManager.find(), i get the user object, when i do a merge on the user object, the relation with the applications is lost. in my logs i also see a delete statement (Hibernate: delete from user_applications where user_id=?). This is definitely not the behaviour i want ;( I cannot see what i am doing wrong, do i need to pre-load the application list?
Anybody who can help me figuring out what the problem could be?
the necessary code: User class @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE}) @JoinTable(name="user_applications", joinColumns= @JoinColumn(name="user_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="application_id", referencedColumnName="id")) private List<Application> applications = new ArrayList<Application>();
Application class @ManyToMany(mappedBy="applications") private List<User> users;
MERGE code: public void update(User user) { entityManager.getTransaction().begin(); entityManager.merge(user); entityManager.getTransaction().commit(); }
|