Reputation: 107
I currently work on a web app using Java EE/EclipseLink and postgreSQL. About my entities, I manage in particular "Project" , "User" and "Right" (access rights like read, write, delete...). The relationship between these entities is : a project can have multiple users who have different rights on the project. Hence, I get a triple association: Project+User+Right.
I'm facing an annoying problem during this association's persistence. When I persist a project with its information, everything is fine (it's in the DB and I can use it in my app), I also have persisted rights and users. Then, I want to add an association between them so I create the association entity called ProjectUserRight, I set the project, the user and the right from existing entities but when I persist it, I get the exception below :
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne « id_project »
Error Code: 0
Call: INSERT INTO project_user_right (id_right, id_project, id_user) VALUES (?, ?, ?)
bind => [3 parameters bound]
Query: InsertObjectQuery(ProjectUserRight@192db555)
Here is the class Project :
@Entity
@Table(name="project")
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_project", unique=true, nullable=false)
private Integer idProject;
//bi-directional many-to-one association to ProjectUserRight
@OneToMany(mappedBy="project", cascade={CascadeType.ALL})
private Set<ProjectUserRight> projetUtilDroits;
...
The class User :
@Entity
@Table(name="user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_user", unique=true, nullable=false)
private Integer idUser;
@Column(name="nom_utilisateur", nullable=false, length=50)
private String userName;
//bi-directional many-to-one association to ProjectUserRight
@OneToMany(mappedBy="user")
private Set<ProjectUserRight> projetUtilDroits;
...
The class Right :
@Entity
@Table(name="right")
public class Right implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_right", unique=true, nullable=false)
private Integer idRight;
@Column(name="type_right", nullable=false, length=10)
private String typeRight;
//bi-directional many-to-one association to ProjectUserRight
@OneToMany(mappedBy="right")
private Set<ProjectUserRight> projetUtilDroits;
...
And the association class :
@Entity
@Table(name="project_user_right")
public class ProjectUserRight implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ProjectUserRightPK id;
//bi-directional many-to-one association to Right
@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="id_right", insertable=false, updatable=false)
private Right right;
//bi-directional many-to-one association to Project
@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="id_project", insertable=false, updatable=false)
private Project project;
//bi-directional many-to-one association to User
@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="id_user", insertable=false, updatable=false)
private User user;
And the ProjectUserRightPK (edited after comment) :
@Embeddable
public class ProjectUserRightPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="id_project", unique=true, nullable=false)
private Integer idProject;
@Column(name="id_right", unique=true, nullable=false)
private Integer idRight;
...//getters and setters
@Column(name="id_user", unique=true, nullable=false)
private Integer idUser;
I wrote the following code to persist the association:
Project project = getService(Projet.class).getFromID(projectId);//retrieve the existing project from database with id
User user = getService(Utilisateur.class).getFromID(userId);//retrieve the existing user from database with id
Right right = getService(Right.class).getFromID(rightId);//retrieve the existing right from database with id
if(project !=null && user!=null && right!=null){
ProjectUserRight pud = new ProjectUserRight();
pud.setRight(right);
pud.setProject(project);
pud.setUser(user);
project.getProjectUserRights().add(pud);
right.getProjectUserRights().add(pud);
user.getProjectUserRights().add(pud);
service(ProjetUtilDroit.class).persist(pud);//call the persist function on this entity (works fine with all other entities)
}
I also tried to merge the project instead of persisting the association but I get the same error... (I also checked the id project and it is properly set) I'm wondering if it's the association which is wrongly annotated but I can't find the proper way to change it.
So, I really need your help! :-)
Thanks in advance for your advice!
Upvotes: 1
Views: 2541
Reputation: 691635
You missed the @MapsId
annotation:
Designates a ManyToOne or OneToOne relationship attribute that provides the mapping for an EmbeddedId primary key, an attribute within an EmbeddedId primary key, or a simple primary key of the parent entity. The value element specifies the attribute within a composite key to which the relationship attribute corresponds.
@MapsId("idRight")
@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="id_right")
private Right right;
@MapsId("idProject")
@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="id_project")
private Project project;
@MapsId("idUser")
@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="id_user")
private User user;
Upvotes: 1