大家好,我的多对多映射遇到了一个重大问题
我收到错误
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
用于连接双向关系的不同方(例如attendees
和appointments
)。 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/