如何在父级中创建具有复合ID的双向一对多关系?

时间:2015-11-02 10:39:03

标签: java jpa persistence

如何更新以下类以获得父母与子女之间的一对多关系?

父:

@Entity
public class Parent {

    @Embeddedid
    private ParentId id;

    @OneToMany
    private List<Child> children;

    public Parent() {
    }

    public Parent(final ParentId id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Parent [id=" + this.id + ", children=" + this.children + "]";
    }

    // (Getters and setters)
}

的ParentId:

@Embeddable
public class ParentId implements Serializable {

    private Integer key1;

    private Integer key2;

    public ParentId() {
    }

    public ParentId(final Integer key1, final Integer key2) {
        this.key1 = key1;
        this.key2 = key2;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ParentId other = (ParentId) obj;
        if (this.key1 != other.key1) {
            return false;
        }
        if (this.key2 != other.key2) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + this.key1;
        result = prime * result + this.key2;
        return result;
    }

    @Override
    public String toString() {
        return "ParentId [key1=" + this.key1 + ", key2=" + this.key2 + "]";
    }

    // (Getters for key1 and key2)
}

子:

@Entity
public class Child {

    private Integer id;

    @ManyToOne
    private Parent parent;

    public Child() {
    }

    public Child(final Integer id, final Parent parent) {
        this.id = id;
        this.parent = parent;
    }

    @Override
    public String toString() {
        return "Child [id=" + this.id + ", parent.getId()=" + this.parent.getId() + "]";
    }

    // (Getters and setters)
}

因为我们在当前项目中遇到了这个问题,所以我会发布我们的解决方案作为这个问题的答案。

1 个答案:

答案 0 :(得分:0)

父:

@Entity
public class Parent {

    @Embeddedid
    private ParentId id;

    /*******************************************************/
    /*******************************************************/
    /*******************************************************/
    // @formatter:off
    @OneToMany(
        cascade = { CascadeType.MERGE, CascadeType.PERSIST },
        fetch = FetchType.EAGER
    )
    @JoinColumns(
        {
            @JoinColumn(name = "parent_key1"),
            @JoinColumn(name = "parent_key2")
        }
    )
    // @formatter:on
    /*******************************************************/
    /*******************************************************/
    /*******************************************************/
    private List<Child> children;

    public Parent() {
    }

    public Parent(final ParentId id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Parent [id=" + this.id + ", children=" + this.children + "]";
    }

    // (Getters and setters)
}

子:

@Entity
public class Child {

    private Integer id;

    @ManyToOne
    /*******************************************************************************/
    /*******************************************************************************/
    /*******************************************************************************/
    // @formatter:off
    @JoinColumns(
        {
            @JoinColumn(name = "parent_key1", insertable = false, updatable = false),
            @JoinColumn(name = "parent_key2", insertable = false, updatable = false)
        }
    )
    // @formatter:on
    /*******************************************************************************/
    /*******************************************************************************/
    /*******************************************************************************/
    private Parent parent;

    public Child() {
    }

    public Child(final Integer id, final Parent parent) {
        this.id = id;
        this.parent = parent;
    }

    @Override
    public String toString() {
        return "Child [id=" + this.id + ", parent.getId()=" + this.parent.getId() + "]";
    }

    // (Getters and setters)
}