我正試圖在兩個JPA實體之間生成一個單向的一對多映射。在我的ChatRoom
課程中,我有一個Vector<User>
類型的對象,代表User
s在ChatRoom
中。我試圖根據userId
(在User
類中)創建一對多映射。下面是我的示例代碼:EclipseLink/JPA @OneToMany Backpointer基數錯誤
@Entity
public class ChatRoom {
@Id
@GeneratedValue
private int chatRoomID;
@OneToMany(mappedBy="userID")
private Vector<User> users;
// A no-argument constructor is required for EclipseLink to instantiate the persistence object properly.
public ChatRoom() {}
// Getter/Setter section
}
這裏是用戶等級:
@Entity
public class User {
@Id
@GeneratedValue
private int userID;
private String username;
// Getter/Setter section
}
當我嘗試生成在Eclipse這些實體的表,我得到以下錯誤:
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [ChatJPA] failed.
Internal Exception: Exception [EclipseLink-7244] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.ValidationException
Exception Description: An incompatible mapping has been encountered between [class iosoa.entity.ChatRoom] and [class iosoa.entity.User]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer.
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
對於如何解決這個問題你有什麼想法嗎?謝謝你的幫助。
[鏈接](http://stackoverflow.com/questions/3515756/unidirectional-relationship-in-entity-bean-jpa)包含如何使用@JoinTable的答案,但在我的情況下,我得到以下錯誤:`異常描述:屬性[用戶]沒有聲明爲類型ValueHolderInterface,但其映射使用間接。映射:org.eclipse.persistence.mappings.UnidirectionalOneToManyMapping [users]` – 2011-02-06 12:40:54