1)
@Entity
@Table(name = "UT_PARENT")
public class ParentObject {
private int id;
private String value;
private Set<ChildObject> children;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", precision = 19, scale = 0)
public int getId() {
return id;
}
//mappedBy refer to the property name of Child.parent
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity=ChildObject.class, mappedBy="parentObject")
public Set<ChildObject> getChildren() {
return children;
}
}
2)
@Entity
@Table(name = "UT_CHILD")
public class ChildObject {
private int id;
private String value;
private ParentObject parentObject;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", precision = 19, scale = 0)
public int getId() {
return id;
}
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity=ParentObject.class)
@JoinColumn(name = "UT_PRAENT_ID", insertable = false, updatable = false)
public ParentObject getParentObject() {
return parentObject;
}
}
分享到:
相关推荐
One2Many JPA OrderBy 与 InMemory 排序示例 订购方式 用于按顺序创建实体的卷曲通过示例 curl -iX POST ...
Hibernate提供了多种映射策略,如一对一(One-to-One)、一对多(One-to-Many)、多对一(Many-to-One)和多对多(Many-to-Many)等,满足了各种复杂的数据关系需求。 #### Queries in Hibernate Hibernate支持...
4. **集合的多态处理**:在映射文件或注解中,可以使用`<one-to-many>`或`<many-to-one>`标签,配合`class`属性指定子类类型,使得集合可以包含多种类型的对象。 5. **多态查询的实现**:Hibernate支持使用`...
<one-to-many name="children" class="ChildEntity" fetch="LAZY"/> ``` 或者在Java注解中: ```java @OneToMany(fetch = FetchType.LAZY) private List<ChildEntity> children; ``` **4. 延迟加载的问题** 尽管...
Step by step and easy to follow, this book describes many of the Java EE 6 specifications and reference implementations, and shows them in action using practical examples. This book uses the new ...