【问题标题】:NotNull not working, Column(nullable = false) workingNotNull 不工作,Column(nullable = false) 工作
【发布时间】:2014-09-11 05:59:47
【问题描述】:

我有一个 Spring 实体,其字段用 javax.validation.constraints 中的 @NotNull 注释

@Entity
public abstract class IdentifiableNamedEntity {
    @NotNull
    @Column(unique = true)
    private String name;
}

问题是,如果为 name 字段设置 null 值,它会存储在数据库中。但是,如果我按以下方式更改课程,则会引发我希望收到的异常:

@Entity
public abstract class IdentifiableNamedEntity {
    @Column(unique = true, nullable=false)
    private String name;
}

有没有一种方法可以避免指定 nullable=false,但让 @NotNull 表现得如我所愿?是否有任何依赖于标准 Java 注释的 nullable=false 替代方法,例如一些 Hibernate 配置?

这是我的弹簧配置:

ApplicationContext

<beans ...>
<context:property-placeholder location="classpath*:spring/database.properties" />
<context:component-scan base-package="com.lh.clte" />
<import resource="classpath:spring/applicationContext-persistence.xml" />
</beans>

applicationContext-persistence

<beans ...>
<import resource="classpath:spring/applicationContext-jpa.xml" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
    <property name="initialSize" value="3" />
    <property name="maxActive" value="10" />
</bean>

<tx:annotation-driven mode="proxy"
    transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
    id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="dataSource" ref="dataSource" />
</bean>
</beans>

applicationContext-jpa

<beans ...>
<jpa:repositories base-package="com.lh.clte.repository" />
</beans>

由于我使用的是存储库,因此我还报告了对应的实体存储库

@Repository
public interface IdentifiableNamedEntityRepository extends JpaSpecificationExecutor<IdentifiableNamedEntity>, JpaRepository<IdentifiableNamedEntity, Long> {
}

【问题讨论】:

    标签: java spring jpa notnull


    【解决方案1】:

    @NotNull 是一个 JSR 303 Bean 验证注解。它与数据库约束本身无关。此注释用于验证。 @Column(nullable = false) 是声明一列不为空的方式。最后一个注释用于指示数据库架构详细信息

    【讨论】:

    • 是的,我明白这一点,但我也希望 Hibernate 使用该 java-validation-level 信息来得出该字段在数据库中不应为空。至少,即使这不是默认行为,我希望它是可配置的。
    • 您是否拥有所有必需的库才能让 Hibernate 验证器正常工作。 hibernate.org/validator/documentation/getting-started
    • 不,这是错误的:我添加了 hibernate-validator,版本 4.2.0.Final,它起作用了。谢谢
    • 您也可以使用@Valid @NotNull 。当应用@Valid 时,JPA 验证会自动启动以验证字段/列。
    【解决方案2】:

    您也可以使用 Hibernate Validator 的@NotEmpty

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      • 2015-05-18
      • 2016-01-16
      • 2018-01-08
      • 2011-02-23
      • 2016-10-04
      相关资源
      最近更新 更多