java Hibernate - 一对多关系 - 外键总是“空”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16865658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:12:18  来源:igfitidea点击:

Hibernate - One to many relation - foreign key always "null"

javasqlhibernatemapping

提问by jan

I have two really simple objects and one object should contain the other one in an "one-to-many" relation in a set. The objects get inserted correctly in the database, but the foreign key in the "children" table is always "null".

我有两个非常简单的对象,一个对象应该在集合中以“一对多”关系包含另一个对象。对象在数据库中正确插入,但“子”表中的外键始终为“空”。

I can't figure out why:

我不明白为什么:

This is the test-object and it hold the children in its set:

这是测试对象,它将子项保存在其集合中:

@Entity
@Table(name="test")
public class TestObj {

    public TestObj(){}

    private Long id;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    private Set<Children> children = new HashSet<Children>();

    @OneToMany(mappedBy = "testObj", cascade = CascadeType.ALL)
    public synchronized Set<Children> getChildren() {
        return children;
    }
    public synchronized void setChildren(Set<Children> children) {
        this.children = children;
    }
    public void addChildren(Children child){
        children.add(child);
    }
}

This is the children object and it holds an back-link to the "TestObj":

这是子对象,它包含一个指向“TestObj”的反向链接:

@Entity
@Table(name = "children")
public class Children {

    public Children(){}

    private Long id;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    private TestObj testObj;

    @ManyToOne
    @JoinColumn
    public TestObj getTestObj() {
        return testObj;
    }

    public void setTestObj(TestObj testObj) {
        this.testObj = testObj;
    }
}

I persist this objects with this code:

我使用以下代码保留此对象:

EntityManagerFactory entityManagerFactory = HibernateEntityMangerSingelton.getEntityManagerFactory();
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();


TestObj user = new TestObj();

Children child = new Children();
user.addChildren(child);
try {

    entityManager.persist(user);

    entityManager.getTransaction().commit();

} catch (Exception e) {
    System.out.println(e);
}finally{
    entityManager.close();
}

Can some explain me why this is happening?

有人可以解释我为什么会这样吗?

回答by JB Nizet

It's quite simple: you never initialize the testObjfield in Children(which should be named Child, BTW). Children.testObjis the owner of the association, and is the field that is mapped to the join column, so if it's null, the join column will be null.

这很简单:您永远不会初始化(应该命名为 Child,顺便说一句)中的testObj字段ChildrenChildren.testObj是关联的所有者,并且是映射到联接列的字段,因此如果它为空,联接列将为空。

回答by Petr Dostál

I had a similar problem which i solved by calling setter of the owner side. The 2 metods seting and adding the children to the TestObj should be altered like this, so that the TestObj is initialized at the owner side:

我有一个类似的问题,我通过调用所有者端的 setter 解决了这个问题。设置和将子项添加到 TestObj 的 2 个方法应该像这样更改,以便在所有者端初始化 TestObj:

public synchronized void setChildren(Set<Children> children) 

{



this.children = children;


for(Children child : children)
    {
    // initializing the TestObj instance in Children class (Owner side) so that it is not a null and PK can be created
            child.setTestObj(this);
    }
    }

The second method:

第二种方法:

public void addChildren(Children child)
{
    children.add(child);
//Intializing the TestObj instance at the owner side
    child.setTestObj(this);
}