java - 异常描述 : An incompatible mapping has been encountered between [class sws.Entity.Appointment] 和 [class sws.entities.User]

标签 java entity-framework jpa

大家好,我的多对多映射遇到了一个重大问题

我收到错误

Internal Exception: Exception [EclipseLink-7244] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: An incompatible mapping has been encountered between [class sws.entities.Appointment] and [class sws.entities.User]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer.
Local Exception Stack: 
Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException

这是我的实体

@Entity
public class Appointment implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Temporal(TemporalType.TIMESTAMP)
    private Date startDatetime, endDatetime;
    private String appointmentDescription;
    @OneToOne(mappedBy = "username")
    private User owner;
    @ManyToMany(mappedBy = "appointments")
    private Collection<User> attendees;

@Entity(name = "USERS")
public class User implements Serializable {

    @ManyToOne
    private Address address;
    private static final long serialVersionUID = 1L;
    private String firstName, surname, telephoneNumber, emailAddress, gender;
    @Id
    @Column(name = "USER_NAME")
    private String username;
    @Column(name = "PASSWD", length = 32, columnDefinition = "VARCHAR(32)")
    private String password;
    @OneToOne(mappedBy = "user")
    private Role role;
    @Temporal(TemporalType.DATE)
    private Date DateOfBirth;

    @ManyToMany
    private Collection<Appointment> appointments;

我谷歌了一下,找不到为什么这样做,有什么想法吗?

最佳答案

我认为真正的问题在这里:

@OneToOne(mappedBy = "username")
private User owner;

mappedBy 用于连接双向关系的不同方(例如attendeesappointments)。 owner 是单向关系,因此这里不需要 mappedBy:

@OneToOne
private User owner;

关于java - 异常描述 : An incompatible mapping has been encountered between [class sws.Entity.Appointment] 和 [class sws.entities.User],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20421534/

相关文章:

JAVA - 获取类的通用实现

java - 单击 JButton 时动态添加组件到 JPanel

java - 重复加载和保存相同的图像到文件系统会改变图像的数据

hibernate - 了解 Hibernate/JPA 对 findAll 和 findOne 的不同行为

java - 如何引发 OptimisticLockException

c# - GAE 上的 RoR?

c# - 在不破坏已发布站点的情况下,代码优先 EF 中更新数据库模型的工作流程是什么?

c# - 处理对数据库的重复 linq 查询的正确做法应该是什么?

c# - 事务(进程 ID xx)在锁定资源上与另一个进程发生死锁,并已被选为死锁牺牲品。重新运行交易

java - JPA 桌面应用程序和远程 MySQL - 如何