0
votes

I have two 3 tables in my database:

group
----------
groupId    PK
name
user_account
----------
userId        PK
user_grouping
----------
groupId     PK    FK grouping(groupId -> groupId)
userId      PK    FK user_account(userId -> userId)

In my UserAccount Entity, I have the following line:

@JoinTable(name = "user_group", joinColumns = {
    @JoinColumn(name = "userId", referencedColumnName = "userId")}, inverseJoinColumns = {
    @JoinColumn(name = "groupId", referencedColumnName = "groupId")})
@ManyToMany
private List<Grouping> groupingList;

This is to show the relationship between all the tables. However, when I deploy, I get the following error:

SEVERE: Exception while preparing the app : Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [com.dv_model_ejb_1.0-SNAPSHOTPU] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field groupingList] from the entity class [class com.dv.model.entity.UserAccount] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
Local Exception Stack: 
Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [com.dv_model_ejb_1.0-SNAPSHOTPU] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field groupingList] from the entity class [class com.dv.model.entity.UserAccount] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
    at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:221)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1402)
...

I am not sure exactly how to interpret this error message. I am assuming I do not have the table relationship correctly modeled in my entity. But I am not sure why. Before today, this was compiling fine. Can anyone provide assistance?

1
Your code is not consistent. Is it user_group or user_grouping? And what is the Grouping entity? A Group? What's the mapping of both entities involved? - JB Nizet

1 Answers

0
votes

Description of tables is inconsistent (grouping vs group) and name of join table in entity mappings is not one of the table names. Because of these inconsistencies I assume following table structure:

useraccount (userid PK)
grouping (groupdid PK, name)
user_grouping (userId PK, groupId PK)
- FK userId references to user_account.userid
- FK groupId references to grouping.groupId

One correct way to map this to two entities is following:

@Entity
public class UserAccount {
    @Id int userId;

    @JoinTable(name = "user_grouping", joinColumns = {
       @JoinColumn(name = "userId", referencedColumnName = "userId")},
       inverseJoinColumns = {
       @JoinColumn(name = "groupId", referencedColumnName = "groupId")})
       @ManyToMany
       private List<Grouping> groupingList;
    //get and set omitted.
}

@Entity
public class Grouping {
    @Id int groupId;
    String name;
    //get,set, and optional inverse side of relationship omitted
}