我有两个类,一个包含在另一个中。SchoolClass和学生当在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));发布于 2020-09-22 02:23:24
异常NULL not allowed for column "schoolClassId"明确指出schoolClassId不能为空。
这是nullable = false属性,它将对列schoolClassId强制执行not null约束,该约束可以在学生创建表中转换为schoolClassId bigint NOT NULL。
schoolClassId列上的insertable=true表示该列包含在插入查询中。因此,只要持久化了SchoolClass的实例,相关的学生实例也会被持久化。学生实体插入将包括SchoolClassId列,它的值引用SchoolClass id的实例,在本例中不为null。
因此,简而言之,只要列schoolClassId为null,就会抛出违反约束的情况,因此在保持空值的情况下,如果必须消除这种违反,则需要设置insertable=false = true。
https://stackoverflow.com/questions/63996012
复制相似问题