我目前正在使用Java EE / EclipseLink和postgreSQL处理Web应用程序。 关于我的实体,我特别管理“Project”,“User”和“Right”(访问权限,如读取,写入,删除...)。这些实体之间的关系是:项目可以拥有多个对项目具有不同权限的用户。因此,我获得了三重关联:Project + User + Right。
在这种关联的持久性过程中,我正面临一个烦人的问题。当我坚持一个项目及其信息时,一切都很好(它在数据库中,我可以在我的应用程序中使用它),我也坚持权利和用户。然后,我想在它们之间添加一个关联,所以我创建一个名为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项目并且设置正确)我想知道它是否是错误注释的关联但是我找不到改变它的正确方法。
所以,我真的需要你的帮助! : - )
提前感谢您的建议!
答案 0 :(得分:1)
您错过了@MapsId
注释:
指定ManyToOne或OneToOne关系属性 提供EmbeddedId主键(属性)的映射 在EmbeddedId主键中,或者是一个简单的主键 父实体。 value元素指定a中的属性 关系属性对应的复合键。
@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;