-->
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 delete JoinTable Row
PostPosted: Fri Mar 01, 2013 11:39 am 
Newbie

Joined: Fri Mar 01, 2013 11:32 am
Posts: 1
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);
}


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