java - @Column 中 nullable=true 的 Hibernate 处理

标签 java mysql hibernate jpa orm

我正在使用 Hibernate 和由 Mysql 数据库支持的 Springs。

这是类似于我用来输入的实体

@Entity
@Table(name = "my_table") {

    @Basic
    @Column(name = "my_string", nullable = false)
    private String myString;
}

sql定义是

CREATE TABLE `my_table` (
 `my_string` varchar(200) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

虽然该表允许空值,但根据 JPA 注释,列 myString 是不可空的。这会导致不可预测的行为。

问:在进行插入时是否始终在实体级别强制执行不可为空?或者有没有可能被忽略的情况

我的预期是所有参赛作品都应该被拒绝。但是通过这种设置,许多条目 (>7000) 已进入表中。 只有有时我从 spring 中得到一个 DataIntegrityViolation 异常

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: ...; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: ....
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:652)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58) 
    ....  
    ....
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: ....
    at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:103)
    ....

我知道在实体和表中使用单独的签名是一种不好的做法,但我正在尝试确定导致此行为的原因以及可能因此而遗漏的任何其他漏洞。

Versions -
Mysql- 5.5
Hibernate - 4.0.0
Hibernate-jpa - 2.0
Spring-core- 3.1.0
Spring-jdbc- 3.1.2

最佳答案

人们经常混淆@Column(nullable) and @NotNull annotations .

@Column(nullable) 用于生成的 DDL 脚本。但是你不使用 Hibernate 来生成你的 DDL,所以你根本不应该依赖它。你想要的是 @NotNull 这是一个运行时验证触发器。

如果您定义 Hibernate Validator Annotation(或 JPA 2.0 的)you can get the DDL for those as well :

Out of the box, Hibernate (as of version 3.5.x) will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity is annotated @NotNull, its columns will be declared as not null in the DDL schema generated by Hibernate.

如果您将 JPA 与 Hibernate 一起使用,请确保启用验证。

If you are using JPA 2 and Hibernate Validator is in the classpath the JPA2 specification requires that Bean Validation gets enabled. The properties javax.persistence.validation.group.pre-persist, javax.persistence.validation.group.pre-update and javax.persistence.validation.group.pre-remove as described in Section 10.1.2, “Hibernate event-based validation” can in this case be configured in persistence.xml. persistence.xml also defines a node validation-mode which can be set to AUTO, CALLBACK, NONE. The default is AUTO.

关于java - @Column 中 nullable=true 的 Hibernate 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23996149/

相关文章:

java - 如果 future 在循环内,Java 中的 ExecutorService 会如何表现

java - Spring Boot 管理员无法从 Eureka 获取注册表

javascript - 检查MySQL是否仍然连接或断开

mysql - 如何使用 JOIN 更新 SELECT 表中的字段

java - 在 Hibernate 中快速插入许多实体

java - 在没有 persistence.XML 的情况下使用 JPA 和 Spring 连接到 mysql 数据库

java - "Exception in thread "主要 "java.lang.ArrayIndexOutOfBoundsException: 178"错误,不知道为什么

php - SQL : How to sort query by date (DESC) while having rows with column specified value at top?

java - 如何使用HQL从实体中获取数据?

sql - 如何计算 hibernate 在一个 Grails 请求中执行了多少个 SQL 查询?