我目前正在使用 Java EE/EclipseLink 和 postgreSQL 开发一个 Web 应用程序。关于我的实体,我特别管理“项目”、“用户”和“权限”(读取、写入、删除等访问权限...)。这些实体之间的关系是:一个项目可以有多个对项目具有不同权限的用户。因此,我得到了三重关联:项目+用户+权利。
在这个协会的坚持中,我遇到了一个烦人的问题。当我用它的信息持久化一个项目时,一切都很好(它在数据库中,我可以在我的应用程序中使用它),我也拥有持久的权限和用户。然后,我想在它们之间添加一个关联,所以我创建了一个名为 ProjectUserRight 的关联实体,我从现有实体中设置了项目、用户和权限,但是当我坚持它时,我得到以下异常:
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)
这是类项目:
@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;
...
类用户:
@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;
...
右类:
@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;
...
和关联类:
@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;
和 ProjectUserRightPK (评论后编辑):
@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;
我编写了以下代码来保持关联:
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)
}
我也尝试合并项目而不是保留关联,但我得到了同样的错误......(我还检查了 id 项目并且它设置正确)我想知道它是否是错误注释的关联但我可以'找不到合适的方法来改变它。
所以,我真的需要你的帮助!:-)
提前感谢您的建议!