Should Hibernate Bidirectional Associations Avoid?

In the Spring / Hibernate project, we have a one-to-many relationship between two objects. Required Operations:

  • find parent-parent;
  • find parental children;
  • When the parent is deleted, we also need to remove the children;
  • mass creation of children.

We came up with two ways to implement this.

  • Bidirectional association : the child has the @ManyToOne column connecting it to the parent, and the parent has the @OneToMany lazy collection of children. All of the above operations can be performed in the model:

     child.getParent(); parent.getChildren(); //lazy loading session.delete(parent); //cascade removal of the children does the trick here session.save(parent); //cascade persist created the children 
  • Unidirectional communication: the child has an @ManyToOne column that binds it to the parent, but the parent does not have a reference to the children. Most operations should be performed in maintenance methods:

     child.getParent(); //still in the model Collection<Child> findChildren(Parent parent); //service method in ChildService void deleteChildren(Parent parent); //service method in ChildService void createChild(Parent parent, ... childAttributes); //service method in ChildService invoked for each new child. 

The first approach seems to be easier to implement (you can reuse the cascading Hibernate functions), but some of us consider bidirectional associations as a potential cause of problems.

What should be the best design choice? Are there any known issues, performance, or design created by a bi-directional approach?

+7
source share
4 answers

If your queries do the same as the queries executed behind the scene using Hibernate when they are lazy loading children, I donโ€™t see what you are getting, but just donโ€™t use the OneToMany association.

If you know what you are doing, and what each method calls in your organization, it means that in terms of database queries, you should not have problems with matching collections. Sometimes it is wise to cross them, sometimes it is better to use a special query to avoid too many calls to the database. The key is to understand what is happening.

Having asociation can also be very useful to be able to navigate through it in HQL queries, not necessarily to invoke the associated getter.

+3
source

While you are in a situation where lazy loading really works for you, I have not seen a real problem with bidirectional relationships. I once worked on a Flex application that used Hibernate, and bidirectional relationships often led to loading the entire database when the data was serialized to the client.

Ymmv

0
source

@JB Nizet's answer says almost everything about this: one more thing: looking at the sample method calls you sent, the bidirectional method is likely to make your business logic code more readable.

0
source

Yes, this should be avoided as much as possible. if you really think this is your requirement, then use only bidirectional. eg. Emp and Dept, each emp has one department, but there are many employees in the department. emp should know about its department, but it is not necessary that the department should also know their department.

-one
source

All Articles