I want many to many relation mapping with explicit join table.
My java spring project is providing REST api in HAL format and now it has only two types of classed:
- entities defined and
- "empty" interfaces for repositories (annotated by @RepositoryRestResource).
Sidenote, dependencies are circa these:
spring-boot-starter-parent
spring-boot-starter-data-jpa
spring-boot-starter-data-rest
spring-data-rest-hal-browser
postgresql / h2
spring-boot-starter-hateoas
Relations between tables (I mean _links of rest resources. have a look at sample hal+json document and look for ea:basket
for example.) are working as expected and it is working "for free", because of magic powder from spring auto-configuration and other magic included.
I am struggling now when adding new many to many dependency.
I have entities A, B and Tag. I want to have any number of Tag entities to be associated with A an B entities. I have no need to have any list/set in A and B entities (I will use jooq if I need anything more than crud).
First problem/question:
I see at least three approaches to model many to many relation with explicit join table (to be able to model my association needs) and I do not know differences. What are differences of these approaches?:
- use embeddable composite key in join table as Vlad suggest? https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/
- use @IdClass approach as stated here: https://stackoverflow.com/a/3588400/11152683
- use multiple @Id in join table as shown here: https://hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example-with-spring-boot-hsql/
Second question: What approach is needed in my case to easily model associations and make magic powder do its work when providing HAL format in crud repositories. I mean that links of relations will be generated automagically.