休眠一对一,插入时,为什么FK为空



当我运行此代码时,它运行时没有错误。但当我检查这些值时,正如你所看到的,在";Tbl_讲师细节";parentId为null的表有人能帮忙吗。非常感谢。

这是我的实体和具有表关系的主类在此处输入图像描述

这是我数据库中的表格

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中引用的。在主类中,外键传递null,因为您可以在保存父表之前引用父表。

下面是修改后的代码:

实体

@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();

最新更新