gpt4 book ai didi

jpa - EclipseLink/JPA @OneToMany 反向指针基数错误

转载 作者:行者123 更新时间:2023-12-04 15:47:25 26 4
gpt4 key购买 nike

我正在尝试在两个 JPA 实体之间生成单向一对多映射。在我的 ChatRoom类(class),我有一个 Vector<User>表示 User 的类型对象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
}

这是用户类:
@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)

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

最佳答案

一些东西,

您的mappedBy 是错误的,OneToMany 的mappedBy 必须是ManyToOne。我建议您将 ManyToOne 添加回来,或者使用 @JoinTable,如果您不希望,那么您可以使用 @JoinColumn 而没有 mappingBy。

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

关于jpa - EclipseLink/JPA @OneToMany 反向指针基数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4912868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com