JPA中的OneToMany映射,ID值不可用于第二级子对象

时间:2017-02-07 13:27:00

标签: hibernate jpa

在我提出问题之前,让我简要介绍一下我的代码:

@Entity 

public class GrandParent{ PrimaryKey : GrandParentId

@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST,CascadeType.REFRESH })
    @JoinColumn(name = "GrandParent_ID", referencedColumnName = "GrandParent_ID", foreignKey = @javax.persistence.ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) @Where(clause = "ACTIVE = 1 ")
    private final List<Parent> parents = new ArrayList<Parent>();}   




 @Entity 
public class Parent
{

@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST,CascadeType.REFRESH })
    @JoinColumns({
            @JoinColumn(name = "Parent_ID", referencedColumnName = "Parent_ID", foreignKey = @javax.persistence.ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)),
            @JoinColumn(name = "GrandParent_ID", referencedColumnName = "GrandParent_ID", foreignKey = @javax.persistence.ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))    })

private final List<Child> childs= new ArrayList<Child>();

.... }

@Entitypublic class Child { PrimaryKey:ChildId` }

这里当我尝试保存/或在GrandParent Entity上调用Save时,所有关联的子节点都会被保存,但需要在子实体中填充的grandParentId将变为0.0。

所以我的问题是:有没有办法可以通过任何anno将grandParent Entity的主要id值级联到Child Entity

0 个答案:

没有答案