我正在尝试在子表“contact”中插入person的id。但是hibernate在fk列中存储值null。
我执行dto到实体的Map,在实体中它已经带来了个人的数据和联系人。最后我必须拯救这个人。
有一个表继承!
父表:
@Entity
@Table
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, columnDefinition = "CHAR(2)", length = 2)
public abstract class Person implements Serializable {
@OneToMany(fetch = FetchType.EAGER, mappedBy = "person")
@Cascade(value={org.hibernate.annotations.CascadeType.ALL})
private @Getter @Setter Set<Contact> contacts;
}
人员表:
@Entity
@Table
@DiscriminatorValue(value="PJ")
public class Company extends Person implements Serializable {
@Column(nullable = false, columnDefinition = "DATE")
private @Getter @Setter LocalDate constitutionDate;
}
问题出在哪里!
@Entity
@Table(name = "contact")
@EqualsAndHashCode
public class Contact implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private @Getter Integer id;
@Column(columnDefinition = "VARCHAR(16)", length = 16, nullable = false)
private @Getter @Setter String phoneNumber;
@ManyToOne(fetch = FetchType.LAZY, optional = false,targetEntity=Person.class)
@JoinColumn(name = "person_id", nullable = false, referencedColumnName="id")
private @Getter @Setter Person person;
public Contact() {}
public Contact(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
我做错什么了?最好的策略是什么?
对不起,我英语不好!
1条答案
按热度按时间r1wp621o1#
找不到明显的错误,但让我试试:
person类上的级联注解不需要特定的hibernate枚举,您可以使用如下方法:
在联系人类的manytone注解中,请尝试添加:
顺便说一句,在我看来,个人和公司之间的继承关系似乎不合逻辑,但这肯定与你所说的问题无关。