我在JPA EclipseLink中的用戶和組之間有一個ManyToMany關係。我的實現看起來像這樣:ManyToMany和JPA的問題EclipseLink
用戶類別:
@Entity
@Table(name="mbm_user")
public class User extends CanAccessBook{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(nullable = false)
private Long id;
@OneToMany(mappedBy="user",cascade=CascadeType.PERSIST)
private List<GroupMembership> groups = new ArrayList<GroupMembership>();
/**
GETTER & SETTER
**/
}
組類:
@Entity
@Table(name="mbm_group")
public class Group extends CanAccessBook{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(nullable = false)
private Long id;
@OneToMany(mappedBy="group", cascade=CascadeType.PERSIST)
private List<GroupMembership> users = new ArrayList<GroupMembership>();
/**
GETTER & SETTER
**/
}
GroupMembership類:
@Entity
@Table(name = "group_user")
@IdClass(UserGroupPK.class)
public class GroupMembership {
@Id
@ManyToOne
@PrimaryKeyJoinColumn(name="USERID")
private User user;
@Id
@ManyToOne
@PrimaryKeyJoinColumn(name="GROUPID")
private Group group;
@Temporal(TemporalType.DATE)
private Date joinedAt = new Date();
@Temporal(TemporalType.DATE)
private Date validUntil;
/**
GETTER & SETTER
**/
}
UserBookPK類:
public class UserBookPK{
@Id
@Column(name="BOOKID")
private Long book;
@Id
@Column(name="USERID")
private Long user;
public Long getBook() {
return book;
}
public void setBook(Long book) {
this.book = book;
}
public Long getUser() {
return user;
}
public void setUser(Long user) {
this.user = user;
}
}
我的JUnit測試報告是沒有錯誤的,但如果我測試它在我的Tomcat中,我發現了以下錯誤
例外[EclipseLink的-59](Eclipse持久服務 - 2.1.1.v20100817- r8050):org.eclipse.persistence.exceptions.DescriptorException 異常說明:實例變量[groupId]未在域類[de.kapieren.mbm.server.model.GroupMembership]中定義,或者無法訪問。 內部異常:java.lang.NoSuchFieldException:的groupId 映射:org.eclipse.persistence.mappings.DirectToFieldMapping [groupId-- > group_user.GROUPID] 描述符:RelationalDescriptor(de.kapieren.mbm.server.model.GroupMembership - > [DatabaseTable(GROUP_USER)])
異常[的EclipseLink-59](Eclipse持久服務 - 2.1.1.v20100817-r8050):org.eclipse.persistence.exceptions.DescriptorException 異常描述:實例變量[用戶id ]未在域類[de.kapieren.mbm.server.model.GroupMembership]中定義,或者它不可訪問。 內部異常:java.lang.NoSuchFieldException:用戶id 映射:org.eclipse.persistence.mappings.DirectToFieldMapping [userId-- > group_user.USERID] 描述符:RelationalDescriptor(de.kapieren.mbm.server.model.GroupMembership - > [DatabaseTable(GROUP_USER)])
異常[的EclipseLink-0](Eclipse持久服務 - 2.1.1.v20100817-r8050):org.eclipse.persistence.exceptions.DescriptorException 異常描述:內部錯誤期間,訪問主鍵對象[202]。 內部異常:java.lang.NoSuchFieldException:的groupId 描述:RelationalDescriptor(de.kapieren.mbm.server.model.GroupMembership - > [DatabaseTable(GROUP_USER)])
怎麼回事錯在這裏?
開始提示:當您更改用戶的用戶,會發生什麼 - >用戶用戶標識... – 2010-11-11 16:13:23
爲了完整起見,可您發佈的集團類爲好。 – 2010-11-11 16:14:22
集團類仍然存在 – LeonS 2010-11-11 16:31:48