在冬眠中通过财产获得相关的孩子?

时间:2019-03-15 15:20:32

标签: java hibernate spring-boot

我有这个java类,它是父类:

@Getter @Setter @NoArgsConstructor
@Entity
@Table(name="parent")
public class Parent{
    @Id
    @Column(name="ID", nullable=false)
    private int Id;

    @Column(name="PARENT_ID")
    private String parentID;

    @Column(name="FIRST_NME")
    private String firstName;

    @Column(name="LAST_NME")
    private String lastName;

    /* This is the relationship I am trying to establish! */
    @OneToMany(fetch=FetchType.EAGER, mappedBy = "parent", cascade = {CascadeType.ALL})
    List<Child> children;
}

这是我的孩子班:

@Getter @Setter @NoArgsConstructor
@Entity
@Table(name="child")
public class Child {

    @EmbeddedId
    private CompositeKeyChild id;

    @Column(name="CHILD_NAME")
    private String insertUser;

    @ManyToOne
    @JoinColumn(name="PARENT_ID", insertable = false, updatable = false)
    private Parent parent;
}

这是上面的代码中提到的复合Key类:

@Embeddable
public class CompositeKeyChild implements Serializable {

    @Column(name = "PARENT_ID")
    private String parentID;

    @Column(name = "CHILD_CD")
    private String childCD;

    public CompositeKeyChild() {
    }

    public CompositeKeyChild(String parentID, String childCD) {
        this.parentID = parentID;
        this.childCD = childCD;
    }

    public String getParentID() {
        return ParentID;
    }

    public String getChildCD() {
        return childCD;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CompositeKeyChild)) return false;
        CompositeKeyChild that = (CompositeKeyChild) o;
        return Objects.equals(getParentID(), that.getParentID()) &&
                Objects.equals(getChildCD(), that.getChildCD());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getParentID(), getChildCD());
    }
}

我正在尝试做的事情:

当我使用简单的JPA存储库执行findAll()时,我希望能够找回所有父母,其中包括所有子女。

当前发生的事情:

我有没有孩子的父母。

值得注意的事情:

  • 子表有一个复合键,所以我这样处理, 但是-也许我需要做一些其他事情才能获得 映射以这种方式使用ID?

  • 我意识到具有ID字段和PARENT_ID列是 可能很愚蠢,但这是我正在使用的旧代码,并且有人 以这种方式设计。我无法更改。

  • 我绝对知道我在数据库中有相关数据。我手动输入了它,并且一个简单的带有联接的select语句实际上会显示给我。

0 个答案:

没有答案