6

I have a Spring Entity with a field annotated with @NotNull from javax.validation.constraints

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

The issue is that if a set a null value for the name field, it gets stored in the database. However, if I change the class as follow, it raises the exception I would like to receive:

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

Is there a way I can avoid to specify nullable=false, but getting @NotNull to behave as I would like? Is there any alternative to nullable=false that relies on standard Java annotations, e.g. some Hibernate configuration?

This is my spring configuration:

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>

Since I am using repositories, I also report the corrisponding entity repository:

@Repository
public interface IdentifiableNamedEntityRepository extends JpaSpecificationExecutor<IdentifiableNamedEntity>, JpaRepository<IdentifiableNamedEntity, Long> {
}
Manu
  • 4,019
  • 8
  • 50
  • 94

2 Answers2

8

@NotNull is a JSR 303 Bean Validation annotation. It has nothing to do with database constraints itself. This annotation is intended for validation. @Column(nullable = false) is the way of declaring a column to be not-null. This last annotation is intended for indicating database schema details

  • Yes, I understand that, but I would also expect that Hibernate used that java-validation-level information to derive that the field should not be null in the database. At least, even though this is not a default behavior, I would expect that to be configurable. – Manu Jul 20 '14 at 17:45
  • 2
    Do you have all required libraries in order to get working properly the Hibernate Validator. http://hibernate.org/validator/documentation/getting-started/ –  Jul 20 '14 at 17:48
  • 2
    no, that was the mistake: I added hibernate-validator, version 4.2.0.Final, and it worked. Thanks – Manu Jul 20 '14 at 18:01
  • You can also use `@Valid @NotNull `. When `@Valid` is applied JPA validation automatically kicks in to validate the field / column. – vijay Sep 05 '18 at 06:49
0

You can also use @NotEmpty of Hibernate Validator.

pms
  • 944
  • 12
  • 28