• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code suggestion regarding Entity objects  RSS feed

 
Rj Ewing
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm doing some refactoring and had some questions about what should go in the Entity vs Repository. I Entity A and Entity B. Entity A is often a child of Entity B, but not always.

Where would the best place to put a method like getEntityB(EntityA entity)? Does this belong in EntityB Repository or in EntityA? Or somewhere else?

RJ
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you correctly, you have an A and a B where B may have a parent property connecting it to A and likewise A [i]may[/y] have a child property connecting it to B. In other words, a 0..1 relationship.

In such cases, the common solution for JPA/EJB would be to annotate (or XML-mark) those relationships on the A and B Entity class definitions with "@OneToOne" and nullable capabilities. Remember to allow for lazy/eager fetch as needed.

If it gets more technical than that, consider asking in the ORM forum. You'll find more expertise there.
 
Rj Ewing
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I should have mentioned that we are using spring jdbc. I was aware of JPA oneToOne, but wasn't sure of the best way to replicate that using jdbc.

I'm assuming that I would need to inject EntityB Repository into EntityA class and have a method (EntityA.getEntityB()) which will fetch EntityB via the EntityB Repository?
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been a long time since I used Spring JDBC, but if that's what you are doing, I'd recommend putting the code in a Repository, not in the Entities.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!