所以,情况如下:
表ComputerInventory
,其中包含{computerInventoryID(主键)、TagID(唯一)、名称等}
表reviewStatus
,其中包含{reviewStatusID(主键)、computerInventoryID (ForeignKey)、status }
我已经为 ReviewStatus
编写了 hibernate 实体:
public class ReviewStatus implements Serializable {
public enum reviewStatus {
To_Be_Reviewed,
Reviewed
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long reviewStatusId;
@Column(name = "lastModifiedTime")
private Date lastModifiedTime;
@Column(name = "Comments")
private String comments;
@Enumerated(EnumType.STRING)
@Column(name = "status")
//all above params have gettetrs and setters
private reviewStatus status;
//TODO: Mapping to computerinventory
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "computerInventoryId")
//For one ComputerInventoryID, there can be many review Statuses.
private Set<ReviewStatus> reviewStatusSet;
public long getreviewStatusId() {
return reviewStatusId;
}
我的疑问: 对于一个ComputerInventoryID,可以有多个review Statuses,所以我有一个
Set<ReviewStatus> reviewStatusSet
在哪里返回 reviewstatus 中的条目列表?抱歉,但我不明白如何编写 get/set 来返回和设置一堆记录的评论状态。
最佳答案
ReviewStatus 中的引用应该是 ComputerInventory,而不是其 ID。 Hibernate 允许您抽象出主键(ID)的详细信息,从而使您可以直接从一个对象引用到另一个对象。您应该在您的 private ComputerInventory computerInventory;
上使用 @ManyToOne
注释。
关于java - 多对一 Hibernate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24742267/