Hibernate一对一,插入时,为什么FK为null

Hibernate One-to-One, When inserting, Why FK is null

当我运行这段代码的时候,是运行出来的错误。但是当我检查值时,如您所见,在“Tbl_InstructorDetail”table 中,parentId 为空 谁能帮忙。 谢谢。

这是我的实体和我的主要 class 与 table 关系 enter image description here

这是我的 table 来自我的数据库

create table Tbl_Instructor
(
    uuid  int identity
        constraint Pk_Tbl_Instructor_uuid
            primary key,
    Title nvarchar(50)
)

create table Tbl_InstructorDetail
(
    uuid       int identity
        constraint Pk_Tbl_InstructorDetail_uuid
            primary key,
    Created_By nvarchar(50),
    parentId   int
        constraint Fk_Tbl_InstructorDetail_Tbl_Instructor
            references Tbl_Instructor
)
@Entity
@Table(name = "Tbl_InstructorDetail", schema = "dbo", catalog = "OJT_2021_KST")
public class TblInstructorDetailEntity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "uuid", nullable = false)
    private int uuid;
    @Basic
    @Column(name = "Created_By", nullable = true, length = 50)
    private String createdBy;
    @Basic
    @Column(name = "parentId", nullable = true,insertable = false,updatable = false)
    private Integer parentId;

    @OneToOne
    @JoinColumn(name = "parentId",referencedColumnName="uuid")
    private TblInstructorEntity instructorEntity;
@Entity
@Table(name = "Tbl_Instructor", schema = "dbo", catalog = "OJT_2021_KST")
public class TblInstructorEntity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "uuid", nullable = false)
    private int uuid;
    @Basic
    @Column(name = "Title", nullable = true, length = 50)
    private String title;

    @OneToOne(mappedBy="instructorEntity",cascade = CascadeType.ALL)
    private TblInstructorDetailEntity detailEntity;

Main class

            TblInstructorEntity instructor = new TblInstructorEntity();
            instructor.setTitle("This is a Test");

            TblInstructorDetailEntity detail = new TblInstructorDetailEntity();
            detail.setCreatedBy("Kyle");

            instructor.setDetailEntity(detail);

            session.getTransaction().begin();
            session.save(instructor);
            session.getTransaction().commit();

您不需要在 TblInstructorDetailEntity 中添加 parentId,因为它引用自 TblInstructorEntity。在主 class 外键中传递 null 因为您可以在保存父 table.

之前引用父 table

下面是修改后的代码:

实体

@Entity
@Table(name = "Tbl_InstructorDetail", schema = "dbo", catalog = "OJT_2021_KST")
public class TblInstructorDetailEntity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "uuid", nullable = false)
    private int uuid;
    @Basic
    @Column(name = "Created_By", nullable = true, length = 50)
    private String createdBy;
    
    // remove parentId column because it is foreign key

    @OneToOne
    @JoinColumn(name = "parentId",referencedColumnName="uuid")
    private TblInstructorEntity instructorEntity;

    // getter setter
}

@Entity
@Table(name = "Tbl_Instructor", schema = "dbo", catalog = "OJT_2021_KST")
public class TblInstructorEntity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "uuid", nullable = false)
    private int uuid;
    @Basic
    @Column(name = "Title", nullable = true, length = 50)
    private String title;

    @OneToOne(mappedBy="instructorEntity",cascade = CascadeType.ALL)
    private TblInstructorDetailEntity detailEntity;

    // getter setter
}

主要

Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();

TblInstructorEntity instructor = new TblInstructorEntity();
instructor.setTitle("This is a Test");

TblInstructorDetailEntity detail = new TblInstructorDetailEntity();
detail.setCreatedBy("Kyle");

session.save(instructor); // Save parent entity

detail.setInstructorEntity(instructor); // Reference from parent entity

session.save(detail); // Save child entity

session.getTransaction().commit();
HibernateUtil.shutdown();