Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. @Entity
  2. public class Course {
  3.  
  4. // Primary key and other fields would be here...
  5.  
  6.  
  7. @OneToMany(mappedBy = "course", fetch = FetchType.LAZY)
  8. private List<Student> students;
  9.  
  10. // Getters, setters...
  11.  
  12. }
  13.  
  14. @Entity
  15. public class Student {
  16.  
  17. // Primary key and other fields...
  18.  
  19. @ManyToOne(fetch = FetchType.EAGER)
  20. @JoinColumn(name = "course_id")
  21. private Course course;
  22.  
  23. // Getters and setters...
  24.  
  25. }
  26.  
  27. Course course;
  28. // Fetch the course from persistence
  29. Student student = new Student("John Doe");
  30. student.setCourse(course);
  31. course.getStudents().add(student);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement