JPA映射,父到子和子到父,带有两个类和抽象类

JPA映射,父到子和子到父,带有两个类和抽象类,jpa,abstract,many-to-one,Jpa,Abstract,Many To One,我有两个类,它们继承自一个抽象类,并且具有父子关系 所以我使用注释OneToMany和ManyToOne,但子类中的父实体总是空的。 有人能帮我一下吗?我花了几个小时在谷歌上搜索和测试了很多配置文件,但都没有成功 以下是我的类中的代码: public @Table(name="flowentity") @Entity abstract class FlowEntity { final static Logger log = LoggerFactory.getLogger(FlowEntity.

我有两个类,它们继承自一个抽象类,并且具有父子关系

所以我使用注释OneToMany和ManyToOne,但子类中的父实体总是空的。 有人能帮我一下吗?我花了几个小时在谷歌上搜索和测试了很多配置文件,但都没有成功

以下是我的类中的代码:

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;

如果我不添加@JoinColumn注释,JPA将创建一个关联表,但无法检索父级,而关联可以通过在数据库中请求直接完成

非常感谢你的帮助


问候,

谢谢克里斯的评论,你说得对,我忘了更改表名。我认为这不是问题所在,因为继承映射位于一个带有DTYPE鉴别器列的表flowentity中

最后,我通过在添加新子级时设置父属性来解决问题,如下所示:

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

关于,

为什么这两个子类在同一个ams\U newCPEntity表中?它可能不相关,但如果所有子类都使用ams_NewcEntity表,则可以将其作为辅助表添加到抽象流实体中,而不是每个子类中。如果不将@JoinColumn添加到多个单亲映射中,JPA要求join列默认为“parent_flowId”。如果它使用关系表,则表明JPA提供程序处理注释的方式存在问题。打开日志记录并检查警告。
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();
}
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);
    }
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;
    }