簡體   English   中英

Hibernate 5.3.10 一對多可插入 = false 的行為不同於 Hibernate 5.2.1

[英]Hibernate 5.3.10 one-to-many insertable = false behaves differently from Hibernate 5.2.1

我有兩個類,一個包含在另一個中。 SchoolClass 和 Student 在 Hibernate 5.2.1 中保留它們時,一切都按預期工作,但在 Hibernate 5.3.10 中保留時,我必須刪除或設置insertable = true以獲得相同的結果,否則我會出現異常。

我正在尋找的是確認 hibernate 的行為已更改。 何時何地為什么...

我根本找不到任何關於此的文檔。

jdbc.spi.SqlExceptionHelper - NULL not allowed for column "schoolClassId"; SQL statement:
insert into tStudent (studentId, name) values (null, ?)
org.hibernate.exception.ConstraintViolationException: could not execute statement
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement.
@Entity
@Table(name = "tSchoolClass")
@AutowiringTarget
public class SchoolClass {

   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "schoolClassId")
    private Long id;


    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "schoolClassId", nullable = false, insertable = false, updatable = false)
    private List<Student> students;
@Entity
@Table(name = "tStudents")
@AutowiringTarget
public class Students {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "StudentId")
    private Long id;

H2數據庫。

CREATE TABLE tSchoolClass (
    schoolClassId int IDENTITY(1,1) NOT NULL,
    CONSTRAINT PK_tSchoolClass PRIMARY KEY (schoolClassnId));

CREATE TABLE tStudents (
    studentId  int IDENTITY(1,1) NOT NULL,
    schoolClassint NOT NULL,
    

    CONSTRAINT PK_tStudents PRIMARY KEY (studentId),
    CONSTRAINT FK_tStudent_tSchoolClass FOREIGN KEY (schoolClassId) REFERENCES tSchoolCLass (SchoolClassId));

NULL not allowed for column "schoolClassId"明確表示schoolClassId不能是 null。

它的nullable = false屬性將對列 schoolClassId 強制執行 not null 約束,該約束可以在學生創建表中轉換為schoolClassId bigint NOT NULL

schoolClassId列上的insertable=true意味着該列包含在插入查詢中。 因此,只要 SchoolClass 的實例被持久化,關聯的 Student 實例也將被持久化。 學生實體插入將包括 SchoolClassId 列,其值引用 SchoolClass id 的實例,在本例中不是 null。

所以簡而言之,只要列 schoolClassId 為 null,就會拋出約束違規,因此保持 insertable=false,如果必須消除違規,則需要設置 nullable = true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM