休眠双向@ManyToOne,更新不拥有方不工作 [英] Hibernate bidirectional @ManyToOne, updating the not owning side not working

查看:101
本文介绍了休眠双向@ManyToOne,更新不拥有方不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的设置来尝试带注释的双向映射:

  @Entity 
public class TypeA扩展AbstractModel< TypeA> {

@Id
@GeneratedValue
private int id;

@OneToMany(mappedBy =a)
私人收藏< TypeB> BS;

// Getters& Setters ...
}

  @Entity 
public class TypeB extends AbstractModel< TypeB> {

private static final long serialVersionUID = -3526384868922938604L;

@Id
@GeneratedValue
private int id;

@ManyToOne()
@JoinColumn(name =a_id)
private TypeA a;
}

设置属性TypeA.bs时,不会影响映射,但这应该。请参阅以下示例:

  TypeB b = new TypeB(); 
this.typeBDao.save(b);

TypeA a = new TypeA();
a.setBs(ListUtils.createList(b));

System.out.println(a.getBs()); //输出:[TypeB @ 25fe4d40]

this.typeADao.save(a);

System.out.println(a.getBs()); //输出:[TypeB @ 25fe4d40]

this.typeADao.refresh(a);

System.out.println(a.getBs()); //输出:[]

this.typeBDao.refresh(b);
System.out.println(b.getA()); //输出:null

如果映射是双向的,则应该填充集合, b应该更新,但它没有。任何想法?



编辑感谢您的帮助人员,现在我明白了!

h2_lin>解决方案

对于一个一致的域模型,您应该始终设置关系的两侧,如下所示:

  TypeB b = new TypeB(); 

TypeA a = new TypeA();
a.setBs(ListUtils.createList(b));
b.setA(a);

this.typeBDao.save(b);
this.typeADao.save(a);

当实体处于不一致状态时,JPA将始终根据对象的状态拥有JPA关系的一面。在这种情况下,TypeB拥有与TypeA的关系。因此,如果TypeB的对象没有对TypeA的引用,那么JPA假定没有定义关系。


I have a really simple setup to try out a bidirectional mapping with annotations:

@Entity
public class TypeA extends AbstractModel<TypeA> {

    @Id
    @GeneratedValue
    private int id;

    @OneToMany(mappedBy="a")
    private Collection<TypeB> bs;

    // Getters & Setters ... 
}

and

@Entity
public class TypeB extends AbstractModel<TypeB> {

    private static final long serialVersionUID = -3526384868922938604L;

    @Id
    @GeneratedValue
    private int id;

    @ManyToOne()
    @JoinColumn(name="a_id")
    private TypeA a;
}

When I set the property TypeA.bs this does not affect the mapping, although it should. See the following example:

TypeB b = new TypeB();
this.typeBDao.save(b);

TypeA a = new TypeA();
a.setBs(ListUtils.createList(b));

System.out.println(a.getBs()); // output: [TypeB@25fe4d40]

this.typeADao.save(a);

System.out.println(a.getBs()); // output: [TypeB@25fe4d40]

this.typeADao.refresh(a);

System.out.println(a.getBs()); // output: []

this.typeBDao.refresh(b);
System.out.println(b.getA()); // output: null

If the mapping is bidirectional, the collection should be populated and the property a of b should be updated, but it isnt. Any ideas?

Edit Thanks for your help folks, now I got it!

解决方案

For a consistent domain model you should always set both sides of the relation, like this:

TypeB b = new TypeB();

TypeA a = new TypeA();
a.setBs(ListUtils.createList(b));
b.setA(a);   

this.typeBDao.save(b);
this.typeADao.save(a);

When your entities are in an inconsistent state, JPA will always store values according to the object state of the owning side of the JPA relation. In this case TypeB owns the relation to TypeA. Thus if an object of TypeB does not have a reference to TypeA, JPA assumes there is no relation defined.

这篇关于休眠双向@ManyToOne,更新不拥有方不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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