【问题标题】:Hibernate One-to-One, When inserting, Why FK is nullHibernate一对一,插入时,为什么FK为null
【发布时间】: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();

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    您不需要在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();
    

    【讨论】:

    • 谢谢,这个问题困扰了我好几天。终于完成了。非常感谢。顺便说一下一对多和多对一,基本上是一样的吧?
    • 对不起,我还需要 3 个声望。当我再获得 3 个声望时,我会支持你的回答,我保证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多