Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 931
Erik Avatar asked Mar 28 '11 14:03

Erik


People also ask

What is owning side of relationship Hibernate?

The owning side of the relation tracked by Hibernate is the side of the relation that owns the foreign key in the database.

What is the use of bidirectional mapping in Hibernate?

The bidirectional Many-to-One association mapping is the most common way to model this relationship with JPA and Hibernate. It uses an attribute on the Order and the OrderItem entity. This allows you to navigate the association in both directions in your domain model and your JPQL queries.

Is OneToMany bidirectional?

Bidirectional @OneToMany Relationship – Employer/Employee When you traverse from the “Many” side to the “One” side, you only need to make reference to one object, which is why the Employee class holds a single reference to an Employer class via the private Employer employer instance variable.

What is bidirectional relationship in JPA?

In a bidirectional relationship, each entity has a relationship field or property that refers to the other entity. Through the relationship field or property, an entity class's code can access its related object. If an entity has a related field, the entity is said to “know” about its related object.


1 Answers

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.

like image 196
Kdeveloper Avatar answered Sep 28 '22 16:09

Kdeveloper