多对一的Hibernate映射

时间:2014-07-14 17:26:10

标签: java hibernate

所以,在这种情况下:

ComputerInventory{computerInventoryID (Primary key), TagID(unique), Name etc}

reviewStatus{reviewStatusID(Primary key), 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,可以有许多评论状态,所以我有一个

Set<ReviewStatus> reviewStatusSet

我在reviewstatus中返回条目列表?对不起,但我不明白如何写一个get / set来返回并设置一堆记录的评论状态。

1 个答案:

答案 0 :(得分:1)

您对ReviewStatus的引用应该是ComputerInventory,而不是它的ID。 Hibernate允许您抽象出主键(ID)的细节,让您直接从一个对象引用到另一个对象。您应该在@ManyToOne上使用private ComputerInventory computerInventory;注释。