【问题标题】:EclipseLink/JPA @OneToMany Backpointer Cardinality ErrorEclipseLink/JPA @OneToMany 反向指针基数错误
【发布时间】:2011-06-22 05:20:35
【问题描述】:

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

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

【问题讨论】:

  • link 包含如何使用@JoinTable 执行此操作的答案,但在我的情况下,我收到以下错误:Exception Description: The attribute [users] is not declared as type ValueHolderInterface, but its mapping uses indirection. Mapping: org.eclipse.persistence.mappings.UnidirectionalOneToManyMapping[users]

标签: jpa persistence eclipselink one-to-many


【解决方案1】:

一些事情,

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

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

【讨论】:

    【解决方案2】:

    我猜这是 mappedBy 注释的问题。应该是:“私人聊天室聊天室;”在您的用户类中。并在 mappedBy 中尝试写入此变量。 附言如果您的关系中没有指向 CharRoom 的链接,那么您肯定有连接表。那么你应该使用@JoinTable 注解和@OneToMany 而不使用mappedBy。

    【讨论】:

    • 我尝试了@JoinTable 方法,但现在出现以下异常:Exception Description: The attribute [users] is not declared as type ValueHolderInterface, but its mapping uses indirection. Mapping: org.eclipse.persistence.mappings.UnidirectionalOneToManyMapping[users]
    • @damular,我使用了@JoinColumn(name="user_id", referencedColumnName="chatroom_id") 并将名称分配给了我的 ID。问题是,如果您不使用ListMapSet,那么您必须指定自己的间接寻址。我还不知道该怎么做。
    • 这里是一个员工和电话的例子:@Entity public class Employee { @Id private int id;私有字符串名称; @OneToMany @JoinTable(name="EMP_PHONE", joinColumns=@JoinColumn(name="EMP_ID"), inverseJoinColumns=@JoinColumn(name="PHONE_ID")) 私人收藏 电话; // ... }
    猜你喜欢
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多