具有复合键的 Hibernate 单向 OneToMany
Hibernate Unidirectional OneToMany with composite key
我想构建 Hibernate OneToMany 关系,其中
Parent 有一个复合主键,Child 有一个主键(hibernate-auto 生成)。下面是我的工作示例代码:
class Parent{
@EmbeddedId
private ParentPk parentPk;
@OneToMany( mappedBy="parent")
private List<ChildType1>;
@OneToMany( mappedBy="parent")
private List<ChildType2>;
@OneToMany( mappedBy="parent")
private List<ChildType3>;
//--setters and getters
}
@Embeddable
public class ParentPk {
private Long parentId;
private BigDecimal version;
//..setters and getters
}
class ChildType1{
@Id
private Long childId;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumn(name = "parentId"),
@JoinColumn(name = "version") })
private Parent parent;
//..other fields and setters and getters
}
//--ChildType2 and ChildType3 like above
但现在我想将上面的模型建模为 OneToMany 单向关系,即子项不应引用父项(希望在子项中省略 Parent 实例 class)。可能吗?
示例方法:
@Entity
class Parent {
@EmbeddedId
private ParentPk parentPk;
@OneToMany
@JoinColumns({
@JoinColumn(name = "parentId", referencedColumnName = "parentId"),
@JoinColumn(name = "version", referencedColumnName = "version")
})
private List<ChildType1> children1;
// exactly the same annotations as for children1
private List<ChildType2> children2;
// exactly the same annotations as for children1
private List<ChildType3> children3;
//..other fields and setters and getters
}
@Entity
class ChildType1 {
@Id
private Long childId;
//..other fields and setters and getters
}
//--ChildType2 and ChildType3 like above
我想构建 Hibernate OneToMany 关系,其中 Parent 有一个复合主键,Child 有一个主键(hibernate-auto 生成)。下面是我的工作示例代码:
class Parent{
@EmbeddedId
private ParentPk parentPk;
@OneToMany( mappedBy="parent")
private List<ChildType1>;
@OneToMany( mappedBy="parent")
private List<ChildType2>;
@OneToMany( mappedBy="parent")
private List<ChildType3>;
//--setters and getters
}
@Embeddable
public class ParentPk {
private Long parentId;
private BigDecimal version;
//..setters and getters
}
class ChildType1{
@Id
private Long childId;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumn(name = "parentId"),
@JoinColumn(name = "version") })
private Parent parent;
//..other fields and setters and getters
}
//--ChildType2 and ChildType3 like above
但现在我想将上面的模型建模为 OneToMany 单向关系,即子项不应引用父项(希望在子项中省略 Parent 实例 class)。可能吗?
示例方法:
@Entity
class Parent {
@EmbeddedId
private ParentPk parentPk;
@OneToMany
@JoinColumns({
@JoinColumn(name = "parentId", referencedColumnName = "parentId"),
@JoinColumn(name = "version", referencedColumnName = "version")
})
private List<ChildType1> children1;
// exactly the same annotations as for children1
private List<ChildType2> children2;
// exactly the same annotations as for children1
private List<ChildType3> children3;
//..other fields and setters and getters
}
@Entity
class ChildType1 {
@Id
private Long childId;
//..other fields and setters and getters
}
//--ChildType2 and ChildType3 like above