EclipseLink/JPA @OneToMany Backpointer基数错误

Nic*_*uiz 4 persistence jpa one-to-many eclipselink

我试图在两个JPA实体之间生成单向一对多映射.在我的ChatRoom课堂上,我有一个Vector<User>表示Users中的s 的类型对象ChatRoom.我正在尝试基于userId(在User类中)创建一对多映射.这是我的示例代码:

@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
}
Run Code Online (Sandbox Code Playgroud)

这是User类:

@Entity
public class User {
    @Id
    @GeneratedValue
    private int userID;

    private String username;

    // Getter/Setter section
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在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)
Run Code Online (Sandbox Code Playgroud)

您对如何解决此问题有任何想法吗?谢谢你的帮助.

Jam*_*mes 7

一些东西,

您的mappedBy错误,OneToMany的mappedBy必须是ManyToOne.我建议您添加ManyToOne,或使用@JoinTable,如果您不希望,那么您可以使用@JoinColumn而不使用mappedBy.

Vector在JPA中无效,您必须使用List,Set或Map.您仍然可以使用Vector作为实现类.如果你真的想使用Vector,那么EclipseLink将支持这一点,但你必须使你的映射EAGER.