In a one-to-many relationship usualy the field annotated with @ManyToOne is the owner - the other side has 'mappedBy' attribute. However if I skip the 'mappedBy' and annotate both sides with @JoinColumn (same column) I can update both sides - changes are propageted to db.
I do not have two unidirectional relations instead of one bidirectional - there's just one join column.
What problems can I encounter by not having one side chosen as relations owner?
My entity looks similar to the following:
@Entity
public class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "parent_id")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private B parent;
@OneToMany()
@JoinColumn(name = "parent_id")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private List<B> children = new ArrayList<B>();
...
}
It doesn't seem to have any impact on performance (at least inserts look ok) Here is a simple test and log output:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
B a = new B("a");
B b = new B("b");
B c = new B("c");
B d = new B("d");
B e = new B("e");
session.save(a);
session.save(b);
session.save(c);
session.save(d);
session.save(e);
session.getTransaction().commit();
System.out.println("all objects saved");
session.beginTransaction();
a.getChildren().add(b);
a.getChildren().add(c);
session.save(a);
session.getTransaction().commit();
System.out.println("b and c added as children");
session.beginTransaction();
a.getChildren().add(d);
a.getChildren().add(e);
session.getTransaction().commit();
System.out.println("e and f added as children");
session.close();
Hibernate: insert into B (fieldInB, parent_id) values (?, ?)
Hibernate: insert into B (fieldInB, parent_id) values (?, ?)
Hibernate: insert into B (fieldInB, parent_id) values (?, ?)
Hibernate: insert into B (fieldInB, parent_id) values (?, ?)
Hibernate: insert into B (fieldInB, parent_id) values (?, ?)
all objects saved
Hibernate: update B set parent_id=? where id=?
Hibernate: update B set parent_id=? where id=?
b and c added as children
Hibernate: update B set parent_id=? where id=?
Hibernate: update B set parent_id=? where id=?
e and f added as children