我为表 HOBBY 和 STUDENT 创建了两个实体,但是我很难从联接表中检索我需要的信息。
这是架构:
STUDENT STUDENT_HOBBY HOBBY
-------- ----------- --------
id ------------------ student_id
name hobby_id ---------- id
lastname hobby_name
--------- ---------- --------
where
Student to student_hobby = one to many
Hobby to student_hobby = one to many
(that means a student may have many hobbies and a hobby can belong to more than one student)
这是学生实体类:
@Entity
@Table(name="STUDENT")
public class Student {
@OneToMany
@JoinTable(name="student_hobbies", joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="hobby_id"))
private Collection<Hobby> hobbies;
@Id
@Column(name="ID")
@GeneratedValue
private Integer id;
@Column(name="NAME")
private String name;
@Column(name="LASTNAME")
private String lastName;
// Getters and setters here
}
这是 Hobby 实体类:
@Entity
@Table(name="HOBBY")
public class Hobby {
@OneToMany
@JoinTable(name="student_hobbies", joinColumns=@JoinColumn(name="hobby_id"),
inverseJoinColumns=@JoinColumn(name="student_id"))
private Collection<Student> students;
@Id
@Column(name="ID")
@GeneratedValue
private Integer id;
@Column(name="HOBBY_NAME")
private String hobby_name;
// Getters and setters here
}
现在我想实现以下DAO,但我不知道具体该怎么做:
public interface MyDAO {
public void addHobbyForStudent(int student_id, int hobby_id);
public void RemoveHobbyForStudent(int student_id, int hobby_id);
}
我应该为联接表创建其他实体类吗?有人可以给我一些指示吗?