Hibernate 5.3.10的one-to-many,insertable = false与Hibernate 5.2.1的行为不同。

huangapple go评论57阅读模式
英文:

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 -"schoolClassId"不允许为空SQL 语句
insert into tStudent (studentId, name) values (null, ?)
org.hibernate.exception.ConstraintViolationException: 无法执行语句
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: 无法执行语句
@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));
英文:

I have two classes one contained within the other. SchoolClass and Student
When persisting them in Hibernate 5.2.1 everything works as expected, but when persisting in Hibernate 5.3.10 I have to remove or set insertable = trueto get the same result otherwise I get exception.

What I'm looking for is a confirmation that the behavior of hibernate has changed. When where and why...

I have not been able to find any documentation about this at all.

jdbc.spi.SqlExceptionHelper - NULL not allowed for column &quot;schoolClassId&quot;; 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 = &quot;tSchoolClass&quot;)
@AutowiringTarget
public class SchoolClass {

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


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

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

H2 database.

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

答案1

得分: 1

异常 NULL not allowed for column "schoolClassId" 明确表示 schoolClassId 不能为空。

nullable = false 属性强制在列 schoolClassId 上执行非空约束,它可以翻译为在学生创建表中的 schoolClassId bigint NOT NULL

insertable=trueschoolClassId 列上意味着该列包含在插入查询中。因此,每当 SchoolClass 的实例被持久化时,关联的 Student 实例也将被持久化。学生实体的插入将包括 SchoolClassId 列,其值引用 SchoolClass id 的实例,在这种情况下是非空的。

因此,简而言之,任何时候列 schoolClassId 为 null,都将抛出约束违规异常,因此如果要摆脱违规,保持 insertable=false,则需要将 nullable = true

英文:

The exception NULL not allowed for column &quot;schoolClassId&quot; is clearly saying schoolClassId cannot be null.

Its the nullable = false property, that would enforce the not null constraint on the column schoolClassId which can be translated to schoolClassId bigint NOT NULL in the student create table.

The insertable=true on schoolClassId column would mean the column is included in the insert query. So whenever an instance of SchoolClass is persisted, the associated Student instances will be persisted too. The student entity insert will include the SchoolClassId column , its value referencing to SchoolClass id's instance, which is not null in this case.

So in short, anytime the column schoolClassId is null, the constraint violation will be thrown, so keeping insertable=false, you would need to set nullable = true if you have to get rid of the violation.

huangapple
  • 本文由 发表于 2020年9月22日 00:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63996012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定