• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to realize an CRUD ManyToMany relationship - join table - in Java with JPA?

 
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I’m busy with one of my first Java projects. I’m OCA/OCP certified and making my steps with Spring boot and Maven.

Situation (see ERD attachement):
  • an dat table: main data.
  • an unit table: config data
  • an join table dat_unit: relation between dat and unit


  • Functionality:
    I think it is clear when you see the ERD attachement, an dat record can have been related to one or more units, were the join table does have two additional fields.
    On the dat-detail-page the user can select a unit by dropdown and fills the team field with the team name and the field type of user.

    Postman, my expectation how it should like:

  • Just to CRUD dat records in the database from the detail page is working fine.
  • I’ve been busy with a collegae to get it working for a join table. But after three days googling (baeldung etc.), try-and-error we didn’t succeed. Every time we recognized 'our' situation and thought this is it, but not.
  • Another collegae, with more Java experience understood our point, came up with an example which works for him with three entity classes and three model classes and some extra classes and bunch of code. Hard or not to follow and he told, I’m not sure if this is the right solution.

  • Question:
    My question: does someone know or have understandable tutorial/example how this should be solved in Java + JPA? I can’t imagine that this is the solution for such default situation,  because for the ‘findAll()’ in SQL  it is not more than this:

    (BTW: I have it working in PHP, and I realize that there is some more work behind, because if the user is updating the dat record by removing and/or adding units, than the join table must updated as well! But that is the next step.)

    I hope that I have explained it clearly and hope that I did give the necessary/right information.
    ManyToMany-jointable.jpg
    ERD
    ERD
     
    Nico van de Kamp
    Ranch Hand
    Posts: 34
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Nobody so far...

    I have found two site's found:
  • infoworld.com
  • asbnotebook.com


  • OneToOne is working now and I begin to understand that the approach is (totally) different from let say straight forward thinking of SQL/PL-SQL.
     
    Nico van de Kamp
    Ranch Hand
    Posts: 34
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I did get it working a few weeks ago only I have changed Unit with Roles.

    Employee Entity is my 'Parent' side


    And Role is my 'child' side:


    Join table is employees_roles. If I remove a role from my REST Api, than also an record of the join table is removed and not from the Role table!! That's good because the Role table is an constant/lookup table.

    The only question what is left is that:
    My Role table has three fields: id, code and description
    It is constant/lookup table with 6 prefilled records.
    Question: if I change the value of the field "description" and/or code, than it is overwritten. How can I disable this, so that only records in join table are added?

    ** EDIT **

    Ah, I read also my other post and I think Rob has given the solution there.
    The solution I assume is 'insertable = false, updatable = false':

    It seems strange to me if this is the 'real' solution because how do you maintain this table? Than you have to write another class...? I should expect that you disable it from the parent side writing to the child table.

    If somebody does have another idea and/or better idea, let me know.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic