2017-12-27 1037 views
0

我試圖運行我的彈簧引導,ibatis,jpa應用程序。「使用JPA和Spring引導在實體中映射實體的重複列」

這是我在我的H2數據庫中運行的sql腳本。

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); 

這裏是我的實體:

package org.gso.committee.model; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

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

    private static final long serialVersionUID = 18787545L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long code; 
    private String name; 
    @Column(nullable = true) 
    private long p_code; 

    @ManyToOne() 
    @JoinColumn(name = "p_code") 
    private Committee parent; 

    @OneToMany(mappedBy = "parent") 
    private List<Committee> children = new ArrayList<>(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 Committee getParent() { 
     return parent; 
    } 

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

    public List<Committee> getChildren() { 
     return children; 
    } 

    public void setChildren(List<Committee> children) { 
     this.children = children; 
    } 

    @Override 
    public String toString() { 
     return "Committee [code=" + code + ", name=" + name + ", p_code=" + p_code + "]"; 
    } 
} 

的問題是,當我總是得到錯誤:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: org.gso.committee.model.Committee column: p_code (should be mapped with insert="false" update="false") 

回答

1

的問題是,爲什麼你映射P_CODE作爲一個單獨的屬性作爲它已通過參考父級映射。

但是,如果你想要這個屬性,你必須聲明爲只讀

@Column(nullable = true, insertable = false, updateable = false) 
private long p_code; 

它來設置父參考是非常重要的。做的是最好的方法是添加一些方便的方法是這樣的:

public void addChild(Committee committee) { 
    committee.setParent(this); 
    children.add(committee); 
} 

public void removeChild(Committee committee) { 
    committee.setParent(null); 
    children.remove(committee); 
} 
+0

謝謝,但即使與解決方案的getChildren()總是給我空 –

+0

添加了一個孩子,當你設置的家長嗎?在答案中看到我的編輯。 –

+0

實際上,我只想要getAllCommittees(),我想通過該列表和getChildren()獲取。我認爲這很簡單。我想我在實體中缺少一些東西 –