我正在尝试在子表'Contact'中插入Person的id。但是Hibernate在fk列存储值null。
我执行DTO到实体的映射,它已经带来了人的数据和联系人。最后,我必须保存人员实体。
有一个表继承!
父表:
@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;
}
}
我做错了什么?什么是最好的策略?
对不起我的英语很差!
答案 0 :(得分:2)
找不到任何明显的错误,但让我尝试一些事情:
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, mappedBy = "cliente")
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
BTW,Person和Company之间的继承关系对我来说似乎不合逻辑,但这肯定与你陈述的问题无关。