Hibernate 5.3.10一对多insertable=false的行为与Hibernate 5.2.1不同



我有两个类,一个包含在另一个中。学校班级和学生当在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列强制执行非null约束,该列可以在学生创建表中转换为schoolClassId bigint NOT NULL

schoolClassId列上的insertable=true意味着该列包含在插入查询中。因此,每当SchoolClass的实例被持久化时,关联的Student实例也将被持久化。学生实体插入将包括SchoolClassId列,该列的值引用了SchoolClassId的实例,在本例中该实例不为null。

简而言之,只要列schoolClassId为null,就会抛出约束冲突,因此保持insertable=false,如果必须消除冲突,则需要设置nullable=true。

最新更新