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

时间:2013-05-31 20:42:11

标签: java sql hibernate mapping

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

我无法弄清楚原因:

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

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

这是children对象,它包含“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;
    }
}

我用这段代码坚持这个对象:

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

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

2 个答案:

答案 0 :(得分:6)

这很简单:你永远不会初始化testObj中的Children字段(应该命名为Child,BTW)。 Children.testObj是关联的所有者,是映射到连接列的字段,因此如果它为null,则连接列将为null。

答案 1 :(得分:1)

我有一个类似的问题,我通过调用业主方的setter解决了。将这两个子节点设置并将其添加到TestObj应该像这样改变,以便在所有者端初始化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);
    }
    }

第二种方法:

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