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>
The problem is disappeared after making following steps:
1. migrate libary versions:
hibernate-core
: 3.3.2.GA -> 4.3.10.Finalhibernate-annotations
: 3.3.1.GA -> 3.5.6-Finalhibernate-commons-annotations
: 3.3.0.ga -> 3.2.0.Finalhibernate-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
… 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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With