我有以下实体,并使用hibernate 4.1。*作为JPA提供程序,但在customer表中使用映射到用户的复合主键给了我 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:键'PRIMARY'的重复条目
实体映射如下。
@Entity
class Customer {
@EmbeddedId CustomerId id;
boolean preferredCustomer;
@MapsId("userId")
@JoinColumns({
@JoinColumn(name = "first_name_", referencedColumnName = "first_name"),
@JoinColumn(name = "last_name", referencedColumnName = "last_name")
})
@ManyToOne(optional = false)
private User user;
-----
}
@Embeddable
private UserPK userId;
@Basic(optional = false)
@NotNull
@Column(name = "customer_number")
private int customerNumber;
----
}
@Entity
class User {
@EmbeddedId UserId id;
@Basic(optional = false)
@NotNull
@Column(name = "age")
private int age;
------
}
@Embeddable
class UserId implements Serializable {
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "first_name")
private String firstName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "last_name")
private String lastName;
-------
}
相同的映射与eclipselink一起使用。如果我从CustomerId中删除UserId并引用它,它也可以工作,但我想用hibernate和@MapsId来解决这个问题
我按照JPA 2 - Foreign key that only includes one field from a composite primary key?中的建议尝试使用不同的级联选项,但没有运气。