Home > Mobile >  Hibernate One-to-One, When inserting, Why FK is null
Hibernate One-to-One, When inserting, Why FK is null

Time:12-21

When I run this code, it is running with out error. But When I check the values, as you can see, In the "Tbl_InstructorDetail" table the parentId is null can anyone help. thank you.

This is my Entities and my main class with table relation enter image description here

this is my tables from my database

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

CodePudding user response:

You don't need to add parentId in TblInstructorDetailEntity because it's referenced from TblInstructorEntity. In main class foreign key pass null because you can take a reference to the parent table before save parent table.

Here down is modified code:

Entity

@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
}

Main

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();
  • Related