java - 多对一 Hibernate 映射

标签 java hibernate

所以,情况如下:

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/

相关文章:

java - 是否可以在一个事务中使用多个持久化单元,而不是 XA?

java - 从 Subversion 主干在 GAE 中自动部署

java - 根据设置为 jLabel 的图标指定 jTextField 中所需的文本?

java - 使用 URL 键/值样式参数的 Hibernate 查询限制

oracle - ORA-12519,TNS :no appropriate service handler found

java - 使用 hibernate 在 hsqldb 中选择更新

java - 无法配置 Spring Batch 以因此运行作业

java - 使用 BufferedWriter 和 Loop 保存 2D JTextField 数组中的文本

java - 更改 Active Directory 的邮件属性

java - 创建类路径资源 [jpaContext.xml] 中定义的名称为 'entityManagerFactory' 的 bean 时出错