【发布时间】:2018-06-25 13:33:05
【问题描述】:
我正在尝试在子表“联系人”中插入 Person 的 ID。但是 Hibernate 将值 null 存储在 fk 列。
我执行 DTO 到实体的映射,它已经将人员的数据和联系人带到了该实体。最后我必须保存 person 实体。
有一个表继承!
父表:
@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;
}
}
我做错了什么?最好的策略是什么?
对不起,我的英语不好!
【问题讨论】:
标签: mysql hibernate spring-boot spring-data