有 3 个实体类 - Item、Seller、Comment。在哪里,
Item 1 --- * Comment, and Seller 1 --- * Comment
如何在不添加附加表的情况下使用 JPA/Hibernate 注释映射类?
表结构为:
Item(id, description)
Seller(id, name)
Comment(id, entityType, entityKey, message)
其中 entityType 是 ITEM 或 SELLER,entityKey 是 item.id 或 Seller.id。
现在,我有以下内容:
更新:OneToMany 端现在可以了,仍然需要弄清楚如何让它在 ManyToOne 端工作。
@Entity
@Table(name = "item_tb")
public class Item {
@Id
@GeneratedValue
private Long id;
@Column(name = "description")
private String description;
*** @OneToMany
*** @JoinColumn(name = "entityKey")
*** @Where(clause = "entityType = 'ITEM'")
private List<Comment> comments;
}
@Entity
@Table(name = "seller_tb")
public class Seller {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String name;
*** @OneToMany
*** @JoinColumn(name = "entityKey")
*** @Where(clause = "entityType = 'SELLER'")
private List<Comment> comments;
}
@Entity
@Table(name = "comment_tb")
public class Comment {
@Id
@GeneratedValue
private Long id;
@Column(name = "entityType")
private String entityType;
@Column(name = "entityKey")
private Integer entityKey;
@Column(name = "message")
private String message;
@ManyToOne
...
private Item item;
@ManyToOne
...
private Seller seller;
}