• 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

Dealing with entities whose primary key exists as a compound key in two separate entities.

 
Ranch Hand
Posts: 217
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider my first Entity. EmployeePersonnel. This is essentially the representation of a join table.



Now, let's look at my EmployeeType entity:



Finally, we have the Personnel Type Entity:



I'm not allowed to create an ID column on my EmployeePersonnel join table. I've also tried various things such as creating a @Embeddable EmployeePersonnelId table, like so:



I've also tried giving the @EmbeddedId annotation to both of the variables of EmployeePersonnel. Of course, it says I can't have multiple ones. I haven't been able to figure this out, even after trying various combinations of annotations.
 
Ranch Hand
Posts: 91
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Mark,

I think a bit of more information can come in handy here, such as the
DDL for your tables...

When you say that you cannot create the ID column, what error message
are you getting ?
 
M Richardson
Ranch Hand
Posts: 217
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mano Ag wrote:
Hi Mark,

I think a bit of more information can come in handy here, such as the
DDL for your tables...

When you say that you cannot create the ID column, what error message
are you getting ?



It's not that I'm getting an error message, but rather, the requirements state that I have to work without giving our join table an "ID" column.

The tables are as follows:

Employee_Type : Contains ID and NAME column.
Personnel_Type : Contains ID and NAME column.
Employee_Personnel: Contains EMPLOYEE_TYPE_ID and PERSONNEL_TYPE_ID columns.



Here's the DDL:





 
Sheriff
Posts: 22510
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think you even need class EmployeePersonnel? The @ManyToMany annotations should take care of that.

Speaking of the many-to-many, you now have two distinct many-to-many relations that just happen to use the same join table. That's asking for trouble. You should instead:
  • Choose one of the sides as being the owner. That one gets the @JoinTable annotation. You specify the join columns of the other entity type with the inverseJoinColumns property.
  • The other side doesn't need the @JoinTable annotation. Instead, its @ManyToMany annotation should have its mappedBy set to the name of the property in the other entity. So if you use EmployeeType as the owner, then the @ManyToMany in PersonnelType gets a mappedBy value of personnelTypes. If you do it the other way around, then the mappedBy goes to the EmployeeType class with value employeeTypes.


  • Now here is something that's very important, at least in Hibernate: the owner matters. The owner's list should be used to make changes to the link table (add or remove elements). If you don't then changes will not get persisted in the database. (At least, that was my experience when migrating from TopLink to Hibernate in Spring Boot 1.5.)
     
    M Richardson
    Ranch Hand
    Posts: 217
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote:Why do you think you even need class EmployeePersonnel? The @ManyToMany annotations should take care of that.



    So, I'm not allowed to change the DB model. My thinking was that since the EmployeePersonnel table exists on the DB, it must have a java Entity to represent it, in the form of a class. Additionally, the end task would be to create a JSF screen which would have a page bean, which would point to this EmployeePersonnel class.

    In other words, the data which the EmployeePersonnel contains is like this:

    EmployeeType | PersonnelType
    1                      2
    8                      1
    8                      2
    10                    3
    ...

    These IDs of course, correspond to the EmployeeType and PersonnelType table's IDs, where EmployeeType 1 is a Plumber, and 8 is a Clown, and 10 is a Economist, etc.
    Any combination of an EmployeeType can have any combination of PersonnelType, where a Personnel Type can be a Federal Employee, Contractor, etc.

    Maybe I don't know enough, but based on my experience, since I'm creating a screen and acting on the table: EmployeePersonnel, an Entity must be created for this table. Am I wrong?
     
    Rob Spoor
    Sheriff
    Posts: 22510
    122
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The database model is just fine, there's no need to change that.

    But why do you need to show records of the link table on your screen?
     
    M Richardson
    Ranch Hand
    Posts: 217
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote: But why do you need to show records of the link table on your screen?



    So that I can have a drop down where a user can edit them, delete them, or create new ones.

    For example, There's no record that is an 8|3
    The user will then add a new record,
    click on drop-down on the left, select 8.
    And then pick option 3 for the right column.

    Of course, these options will be names, but that's beyond the scope of the question, and you get the idea.

    In other words, the idea is not to go and individually change the records of the Employee Type and Personnel Type Tables, but rather the values of the LINK table... which is an Entity in its own right.
     
    M Richardson
    Ranch Hand
    Posts: 217
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote:Why do you think you even need class EmployeePersonnel? The @ManyToMany annotations should take care of that.

    Update. Just spoke to my lead and you were correct. I don't need that entity

     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic