2011-02-06 38 views
3

我正試圖在兩個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) 

對於如何解決這個問題你有什麼想法嗎?謝謝你的幫助。

+0

[鏈接](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

回答

6

有幾件事情,

你的mappedBy是錯誤的,一個的mappedBy的一對多必須是多對一。我建議你添加ManyToOne,或者使用@JoinTable,如果你不想,那麼你可以使用@JoinColumn和沒有mappedBy。

向量在JPA中無效,必須使用List,Set或Map。你仍然可以使用Vector作爲你的實現類。如果你真的想使用Vector,那麼EclipseLink將支持這一點,但你必須使你的映射EAGER。

1

我想這是mappedBy註釋的問題。它應該是:「私人聊天室聊天室」在你的用戶類中。並在mappedBy中嘗試寫這個變量。 P.S.如果你的關係中沒有鏈接到CharRoom,那麼你肯定有連接表。那麼你應該使用@JoinTable註解和@OneToMany而不使用mappedBy。

+0

我嘗試了@JoinTable方法,但是現在我得到以下異常:`異常說明:屬性[users]未聲明爲類型ValueHolderInterface,但其映射使用間接。 映射:org.eclipse.persistence.mappings.UnidirectionalOneToManyMapping [用戶]` – 2011-02-06 12:34:31