Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA Many To Many insert from either side

 
Robert Panivino
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day. I have a pretty standard question about a many to many relationship which I'm sure has multiple solutions. I am curious what you guys think is better and what is not.

Supposed we have two tables: A and B. Now suppose we have no Cascading defined. A contains a list reference to B, with @JoinColumn and the fetch type is lazy.

An insert on A will work like this: if I have an object B defined in A's collection, then the join table is populated correctly.

Now, I want to clarify one thing first: this works like this because of the @JoinColumn annotation which specifies the primary key, thus knowing that the join table should be populated?

And my main question is: what options do I have such that by saving an object of type B, the join table would be populated?

I will give one option a colleague of mine implemented, which I don't like one bit: When parsing the DTO enclosing an object of type B, he uses objects of type A to do this. This can be quite complicated since he needs to analyze cases where either B was deleted from an object of type A or was added to it.

I am sure that there are other solutions better than this one. I am curios which ones you recommend and which ones you do not.

Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic