English 中文(简体)
Hibernate @OneToMany relationshipmap
原标题:Hibernate @OneToMany relationship mapping

I am trying to design some kind of user to user relationship, such as "user A follows user B" and "User A wants to be User B s friend".

我有一个用户类别,设计方式就是这样:

@Entity
public class User{
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    List<User> followers;
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    List<User> following;
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    List<User> friendRequests;
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    List<User> requesting;
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    List<User> friends;

}

我有两个问题:

  • Hibernate is giving me cannot simultaneously fetch multiple bags problem
  • I have looked up online, people said to remove FetchType.EAGER or change it to Set instead of List, but that resulted me Field doesn t have a default value

我感觉到,关系没有适当界定,我也应当看到更多的表格,因为现在我只看到用户表和用户——用户表格。


Update

The following creates 3 table, friends, followers, and requesters. Is this somewhat optimized compared to 5 tables? And are there any advantage to this in comparison with what Mr.J4mes suggested?

@ManyToMany
@JoinTable(name = "followers", joinColumns = @JoinColumn(name = "followerId"), inverseJoinColumns = @JoinColumn(name = "userId"))
private List<User> followers;
@ManyToMany
@JoinTable(name = "followers", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "followerId"))
private List<User> following;
@ManyToMany
@JoinTable(name = "friends", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "friendId"))
private List<User> friends;
@ManyToMany
@JoinTable(name = "requesters", joinColumns = @JoinColumn(name = "requesterId"), inverseJoinColumns = @JoinColumn(name = "userId"))
private List<User> friendRequests;
@ManyToMany
@JoinTable(name = "requesters", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "requesterId"))
private List<User> requesting;
最佳回答

首先,为了执行你的特点,你应使用<条码>@Many ToMany而不是<条码>。 1 ToMany。 它希望:

@Entity
public class User implements Serializable {
   @ManyToMany(mappedBy="followers")
   @JoinTable(name="followingTable")
   private Set<User> following;
   @ManyToMany
   @JoinTable(name="followerTable")
   private Set<User> followers;
   @ManyToMany(mappedBy="friendRequests")
   @JoinTable(name="requestingTable")
   private Set<User> requesting;
   @ManyToMany
   @JoinTable(name="friendRequestTable")
   private Set<User> friendRequests;
   @ManyToMany
   private Set<User> friends;
}

Your relationships looks like bidirectional ones to me. If you use @OneToMany, it means that C has 2 followers A and B = A and B only follows C. However, the fact is that one person can follows many people and one person can be followed by many people. In other words, A and B can also follow D.

Besides, you shouldn t use cascadeType.ALL at all. That cascade policy means that if one user deletes his account and you delete the corresponding entry in the database, all of his friends, etc. will also be deleted.

问题回答

排他性只能为用户实体生成一个表格,而Im为用户与用户关系提供一个交叉参考表。 努力编制不同的表格可能会为不同关系(即不同关系)设立不同的实体。

User Entity

@OneToMany
List<Follower> followers;

后续实体

@Entity
class Follower
...
@ManyToOne
User user;

@Id
@Generated
int id;

关于欧研组的称呼,你可能实际上想让所有人成为LAZY,因为根据贵国数据库的建立情况,所有热心负荷都可能非常昂贵。 只有当用户想要装上用户追随者时,我才希望打他们。

<代码>User自Friends 尝试用至少2栏的表格标明<代码>。 用户_ID至。 用户:ID

。 它意味着要装上所有用户及其朋友。 亲爱的朋友应为LAZY。 i. 只有在需要时才装满。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签