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: @ManyToMany loss of relation on update
PostPosted: Tue Jan 19, 2010 5:24 pm 
Newbie

Joined: Tue Jan 19, 2010 5:10 pm
Posts: 2
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();
}


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