Hibernate bidirectional @ManyToOne, updating the not owning side not working

余生长醉 提交于 2019-12-18 17:05:04

问题


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!


回答1:


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.




回答2:


This is the way it's supposed to work: the owning side is the one which owns the relationship, and which is responsible of managing it in database. It's the responsibility of the developer (i.e. your responsibility) to make sure your object graph is always coherent : if one side of a relationship is modified, then the other side should be modified as well.

If you don't always do this, you must at least make sure to modify the owning side of the relationship, because Hibernate only cares about the owning side..




回答3:


The owning side is TypeB, so you need to call setA(), since the association is only managed by the owning side.



来源:https://stackoverflow.com/questions/5460573/hibernate-bidirectional-manytoone-updating-the-not-owning-side-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!