使用 JPA 2.0 的 Self ManyToMany 和附加列

发布于 2024-11-08 01:25:29 字数 1251 浏览 0 评论 0原文

我想在用户模型之间创建一种“友谊”关系。我还需要为每一个友谊增加一个专栏。我知道我需要使用带有复合主键的连接类。这是我的 User 类,

public class User implements Serializable {

@Id
@Basic(optional = false)
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

@OneToMany(mappedBy="friendA")
private Set<Friendship> friends;

我也有 FriendshipId 类

@Embeddable
public class FriendshipId implements Serializable {
private long friendAId; 
private long friendBId;
}

,最后是 Friendship 类,

@Entity
@IdClass(FriendshipId.class)
public class Friendship implements Serializable {


private Integer friendAId;

private Integer friendBId;

@Basic(optional = false)
@Column(name="date_added")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime date_added;


@ManyToOne
@PrimaryKeyJoinColumn(name="friendAId", referencedColumnName="friendAId")
private User friendA;

@ManyToOne
@PrimaryKeyJoinColumn(name="friendBId", referencedColumnName="friendBId")
private User friendB;
}

这似乎可行,但我的数据库中生成的数据库表包含每个 ID 两次 - 我猜一组用于主键,第二组用于外键。

我怎样才能让这个PK变得独特——如果有B对A的友谊,A对B的友谊就会被拒绝?

另一个问题是关于设计的——这是实现我想要实现的目标的好方法吗?我的意思是使用复合密钥而不是简单地使用自己的 pk 来建立友谊。

I want to create kind of "Friendship" relation between Users models. I also need an additional columns for every friendship. I know I need to use joining class with composite primary key. Thats my User class

public class User implements Serializable {

@Id
@Basic(optional = false)
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

@OneToMany(mappedBy="friendA")
private Set<Friendship> friends;

I also have FriendshipId class

@Embeddable
public class FriendshipId implements Serializable {
private long friendAId; 
private long friendBId;
}

And finally Friendship class

@Entity
@IdClass(FriendshipId.class)
public class Friendship implements Serializable {


private Integer friendAId;

private Integer friendBId;

@Basic(optional = false)
@Column(name="date_added")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime date_added;


@ManyToOne
@PrimaryKeyJoinColumn(name="friendAId", referencedColumnName="friendAId")
private User friendA;

@ManyToOne
@PrimaryKeyJoinColumn(name="friendBId", referencedColumnName="friendBId")
private User friendB;
}

This seems to work but database table generated in my database consists every ID twice - I guess one set is for primary key and second for foreign keys.

How can I make this PK unique - that Friendship of A to B will be rejected if there is B to A one?

Another question is about a design - is this a good way to achieve what I want to achieve? I mean using composite key instead of simply own pk for Friendship.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灰色世界里的红玫瑰 2024-11-15 01:25:29

我想在“友谊”实体中,您添加了“用户”实体两次(非常好),但为什么

private Integer friendAId;
private Integer friendBId;

又在那里?这可能是导致 ID 生成两次的原因。

I guess in Friendship entity you added User entity twice (very fine) but why

private Integer friendAId;
private Integer friendBId;

again there? That could be cause of ID getting generated twice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文