使用Spring启动和myibatis在JPA实体中自我引用

时间:2017-12-28 10:19:24

标签: spring hibernate jpa ibatis

我曾经运行过一次脚本:

DROP TABLE IF EXISTS COMMITTEE;
CREATE TABLE COMMITTEE(
    CODE NUMBER PRIMARY KEY,
    NAME VARCHAR2(100),
    P_CODE NUMBER,
    CONSTRAINT P_CODE_FK FOREIGN KEY (P_CODE) REFERENCES COMMITTEE(CODE) 
);

Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(1, 'Parent 1', NULL);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(2, 'Parent 2', NULL);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(3, 'Parent 3', NULL);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(4, 'Child 1.1', 1);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(5, 'Child 1.2', 1);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(6, 'Child 1.3', 1);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(7, 'Child 3.1', 3);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(8, 'Child 3.2', 3);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(9, 'Child 3.3', 3);
Insert into COMMITTEE(CODE, NAME, P_CODE) VALUES(10, 'Child 3.1.1', 7);

在我的映射器中,我必须查询所有COMMITTEE并获取他们的CHILDREN

所以我将我的实体配置如下:

@Entity
@Table(name = "committee")
public class Committee implements Serializable {

    private static final long serialVersionUID = 18787545L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "code")
    private Long code;

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

    @ManyToOne(cascade = { CascadeType.ALL })
    @JoinColumn(name = "p_code", referencedColumnName = "code")
    private Committee parent = null;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "parent")
    private Set<Committee> children = new HashSet<>(0);

    public Committee() {
    }

    public Committee(Long code, String name, Long p_code) {
        this.code = code;
        this.name = name;
        // this.p_code = p_code;
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /*
     * public Long getP_code() { return p_code; }
     * 
     * public void setP_code(Long p_code) { this.p_code = p_code; }
     */

    public Set<Committee> getChildren() {
        System.out.println("getChildren called");
        return children;
    }

    public void setChildren(Set<Committee> children) {
        System.out.println("setChildren called");
        this.children = children;
    }

    public Committee getParent() {
        return parent;
    }

    public void setParent(Committee parent) {
        this.parent = parent;
    }

}

但我总是从getChildren()获得空列表。

我在实体或MyIbatis配置中遗漏了什么?

更新

根据@Hadi的回答以及官方文档的链接,更改了实体

更新2

以下是mapper的代码:

@Mapper
public interface CommitteeMapper {

    @Select("SELECT * FROM COMMITTEE")
    Set<Committee> getParents();

    //Some other staff
}

1 个答案:

答案 0 :(得分:1)

尝试更改此

@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name = "p_code", referencedColumnName = "code") 
private Committee committee = null;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL,mappedBy="committee")
private Set<Committee> children = new HashSet<>(0);