问题描述
架构在这里:
或这里;
现在,我创建了以下类:
-
Student.java
@Entity
public class Student实现Serializable {
@Id
@GeneratedValue
private int id;
@NotNull
@Size(min = 1,max = 35)
私人字符串名称;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
-
Friend.java
@Entity
public class Friend实现Serializable {
@EmbeddedId
@NotNull
@ManyToMany(targetEntity = Student.class)
private FriendPK primaryKey;
public FriendPK getPrimaryKey(){
return primaryKey;
}
public void setPrimaryKey(FriendPK primaryKey){
this.primaryKey = primaryKey;
}
}
-
FriendPK.java
'pre>@Embeddable
公共类FriendPK实现Serializable {
@JoinColumn(名称= id_from,referencedColumnName = id)
private int idFrom;
@JoinColumn(name =id_to,referencedColumnName =id)
private int idTo;
$ b $ public FriendPK(){
}
public int getIdFrom(){
return idFrom;
}
public void setIdFrom(int idFrom){
this.idFrom = idFrom;
}
public int getIdTo(){
return idTo;
}
public void setIdTo(int idTo){
this.idTo = idTo;
code $
1)这足以模拟多对多的关系吗?更具体地说,
我是否需要Student类中的另一个List friends变量来存储这个学生的朋友? 3)同样,在Friend类中是否需要另一个List students变量来存储朋友的学生?
我隐约感到目前的设计还不够,但无法弄清楚需要什么。
谢谢。
解决方案你只需要一个持久化来实现这样的模式。 Hibernate会为
@ManyToMany
关联创建一个连接表。如果您需要指定连接表的名称,则可以使用@JoinTable
注释。
需要
Friend
类。你可以用HQL
或Criteria
来处理friends
。所以你不需要知道连接表。在将HQL
转换为SQL
时,Hibernate将为连接表添加连接。
from Student's inner join fetch s.friends where s.name =:studentName
Keep记住它是
HQL
,对于JPQL
您需要选择
@Table
@Entity
公共班学生{
@I $
@GeneratedValue
private int id;
@NotNull
@Size(min = 1,max = 35)
私人字符串名称;
@ManyToMany
私人列表< Student> friends = new ArrayList< Student>();
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
公共列表< Student> getFriends(){
返回朋友;
}
public void setFriends(List< Student> friends){
this.friends = friends;
}
}
The schema is here:http://sqlfiddle.com/#!9/5ec63/2
or here;Query from many-to-many relationship
Now, I created the following classes:
Student.java
@Entity public class Student implements Serializable { @Id @GeneratedValue private int id; @NotNull @Size(min = 1, max = 35) private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Friend.java
@Entity public class Friend implements Serializable { @EmbeddedId @NotNull @ManyToMany( targetEntity = Student.class ) private FriendPK primaryKey; public FriendPK getPrimaryKey() { return primaryKey; } public void setPrimaryKey(FriendPK primaryKey) { this.primaryKey = primaryKey; } }
FriendPK.java
@Embeddable public class FriendPK implements Serializable { @JoinColumn( name = "id_from", referencedColumnName = "id") private int idFrom; @JoinColumn( name = "id_to", referencedColumnName = "id") private int idTo; public FriendPK() { } public int getIdFrom() { return idFrom; } public void setIdFrom(int idFrom) { this.idFrom = idFrom; } public int getIdTo() { return idTo; } public void setIdTo(int idTo) { this.idTo = idTo; } }
1) Is this enough to model the many-to-many relationship? More specifically,
2) Do I need another "List friends" variable in Student class to store this student's friends?
3) Likewise, do I need another "List students" variable in Friend class to store students which are friends?
I vaguely feel the current design isn't enough, but can't figure out exactly what's needed.
Thank you.
解决方案You need only one persistent to implement such schema. Hibernate will create a join table for
@ManyToMany
association. If you need to specify name of the join table you can use a@JoinTable
annotation.You don't need the
Friend
class. You can deal withfriends
withHQL
orCriteria
. So you don't need to know about a join table. Hibernate will add joins for a join table while convertingHQL
toSQL
.from Student s inner join fetch s.friends where s.name = :studentName
Keep in mind that it is
HQL
, forJPQL
you needselect
.@Table @Entity public class Student { @Id @GeneratedValue private int id; @NotNull @Size(min = 1, max = 35) private String name; @ManyToMany private List<Student> friends = new ArrayList<Student>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getFriends() { return friends; } public void setFriends(List<Student> friends) { this.friends = friends; } }
这篇关于在JPA / Hibernate中建模多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!