简体   繁体   中英

JPA: OneToMany relationship keeps the Collection empty

It seems I am having a difficult time understanding JPA and how the OneToMany relationship actually works.

For example, imagine I have an object Class

@Entity
public class Class {
    @Id
    private String className;

    @OneToMany(cascade = Cascade.ALL, orphanRemoval = true)
    private Set<Student> students;

    // Constructors, Getters, Setter
}

I also have an object Student where it holds Class.

@Entity
public class Student {
    @Id
    private String studentName;

    @ManyToOne
    private Class class;

    // Constructors, Getters, Setter
}

Obviously, a student can have multiple classes but forget about that.

Why is that when I build a class and then build a couple students using that class, findAll() on the ClassRepository returns me an empty set of students.

Class class = new Class("CS", new HashSet<>());
classRepository.save(class); // repository has no special functions

Student student1 = new Student("1", class);
Student student2 = new Student("2", class);

studentRepository.save(student1);
studentRepository.save(student2);

classRepository.findAll() // Returns me a List<Class> with only one class object that has an empty set.

I was thinking the above code should automatically see that the two students are from that one class and so when I call buildingRepository.findAll() , it will return a Class object with the students set populated properly.

Is my understanding wrong then? Or is my code wrong? And how can I change it up to fix it?

You can choose:

1. Unidirectional @OneToMany :

@Entity
public class Class {
    @Id
    private String className;

    @OneToMany(cascade = Cascade.ALL, orphanRemoval = true)
    private List<Student> students=new ArrayList<>();

    // Constructors, Getters, Setter
}

@Entity
public class Student {
    @Id
    private String studentName;

    // Constructors, Getters, Setter
}

Now, if we persist one Class :

Class class1=new Class("name1");
class1.getStudents().add(new Student("student1Name"));
// then you can make a save of class1 in db
classRepository.save(class);

2. Unidirectional @OneToMany with @JoinColumn :

To fix the aforementioned extra join table issue, we just need to add the @JoinColumn in the mix:

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "class_id")
private List<Student> students = new ArrayList<>();

3. Bidirectional @OneToMany :

The best way to map a @OneToMany association is to rely on the @ManyToOne side to propagate all entity state changes:

@Entity
public class Class {
    @Id
    private String className;

    @OneToMany(
        mappedBy = "class",
        cascade = CascadeType.ALL,
        orphanRemoval = true
    )
    private List<Student> students=new ArrayList<>();

    // Constructors, Getters, Setter
    public void addStudent(Student student) {
        students.add(student);
        student.setClass(this);
    }

    public void removeStudent(Student student) {
        students.remove(student);
        student.setClass(null);
    }
}


@Entity
public class Student {
    @Id
    private String studentName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "class_id")
    private Class class;

}

And to persist:

Class c1=new Class("className1");
c1.addStudent(new Student("StudentNAme1"));
c1.addStudent(new Student("StudentNAme2"));
c1.addStudent(new Student("StudentNAme3"));
classRepository.save(c1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM