Russell L.

asked • 12/18/19

Explicitly create Class for joining table or not for Java JPA many-to-many relationships

Hi,


For those who have used Java JPA (Hibernate or EclipseLink) I wanted to ask your opinion on the best way to represent m2m relationships in the Entity classes. The choice as I see it is to create 3 Entity Classes, with the intermediary table having two many-to-one relationships, or use 2 Entity Classes, each maintaining a bi-directional Many-to-Many. Here is a discussion I found that sums up my question https://stackoverflow.com/questions/2881160/2-one-to-many-instead-of-1-many-to-many


I can see the benefit of both ways, but I wa leaning toward have the three tables. For example, imagine an Invoice and Product table with a m2m between them. It makes sense to me to create an "Invoice Line Item" table that maintains a m2o to both Invoice and Product, effectively creating the m2m between Invoice and Product. However, I do know not all m2m have a joining table that makes logical sense for exposure. But the answer in the link above seems to indicate that keeping the DB clean of orphan records suggests still creating the JPA class for the intermediary table, even if it's practically a hidden element to users.


Thoughts?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.