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但無法檢索父級,而關聯可以通過在數據庫中請求直接完成。
謝謝非常幫忙。
問候,
爲什麼兩個子類在同一個ams_newCPEntity表中?它可能沒有關係,但如果子類全部使用ams_newCPEntity表,則可以將其作爲輔助表添加到抽象Flow實體中,而不是添加到每個子類中。如果您未將@JoinColumn添加到ManyToOne父映射中,則JPA要求連接列默認爲「parent_flowId」。如果它正在使用關係表,則表示JPA提供程序處理註釋的方式存在問題。打開日誌並檢查警告。 – Chris 2013-02-21 13:15:10