`
louisling
  • 浏览: 143904 次
  • 性别: 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:

相关推荐

Global site tag (gtag.js) - Google Analytics