如何使用Hibernate定义友谊关系? [英] How to define Friendship relationship using Hibernate?

查看:80
本文介绍了如何使用Hibernate定义友谊关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有FriendRequest和ListOfFriends功能。类似于Facebook,显示收到的朋友请求的数量和获得批准的朋友的数量。



FriendRequest,我的意思是有一个用户收到的好友请求列表。



通过ListOfFriends,我的意思是有一个用户的朋友列表。为了达到这个目的,我定义了下面的类,但是当我尝试使用它的用户名检索用户时,会抛出一个Stackoverflow异常消息。它似乎进入了无限循环。



当我从 toString 方法中删除FriendRequests和Friends时,它会停止抛出异常。



这个问题与我将要实现的类似,但区别在于我没有单独的Credential类。

Member

  @Entity 
public class Member实现Serializable {
@Id
@GeneratedValue
私人长ID;
@Column(name =username,nullable = false,unique = true)
private String username;
@Column(nullable = false)
private String fname;
@Column(nullable = false)
private String lname;
@OneToMany(fetch = FetchType.EAGER,mappedBy =请求者)
私人设置< Friendship> friendRequests = new HashSet< Friendship>();
@OneToMany(fetch = FetchType.EAGER,mappedBy =friend)
private设置< Friendship> friends = new HashSet< Friendship>();

获取者和设置者

@Override
public String toString(){
returnMember [
.....
,fname =+ fname +,lname =+ lname
// + friendRequests.size()+,friends =+ friends.size()+
];


友谊

  @Entity 
public class友谊实现Serializable {
@Id
@ManyToOne
@ JoinColumn(referencedColumnName =username)
成员请求者;
@Id
@ManyToOne
@JoinColumn(referencedColumnName =username)
会员朋友;
@Temporal(javax.persistence.TemporalType.DATE)
日期日期;
@Column(nullable = false)
boolean active;


解决方案

我想推荐DB Design。



现在,根据POJO课程,您将拥有友情表和朋友表。
而不是那个,你可以只有友谊表多一列boolean isAccepted;(POJO类中的变量)。

如果布尔值为true,这意味着该成员(朋友)是朋友。
当你想得到所有的朋友,检索友谊与isAccepted行设置为true;
如果您只想获取friendrequests(或者待处理的请求被批准),则将所有的isAccepted设置为false。



使用目前的DB设计或ER模型,您正在使用。您需要从友谊表中删除该行(因为它不再是朋友请求),如果该人接受好友请求并存储在某处。你在哪里储存朋友。
根据您现在的设计,它存储在朋友表中。但没有专栏表示该行表示某人的朋友。这就好像每次一个成员接受其他成员的请求并仅创建冗余行时向朋友表中添加一行(朋友)。


I need to have "FriendRequest" and "ListOfFriends" functionalities.Similar to facebook, which shows number of received friend requests and number of approved friends.

By "FriendRequest", I mean to have a list of friend requests that the user receives.

By "ListOfFriends", I mean to have a list of friends of the user.

To achieve that, I defined following class but when I try to retrieve a user using its username, a "Stackoverflow" exception message will be thrown. It seems it goes into an indefinite loop.

When I remove "FriendRequests" and "Friends" from my toString method it stops throwing the exception.

This question is similar to what I am going to achieve but the difference is I do not what to have a separate Credential class.

Member

@Entity
public class Member implements Serializable {
    @Id
    @GeneratedValue
    private long id;
    @Column(name = "username", nullable = false, unique = true)
    private String username;
    @Column(nullable = false)
    private String fname;
    @Column(nullable = false)
    private String lname;
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "requester")
    private Set<Friendship> friendRequests = new HashSet<Friendship>();
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "friend")
    private Set<Friendship> friends = new HashSet<Friendship>();

    getters and setters

    @Override
    public String toString() {
       return "Member [
                .....                 
                , fname=" + fname + ", lname=" + lname
                //  + friendRequests.size() + ", friends=" + friends.size() + 
       "]";
    }
}

Friendship

@Entity
public class Friendship implements Serializable {
    @Id
    @ManyToOne
    @JoinColumn(referencedColumnName = "username")
    Member requester;
    @Id
    @ManyToOne
    @JoinColumn(referencedColumnName = "username")
    Member friend;
    @Temporal(javax.persistence.TemporalType.DATE)
    Date date;
    @Column(nullable = false)
    boolean active;

解决方案

I would like to suggest DB Design.

Right now, You are having a friendship table and friends table as per POJO classes. Instead of that, you can just only have friendship table with one more column boolean isAccepted;(variable in POJO class).

If boolean value is true, It means that that member(friend) is a friend. When you want to get all the friends, retrieve the rows of friendship with isAccepted set to true; If you want to get only friendrequests(Or pending requests to be Approved), get all the rows with isAccepted set to false.

With the present DB design or ER model, you are using. You need to delete the row from the friendship table (as It is no more a friend request), If the person accepts the friend request and store somewhere. Where do you store the friends . According to your present design, It stores in the friends table. But there is no column saying that the row indicates a friend of someone. It is just like adding a row(friend) to the friends table every time one member accepts the request of other member and just making redundant rows.

这篇关于如何使用Hibernate定义友谊关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆