gpt4 book ai didi

database - Twitter 喜欢 JPA 中的关系

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:22 24 4
gpt4 key购买 nike

我遇到了 JPA 问题。我正在尝试实现一个允许用户关注其他用户并被关注的数据库。我想我需要(总结)这样的东西:

USER_TABLE: id | userName
RELATIONSHIP_TABLE: id | follower | followed | acceptation

我有两个实体(也总结了):

@Entity
public class User implements Serializable {

@Id
private Long id;

private String userName;

@OneToMany
private Collection<Relationship> followings;

}


@Entity
public class Relationship implements Serializable {

@Id
private Long id;

private User follower;

private User followed;

private boolean accepted;

}

我的问题是我不确定是否可以这样做,因为我获得了比我需要的两个更多的表。

有人能帮帮我吗?谢谢,对不起我的英语。

最佳答案

您获得了更多表,因为您没有使关联成为双向的。 JPA 无法知道 Relationship.followerUser.followings 的另一端,如果您不告诉:

@Entity
public class User implements Serializable {

@OneToMany(mappedBy = "follower")
private Collection<Relationship> followings;

// ...
}


@Entity
public class Relationship implements Serializable {

@ManyToOne
@JoinColumn(name = "follower")
private User follower;

@ManyToOne
@JoinColumn(name = "followed")
private User followed;

// ...
}

The documentation当然会解释它是如何工作的。

关于database - Twitter 喜欢 JPA 中的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11846433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com