lundi 3 août 2015

JPA one to many relation

I have simple OneToMany relation in my web app. I use EclipseLink 2.5.1 and PostgreSQL. My transactions are managed by glassfish 4.1.

public class Student

...

@ManyToOne
@JoinColumn(name = "school_id")
private School school;

sa

public class School

...

@OneToMany(mappedBy = "school", cascade = CascadeType.ALL)
private List<Student> students;

Save action :

public void process( {
    School school = baseManager.findSchoolById(1);
    Student student = new Student();
    student.setSchool(school);
    baseManager.createStudent(student);

    //to check operation result
    School school = baseManager.findSchoolById(1);  //school does not own previously saved student, why?
)

baseManager is an EJB stateless bean. findSchoolById(int id) find school via trivial NamedQuery. createStudent(Student s) only persist new student. Even I refresh my page I don't have any students in the schhol with id = 1. baseManager is located in CDI ViewScoped bean. Student are saved to database correctly. When I re-deploy my application previously saved data are correctly loaded to my application. What do I do wrong?

Aucun commentaire:

Enregistrer un commentaire