异常描述:[class sws.entities.Appointment]和[class sws.entities.User]之间遇到不兼容的映射

时间:2013-12-06 10:25:42

标签: 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;

我做了一个谷歌,但无法找出为什么这样做,任何想法?

1 个答案:

答案 0 :(得分:2)

我认为真正的问题在于:

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

mappedBy用于连接双向关系的不同方面(例如attendeesappointments)。 owner是一种单向关系,因此您不需要mappedBy

@OneToOne
private User owner;