2013-02-20 25 views
1

我有兩個類從一個抽象類繼承,並有一個父子關係。JPA映射,家長對兒童和孩子父母與兩個類和抽象類

因此,我使用註釋OneToMany和ManyToOne,但子類中的父實體始終爲null。 有人可以幫助我,我花了幾個小時搜索並測試許多conf沒有成功。

這些都是從我的類代碼:

public @Table(name="flowentity") @Entity abstract class FlowEntity { 

final static Logger log = LoggerFactory.getLogger(FlowEntity.class); 

//Globals informations concerning the flow state 
private @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Integer flowId = 0; 
private String flowName; 

private @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL) 
     Set<PeopleEntity> actorSet = new HashSet<>(); 


//Global parameters for most of flows 
//Organizational parameters 
private @OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL) 
     @JoinColumn(name="organisationalEntity_Id") 
     OrganisationalEntity organisationalEntity; 

...

public @Table(name="ams_newCPEntity") @Entity class NewMultiCPEntity extends FlowEntity { 

private @OneToMany(targetEntity=NewCPEntity.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL,mappedBy="parent") 
     Set<NewCPEntity> cpList = new HashSet<NewCPEntity>(); 

//Constructor 
    public NewMultiCPEntity(){ 
     setFlowName(EnumFlow.N_CP_M.getFlowAcronym()); 
    } 

...

public @Table(name="ams_newCPEntity") @Entity class NewCPEntity extends FlowEntity { 

final static Logger log = LoggerFactory.getLogger(NewCPEntity.class); 

private boolean formNCPValidated; 

private @ManyToOne @JoinColumn(name="parent_Id", nullable=false) 
     NewMultiCPEntity parent; 

public NewCPEntity(){ 
    log.debug("Instanciation of a new CP"); 
    setFlowName(EnumFlow.N_CP.getFlowAcronym()); 
} 

public @Override OrganisationalEntity getOrganisationalEntity(){ 
    return parent.getOrganisationalEntity(); 
} 

...

如果我不去添加@JoinColumn註釋,JPA創建一個關聯ta但無法檢索父級,而關聯可以通過在數據庫中請求直接完成。

謝謝非常幫忙。

問候,

+0

爲什麼兩個子類在同一個ams_newCPEntity表中?它可能沒有關係,但如果子類全部使用ams_newCPEntity表,則可以將其作爲輔助表添加到抽象Flow實體中,而不是添加到每個子類中。如果您未將@JoinColumn添加到ManyToOne父映射中,則JPA要求連接列默認爲「parent_flowId」。如果它正在使用關係表,則表示JPA提供程序處理註釋的方式存在問題。打開日誌並檢查警告。 – Chris 2013-02-21 13:15:10

回答

0

謝謝克里斯的評論,你是對的,我忘記改變表的名稱。我不認爲這是問題,因爲繼承映射在DTYPE鑑別器列的一個表中。

最後,我加入了新的兒童在這樣的設置父attributs解決我的問題:

public @Table @Entity class NewMultiCPEntity extends FlowEntity { 

    private @OneToMany(targetEntity=NewCPEntity.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL) 
      List<NewCPEntity> cpList = new ArrayList<>(); 

    //Constructor 
     public NewMultiCPEntity(){ 
      setOrganisationalEntity(new OrganisationalEntity()); 
      setFlowName(EnumFlow.N_CP_M.getFlowAcronym()); 
     } 

    public List<NewCPEntity> getNCPList(){ 
     if(cpList == null){ 
      cpList = new ArrayList<>(); 
     } 
     if(cpList.isEmpty()){ 
      addCPEntity(new NewCPEntity()); 
     } 
     return Collections.unmodifiableList(cpList);} 

    public boolean removeCPEntity(NewCPEntity entity){ 
     return cpList.remove(entity); 
    } 
    public boolean addCPEntity(NewCPEntity entity){ 
     entity.setParent(this); 
     entity.setOrganisationalEntity(this.getOrganisationalEntity()); 
     return cpList.add(entity); 
    } 

而且我刪除getOrganizationalEntity的覆蓋在孩子:

public @Table @Entity class NewCPEntity extends FlowEntity { 

    final static Logger log = LoggerFactory.getLogger(NewCPEntity.class); 


    private @ManyToOne(targetEntity=NewMultiCPEntity.class,cascade=CascadeType.ALL) 
      NewMultiCPEntity parent; 

    public NewCPEntity(){ 
     log.debug("Instanciation of a new CP"); 
     setFlowName(EnumFlow.N_CP.getFlowAcronym()); 
    } 

    public NewMultiCPEntity getParent() { 
     return parent; 
    } 

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

問候,