【发布时间】:2022-01-21 18:39:33
【问题描述】:
当我运行此代码时,它运行时没有错误。但是当我检查值时,如您所见,在“Tbl_InstructorDetail”表中,parentId 为空 谁能帮忙。 谢谢。
这是我的实体和我的主要类与表关系 enter image description here
这是我数据库中的表格
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();
【问题讨论】: