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
|