POJO with other POJO links

I am working on an API for accessing data stored on a system. The system contains things like people, meetings, and the procedures associated with these meetings. My application will be strictly read-only.

I use Spring w / RowMapper to create objects such as " Person ", " Appointment " and " Procedure ". I have a DAO for each item. (i.e.: PersonDAO.getById() , PersonDAO.getByName() , ..).

The problem is that the Appointment has a reference to the Person object. In the Person object, it would be nice to have a link to these Person meetings, but if I start downloading them, it will become a circular link.

So, I guess my question is the right way to handle this, just put the links (identifiers) in the POJO, and then at the business level (?) Just make the right calls to get the information? Or is it ok to somehow pass a reference to a DAO in an actual POJO so that I can lazily load object objects on a link? But then how do you handle a circular link? When I have a Person and lazily upload all their appointments, these appointments will also have a person associated with them. When I load this Person , it can potentially have information about the differences from Person I load the "Destinations" for.

Person (object x) lazy load -> Assignments can lazily load Person (object x ').

Since Person could have changed by the time I started lazily loading their appointments. I really need a Person object in Appointment to refer to the same Person object.

I understand. I know that I can just “make it work,” but I want to try and find a good solution. I thought about using hibernation for this, but thought it was really just an excess. Perhaps this is not so.

+6
java hibernate circular-reference lazy-loading
source share
2 answers

You describe bidirectional communication for which Hibernate has some (and generally very good) support.

Read how to do this in the docs.

Moving this manually will be quite inconvenient and error prone. I would not recommend it. Use the power of ORM tools such as Hibernate to exist there.

+2
source share

Expanding the use of Hibernate, I would recommend checking out the JPA annotation support supported by Hibernate (I believe this is part of the J2EE specification). You can annotate your classes with the @ManyToMany annotation. Check out these docs:

https://www.hibernate.org/397.html

0
source share

All Articles