Strategies for working with circular links caused by JPA relationships?

I am trying to break my application into modules using functions packaged in separate banks, such as feature-a.jar, feature-b.jar, etc.

Separate function banners, such as feature-a.jar, should contain all the code for the function, including jpa objects, business logic, the rest of apis, unit tests, integration test ... etc.

The problem I am facing is that bidirectional relationships between JPA objects cause circular references between jar files. For example, the Customer object should be in customer.jar, and the Order should be in order. Jar, but the Client refers to the order and the order, referring to the client, making it difficult to separate it into separate jars / eclipse projects.

The options that I see for working with circular dependencies in a JPA entity:

  • Option 1: Put all the objects in one container / one project
  • Option 2: Do not map specific bi-directianl relationships to avoid circular dependencies between projects.

Questions:

  • What rules / principles did you use to decide when to do bi-directional matching versus no?
  • You were able to break jpa objects into your own projects / jar by function, if so, how did you avoid circular dependency problems?
+4
source share
1 answer

There are no rules that you used to define Circular in JPA . I also have to check it out.

In my example, there is a Category object that has OneToOne up to parentCategory and OneToMany up to childCategories as bidirectional.

I think Circular Reference as below data structure.

 CategoryID Name ParentID 001 AAA 002 002 BBB 003 003 CCC 004 004 DDD 001 

I would suggest to avoid problems.

Example:

 public void addNewCategory(Category category) { checkCircularity(Category category, category.getParent()); } public static void checkCircularity(Category child, Cagegory parent) throws CircularReferenceException { if (superior != null) { if (sub.isEqual(superior) || sub.isEqual(parent.getParent())) { throw new CircularReferenceException("Circularity Referencing........."); } else if (finder.getSuper(superior) != null) { checkCircularity(child, parent.getParent()); } } } 

If the program does not throw a CircularReferenceException , there is no circular reference for this Category instance.

+2
source

All Articles