Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate validation doesn't happen on persist in database

I have the following session factory configuration:

     <session-factory>
        <property name="javax.persistence.validation.group.pre-persist">javax.validation.groups.Default</property>
        <property name="javax.persistence.validation.group.pre-update">javax.validation.groups.Default</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.showSql">true</property>
        <property name="hbm2ddl.auto">validate</property>
        <mapping class="com.terminal.domain.Terminal"/>
        ...
    </session-factory>

Terminal class:

@Entity
@Table(name = "terminal")
public class Terminal {
    @Column(name = "cost")
    @Min(100)
    private Long cost;
    // get and set
}

I have the following code in my service method:

Terminal terminal = new Terminal();
terminal.setCost(98L);
session.save(terminal);

When I invoke it new row adds in terminal table.
expected result: validation exception and new row doesn't add in terminal table.

Why does actual result differs from expected ?

P.S.

As I understood it should work out of the box http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-checkconstraints.html#validator-checkconstraints-orm-hibernateevent

P.P.S.

hibernate stuff dependencies:

       <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.2.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.3.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.3.0.ga</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>

RESOLUTION

The problem is disappeared after making following steps:

1. migrate libary versions:

hibernate-core: 3.3.2.GA -> 4.3.10.Final
hibernate-annotations: 3.3.1.GA -> 3.5.6-Final
hibernate-commons-annotations: 3.3.0.ga -> 3.2.0.Final
hibernate-validator: 4.2.0.Final -> 5.1.3.Final

removed dependency:

<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0</version>
</dependency>

2. replaced in configuration

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/>
</bean

with

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/>
 </bean

3. and for session factory:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation">
            <value>classpath:hibernate-test.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hbm2ddl.auto">create-drop</prop>
            </props>
        </property>
</bean>

with

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation">
            <value>classpath:hibernate-test.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hbm2ddl.auto">create-drop</prop>
            </props>
        </property>
    </bean>

4. session factory configuration:

<session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.showSql">true</property>
        <property name="hbm2ddl.auto">validate</property>
        ...//classes mapping
</session-factory>

Actually I didn't understand which concrete step resolved my problem but in complex it is really works

like image 926
gstackoverflow Avatar asked May 19 '15 21:05

gstackoverflow


People also ask

How does hibernate validation work?

… The validation gets automatically executed when Hibernate, or any other JPA implementation, triggers a pre-persist, pre-update or pre-remove lifecycle events. The pre-persist and pre-update events trigger the validation of the default validation group which contains all validation constraints.

How to validate pre-persist and pre-update events in hibernate?

Hibernate will automatically trigger the validation on pre-persist and pre-update events so that you only store valid information in your database. Thorben is an independent consultant, international speaker, and trainer specialized in solving Java persistence problems with JPA, Hibernate and Spring Data JPA.

What is hibernate persist in JPA?

Hibernate Persist is used to save an entity or a record. There are other methods such as save and update. The persist method is used to change the state of transient entity form to a persisted or managed state. Persist is the method offered by EntityManager of JPA and is also inherited by hibernate Session.

How to use beanvalidation with Hibernate?

BeanValidation provides a set of easy to use standard validations which you can use with Hibernate and all other JPA 2.x implementations. The only things you have to do are to add the required dependencies to your application’s classpath and apply the validation annotations to your entities.


1 Answers

The configuration properties you are referencing apply to JPA. If you are using the JPA API it should work. It looks like that you are using the native Hibernate ORM Session API. You need to use the EntityManager API.

like image 169
Hardy Avatar answered Nov 04 '22 02:11

Hardy