`
louisling
  • 浏览: 146028 次
  • 性别: Icon_minigender_1
  • 来自: ZhuHai
社区版块
存档分类
最新评论

One-To-Many in JPA

    博客分类:
  • JPA
阅读更多
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;
    }
}
分享到:
评论
1 楼 nforce_com 2011-03-25  
:idea:

相关推荐

    one-to-many-jpa-example

    One2Many JPA OrderBy 与 InMemory 排序示例 订购方式 用于按顺序创建实体的卷曲通过示例 curl -iX POST ...

    SPRING HIBERNATE

    Hibernate提供了多种映射策略,如一对一(One-to-One)、一对多(One-to-Many)、多对一(Many-to-One)和多对多(Many-to-Many)等,满足了各种复杂的数据关系需求。 #### Queries in Hibernate Hibernate支持...

    hibernate的多态查询

    4. **集合的多态处理**:在映射文件或注解中,可以使用`&lt;one-to-many&gt;`或`&lt;many-to-one&gt;`标签,配合`class`属性指定子类类型,使得集合可以包含多种类型的对象。 5. **多态查询的实现**:Hibernate支持使用`...

    hibernate延迟加载解决

    &lt;one-to-many name="children" class="ChildEntity" fetch="LAZY"/&gt; ``` 或者在Java注解中: ```java @OneToMany(fetch = FetchType.LAZY) private List&lt;ChildEntity&gt; children; ``` **4. 延迟加载的问题** 尽管...

    Apress.Beginning.Java.EE.6.with.GlassFish.3.2nd.Edition

    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 ...

Global site tag (gtag.js) - Google Analytics