我正在尝试制作复合主键映射,但不起作用。
必要条件是:
$folder = preg_replace('/\.php$/', '', $file);
注释@IdClass
我的代码:
@ManyToOne
他们正试图坚持下去:
@Entity
@IdClass(PhonePK.class)
public class Phone implements Serializable{
@Id
@Column(name = "codigo", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long code;
@Id
@ManyToOne
@JoinColumn(name = "person", referencedColumnName = "code", nullable = false)
private Person person;
@Basic
@Column(name = "number", nullable = false)
private Integer number;
//getters and setters
//equals and hashcode
}
public class PhonePK implements Serializable {
private Long code;
private Long person;
public PhonePK(){}
public PhonePK(Long code, Long person) {
this.code = code;
this.person = person;
}
//getters and setters
//equals and hashcode
}
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "code", nullable = false)
private Long code;
//getters and setters
//equals and hashcode
}
我收到的错误是:
引起:javax.persistence.PersistenceException:org.hibernate.HibernateException:复合标识符的任何部分都不能为空
答案 0 :(得分:1)
您在电话实体上设置的人很可能还没有ID。坚持不会garante和身份证。建议您也刷新交易。
答案 1 :(得分:0)
我在hibernate.atlassian中发现了一个问题。我放弃了复合键,只需将Person
中的外键放入Phone
。