hibernate,spring数据,在子表上插入父表的id

mv1qrgav  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(344)

我正在尝试在子表“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;
    }
}

我做错什么了?最好的策略是什么?
对不起,我英语不好!

r1wp621o

r1wp621o1#

找不到明显的错误,但让我试试:
person类上的级联注解不需要特定的hibernate枚举,您可以使用如下方法:

@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, mappedBy = "cliente")

在联系人类的manytone注解中,请尝试添加:

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })

顺便说一句,在我看来,个人和公司之间的继承关系似乎不合逻辑,但这肯定与你所说的问题无关。

相关问题