【问题标题】:JPA Mapping, parent to children and child to parent with two classes and abstract classJPA Mapping,父到子和子到父,有两个类和抽象类
【发布时间】:2013-02-05 15:54:52
【问题描述】:

我有两个类继承自抽象类并具有父子关系。

所以我使用注释 OneToMany 和 ManyToOne 但子类中的父实体始终为空。 有人可以帮帮我吗,我花了几个小时在谷歌上搜索并测试了许多 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 会创建一个关联表,但无法检索父级,而关联可以直接通过在数据库中请求来完成。

非常感谢您的帮助。

问候,

【问题讨论】:

  • 为什么两个子类在同一个ams_newCPEntity表中?它可能不相关,但如果子类都使用 ams_newCPEntity 表,您可能希望将其作为辅助表添加到抽象流实体而不是每个子类中。如果不将 @JoinColumn 添加到 ManyToOne 父映射,JPA 要求连接列默认为“parent_flowId”。如果它改用关系表,这表明 JPA 提供程序处理您的注释的方式存在问题。打开日志记录并检查警告。

标签: jpa abstract many-to-one


【解决方案1】:

感谢 Chris 的评论,你说得对,我忘记更改表的名称。我认为这不是问题,因为继承映射在一个带有 DTYPE 鉴别器列的表中。

最后我通过像这样添加新孩子时设置父属性来解决我的问题:

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

问候,

【讨论】:

    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多