Hi, I have the following situation:
Trip and User, a user can enroll for a trip and should be able to disenroll also. This last part is not working.
I have 2 Entities with a bidirectional ManyToMany mapping.
Code:
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cascade;
@Entity
@Table(name = "t_tripexecutionmoment")
public class TripExecutionMoment {
@Id
@GeneratedValue
private int id;
@ManyToMany(mappedBy = "enrolledExecutionMoments", cascade = { CascadeType.ALL, CascadeType.ALL}, fetch = FetchType.EAGER)
private List<User> tripExecutionMomentParticipators = new ArrayList<User>();
public TripExecutionMoment() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@JsonIgnore
public List<User> getTripExecutionMomentParticipators() {
return tripExecutionMomentParticipators;
}
public void setTripExecutionMomentParticipators(List<User> tripExecutionMomentParticipators) {
this.tripExecutionMomentParticipators = tripExecutionMomentParticipators;
}
public void addEnrollement(User user){
tripExecutionMomentParticipators.add(user);
}
public void removeEnrollement(User user){
tripExecutionMomentParticipators.remove(user);
}
}
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue
private int id;
@NotBlank(message = "err_invalid_mail")
@Email(message = "err_invalid_mail")
private String email;
@ManyToMany(cascade = { CascadeType.ALL, CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinTable(joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "tripExecutionMomentId") })
private List<TripExecutionMoment> enrolledExecutionMoments = new ArrayList<TripExecutionMoment>();
public User() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
@JsonIgnore
public TripExecutionMoment getCheckedInTripExecutionMoment() {
return checkedInTripExecutionMoment;
}
public void setCheckedInTripExecutionMoment(
TripExecutionMoment checkedInTripExecutionMoment) {
this.checkedInTripExecutionMoment = checkedInTripExecutionMoment;
}
public void addEnrollement(TripExecutionMoment tripExecutionMoment) {
enrolledExecutionMoments.add(tripExecutionMoment);
}
public void removeEnrollement(TripExecutionMoment tripExecutionMoment) {
enrolledExecutionMoments.remove(tripExecutionMoment);
}
}
This is the code for enrollement and disenrollement:
enroll works, I can see in the jointable from the database that a row with the 2 id's were added.
disenroll only works in memory (junit test) but the row is not removed from the database.
Code:
@Transactional
public void enrollParticipator(User user,
TripExecutionMoment tripExecutionMoment)
throws AlreadyBoundException {
if (tripExecutionMoment.getTripExecutionMomentParticipators().contains(
user)) {
throw new AlreadyBoundException(
"This user is already enrolled for this trip");
}
tripExecutionMoment.addEnrollement(user);
user.addEnrollement(tripExecutionMoment);
getEntityManager().merge(tripExecutionMoment);
}
@Transactional
public void disenrollParticipator(User user,
TripExecutionMoment tripExecutionMoment) {
tripExecutionMoment.removeEnrollement(user);
user.removeEnrollement(tripExecutionMoment);
getEntityManager().merge(user);
}