我有两个继承自抽象类的类,并且有一个父子关系。
所以我使用注释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会创建一个关联表,但无法检索父关联,而关联可以通过在数据库中请求直接完成。
非常感谢你的帮助。
此致
答案 0 :(得分:0)
最后,我通过在添加像这样的新子项时设置父属性来解决我的问题:
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;
}
此致