jeudi 26 février 2015

JPA Many to many association entity



I am developing Java EE project with embedded database using JPA ORM. My question is...


When I create @ManyToMany fields in two Entities, I have to create association entity too, or it will do container for me?


Course entity snippet



Public class Course implements Serializable {
...
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;

@ManyToMany
@JoinTable(name="course_user", joinColumns={@JoinColumn(name="course_id", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="user_id", referencedColumnName="id")})
private List<User> enrolledStudents;
...


User Entity snippet



public class User implements Serializable {
private static final long serialVersionUID = 1L;
...

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;

@ManyToMany(mappedBy="enrolledStudents")
private List<Course> enrolledCourses;
...


So is this everything I need or not? Thank you for answers!




Aucun commentaire:

Enregistrer un commentaire