當我運行此代碼時,它運行沒有錯誤。但是當我檢查值時,如您所見,在“Tbl_InstructorDetail”表中,parentId 為空,任何人都可以提供幫助。謝謝。
這是我的物體和我的主類與表關系 在此處輸入影像描述
這是我的資料庫中的表
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();
uj5u.com熱心網友回復:
您不需要添加parentId,TblInstructorDetailEntity因為它是從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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/388095.html
