gpt4 book ai didi

Java-EE-6 : How to store a boolean in a @ManyToMany and @ManyToOne relationships?

转载 作者:行者123 更新时间:2023-12-02 07:14:28 25 4
gpt4 key购买 nike

我正在研究一个基本上由用户和文档组成的数据模型。现在,每次添加新文档时,只要特定用户没有查看该文档(例如单击它),就应该有一个标志将文档标识为“看不见”。

您将如何模拟这样的场景?有没有办法将 boolean 值/标志附加到用户和文档之间的关系?

这是我的简化模型:

@Entity
@Table(name="User")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(length = 128)
private String name;
@ManyToMany(mappedBy = "users", fetch=FetchType.LAZY)
private List<Document> documents = new ArrayList<Document>();

// getters and setters ...

}

这是文档类:

@Entity
@Table(name = "Document")
public class Document {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name = "Inbox", joinColumns = @JoinColumn(name = "document_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
protected List<User> users = new ArrayList<User>();

// getters and setters ...

}

非常感谢您的帮助!

最佳答案

在 JB Nizet 的回答中,Matt 提到连接表有一个唯一的主键 (id)、两个外键的唯一约束和一个附加列。这是一个很好的方法。

但是当您使用@ManyToMany时,您很可能拥有一个由连接表中的两个外键组成的主键。如果您想保留该模式并添加额外的 viewed 列,您很可能需要习惯使用 @Embeddable 类来拥有复合主类 key 反射(reflect)在您的 @Entity 中。请记住,当您这样做时,您将需要覆盖equals/hashcode

您可以看到my answer on the other question作为创建 @Entity 类的起点。在答案中描述了连接表中的实体及其主键。

关于Java-EE-6 : How to store a boolean in a @ManyToMany and @ManyToOne relationships?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15095675/

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