-->
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.  [ 4 posts ] 
Author Message
 Post subject: Removing an object from a collection (many-to-many)
PostPosted: Sat May 03, 2008 6:32 pm 
Newbie

Joined: Sat May 03, 2008 5:57 pm
Posts: 4
I have two classes: User and Photo and a unidirectional many-to-many relationship between then:

@Entity
@Table(name="USER")
public class User {
private Collection<Photo> favouritePhotos = new ArrayList<Photo>();
...
@ManyToMany(cascade={ CascadeType.ALL})
@JoinTable(name = "FAVOURITE_PHOTOS",
joinColumns = @JoinColumn(name = "USER_ID"),
inverseJoinColumns = @JoinColumn(name = "PHOTO_ID"))
public Collection<Photo> getFavouritePhotos() {
return favouritePhotos;
}
}

@Entity
@Table(name="PHOTO")
public class Photo {

}

In my spring service class I am trying to remove a photo from the collection in the User object:

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void removeFavouritePhoto(User user, Photo photo) {
user = userDao.findUserById(user.getId());
user.getFavouritePhotos().remove(photo);
}

I was expecting hibernate to remove a row from the FAVOURITE_PHOTOS table but nothing happens. I'm pretty confident that the transaction semantics are set up correctly because other methods in my service class work as expected so i'm guessing that my mapping is wrong.

Could anyone point me in the right direction?

Thanks


Top
 Profile  
 
 Post subject: question
PostPosted: Sun May 04, 2008 4:09 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Where's the call to actually merge your change into the db? i.e. entityManager.merge(someuser).


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 04, 2008 8:51 am 
Newbie

Joined: Sat May 03, 2008 5:57 pm
Posts: 4
Fixed it! The user object was attached to the session but the photo object was not so hibernate didn't recognize the change. I've now changed it so both objects are attached and it works a dream :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 04, 2008 3:22 pm 
Newbie

Joined: Mon Apr 28, 2008 4:14 pm
Posts: 5
Hi,

What do you mean by "attaching photo object with session"? Isn't it attached by default?

Cheers!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.