大家好,我的多对多映射存在重大问题
我收到错误
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;
我做了一个谷歌,但无法找出为什么这样做,任何想法?
答案 0 :(得分:2)
我认为真正的问题在于:
@OneToOne(mappedBy = "username")
private User owner;
mappedBy
用于连接双向关系的不同方面(例如attendees
和appointments
)。 owner
是一种单向关系,因此您不需要mappedBy
:
@OneToOne
private User owner;