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.
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.)
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
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 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.