两个类之间的JPA关系

时间:2016-05-17 11:02:13

标签: hibernate jpa many-to-one mappedsuperclass

我有一项任务是构建一个包含标题和行的文档。 例如,包含标题(日期,库存)和行(物料,数量,价格,总和)的库存收入凭证。我的问题是我不确定我的班级架构是否正确。代码在这里(JPA + Hibernate):

@Entity
@Table
public class Document extends BaseEntity {
@Column
@Temporal(TemporalType.DATE) 
private Date date;
@Column
@Temporal(TemporalType.DATE) 
private Date createDate;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DocumentRow> rows;
...
}

public class DocumentRow extends BaseEntity {
@ManyToOne(optional = false, cascade = CascadeType.ALL)
private Document document;
@Column(nullable = false, unique = true)
private Integer row;
...
}

@MappedSuperclass
public abstract class BaseEntity implements Serializable {

private static final long serialVersionUID = 8171308450531596347L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; ...}

1 个答案:

答案 0 :(得分:1)

You are doing a bidirectional mapping and you can't combine @ManyToOne and @ManyToMany, despite the logic you are applying, the sql isn't correct because :

@ManyToMany will result in an association Table ManyToOne just a foreign key to the parent entity on the other entity.

If a DocumentRow element can be shared between two different Document then you need a ManyToMany relationship which will result in creating a joinTable and you have to change the @ManyToOne on DocumentRow Table if you want a bidirectional relationship :

@ManyToMany
private List<Document> documents;

If it is not the case and a DocumentRow belongs to just one document, then you need to change the mapping on the parent class which is Document from @ManyToMany to @OneToMany this way :

@Entity
@Table
public class Document extends BaseEntity {
@Column
@Temporal(TemporalType.DATE) 
private Date date;
@Column
@Temporal(TemporalType.DATE) 
private Date createDate;
@OneToMany(mappedBy="document",...)
private Set<DocumentRow> rows;
...
}