0

I am new to JPA, and I'm trying to make a datatable form that allows you to press "Remove" to remove a User from a Course. The problem is that only the first (uppermost) from the datatable is clickable, and removes the course from that user. (When it's removed, the next on the list is now the uppermost and working. )

The problem is that the other links (everyone but the uppermost) are refreshing the site without URL parameters (resetting session?), and doesn't invoke the removeUserFromCourse(..) method. If anyone knows why this is happeing, please feel free to answer and point out my mistakes.

Controller (shortened) :

@Model
public class UserController{
    private UserDAO persister;
    private User user;
    // 103 is just a default value
    private int selectedID = 103;

    @Inject
    public UserController(UserDAO persister) {
        this.persister = persister;
    }

    @PostConstruct
    public void init() {
        this.user = new User();
    }

    public int getSelectedID() {
        return selectedID;
    }

    public void setSelectedID(int selectedID) {
        this.selectedID = selectedID;
    }

    public void removeUserFromCourse(int courseID){
        persister.removeFromCourse(selectedID, courseID);
        user = persister.getUser(selectedID);
    }
}

Form in edit-user.xhtml:

         <h:form>
            <h:dataTable id="mdtb" value="#{userController.getUserCourses()}" var="course"
                         styleClass=" col-lg-6">
                <h:column>
                    <h:outputText value="#{course.name} (#{course.id})" styleClass="col-lg-6"/>
                    <h:commandLink update="mdtb" value="Remove"
                                   action="#{userController.removeUserFromCourse(course.id)}">
                        <f:param name="id" value="#{userController.selectedID}"/>
                    </h:commandLink>
                </h:column>
            </h:dataTable>
        </h:form>

User JPA (shortened):

@Stateless
public class JPAUserDao implements UserDAO {
    EntityManagerFactory entityManagerFactory;
    @PersistenceContext(name = "Egentreningprosjekt")
    EntityManager entityManager;

    public JPAUserDao() {
    }

    public JPAUserDao(EntityManager entityManager){
        this.entityManager = entityManager;
    }

    @Override
    public User update(User user) {
        System.out.println("updating user " + user);
        entityManager.merge(user);
        return user;
    }

    @Override
    public void removeFromCourse(int userID, int courseID) {
        User user = getUser(userID);
        List<Course> courses = user.getCourses();
        Course courseToBeDeleted = null;

        for(Course course : courses){
            if(course.getId() == courseID){
                courseToBeDeleted = course;
            }
        }

        if(courseToBeDeleted != null){
            courses.remove(courseToBeDeleted);
            courseToBeDeleted.getUsers().remove(user);
        }

        user.setCourses(courses);
        update(user);
        entityManager.merge(courseToBeDeleted);
    }

User.java (shortened):

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Min(value = 0, message = "ID cannot be negative")
    private int id;

    @NotNull
    @Pattern(regexp = "^([A-Z|a-z|0-9](\\.|_){0,1})+[A-Z|a-z|0-9]\\@([A-Z|a-z|0-9])+((\\.){0,1}[A-Z|a-z|0-9]){2}\\.[a-z]{2,3}$")
    private String email;

    @ManyToMany(mappedBy = "users", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private List<Course> courses;

    // getters/setters
}
Idva
  • 185
  • 2
  • 12
  • Is it because you have 103 hard coded? Looks like thats causing it to always get the same user. Also, its probably due to it not being an ajax call. It is actually submitting the form. – user489041 Dec 01 '15 at 16:45
  • Possible duplicate of [Understanding process and update attributes of PrimeFaces](http://stackoverflow.com/questions/25339056/understanding-process-and-update-attributes-of-primefaces) – Mahendran Ayyarsamy Kandiar Dec 01 '15 at 17:00

1 Answers1

0

Answering this now 4 years later. This was due to 103 being hardcoded, as the comments hinted to.

Idva
  • 185
  • 2
  • 12