我目前使用Java EE/EclipseLink和postgreSQL開發Web應用程序。 關於我的實體,我特別管理「項目」,「用戶」和「正確」(訪問權限如讀取,寫入,刪除...)。這些實體之間的關係是:一個項目可以擁有多個對該項目擁有不同權限的用戶。因此,我得到了三重關聯:項目+用戶+權利。持久性三重關聯JPA/EclipseLink
我在這個關聯的持久化過程中遇到了一個煩人的問題。當我堅持一個項目的信息時,一切都很好(它在數據庫中,我可以在我的應用程序中使用它),我也堅持權利和用戶。然後,我想他們之間,所以我創建了一個名爲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([email protected])
這裏是類項目:
@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)
}
我也嘗試合併該項目,而不是堅持協會,但我是t同樣的錯誤...(我也檢查了id項目,它設置正確)我想知道是否這是關聯錯誤地註釋,但我找不到改變它的正確方法。
所以,我真的需要你的幫助! :-)
在此先感謝您的建議!
請添加完成的ProjectUserRightPK類 – 2012-07-13 09:16:56
的定義!事實上,我必須承認,我沒有深入探討這個課程...... thx爲您的幫助 – Coralie 2012-07-13 11:14:21