休眠 - 一对多关系 - 外键总是“空” [英] Hibernate - One to many relation - foreign key always "null"

查看:118
本文介绍了休眠 - 一对多关系 - 外键总是“空”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我找不到原因: p>

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

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

public TestObj(){}
$ b $ private private long id ;

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

私人设置<儿童> children = new HashSet< Children>();

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






$ b这是儿童对象,链接到TestObj:

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

public Children(){}

私人长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();

儿童儿童=新生儿童();
user.addChildren(child);
尝试{

entityManager.persist(user);

entityManager.getTransaction()。commit();

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

有人可以解释为什么会发生这种情况吗?$ b $这很简单:你永远不会初始化 Children <中的 testObj 字段。 (它应该被命名为Child,BTW)。 Children.testObj 是关联的所有者,并且是映射到连接列的字段,因此如果它为null,则连接列将为null。


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":

@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?

解决方案

It's quite simple: you never initialize the testObj field in Children (which should be named Child, BTW). Children.testObj is 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.

这篇关于休眠 - 一对多关系 - 外键总是“空”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆