数据库

时间:2015-05-19 21:55:25

标签: java spring hibernate persistence hibernate-validator

我有以下会话工厂配置:

     <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>

终端课程:

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

我的服务方法中有以下代码:

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

当我调用它时,新行添加在terminal表中 预期结果:验证异常和新行未添加到terminal表中。

为什么实际结果与预期不同?

P.S。

据我所知,它应该开箱即用 http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-checkconstraints.html#validator-checkconstraints-orm-hibernateevent

P.P.S。

hibernate stuff依赖项:

       <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>

解决方案

执行以下步骤后,问题就消失了:

1。迁移libary版本:

hibernate-core:3.3.2.GA - &gt; 4.3.10.Final
hibernate-annotations:3.3.1.GA - &gt; 3.5.6决赛
hibernate-commons-annotations:3.3.0.ga - &gt; 3.2.0.Final
hibernate-validator:4.2.0.Final - &gt; 5.1.3.Final

删除了依赖:

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

中替换了

2。

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

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

3。和会话工厂:

<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>

<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>
        <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>

实际上我不明白哪个具体步骤解决了我的问题,但复杂的是它确实有效

5 个答案:

答案 0 :(得分:1)

您引用的配置属性适用于JPA。如果您使用的是JPA API,它应该可以使用。看起来您正在使用本机Hibernate ORM会话API。您需要使用EntityManager API。

答案 1 :(得分:1)

If you're using JPA, you need to add the following EntityManager listeners:

<event type="pre-update">
    <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>
<event type="pre-insert">
    <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>
<event type="pre-delete">
    <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>

And also add the following property:

<property name="javax.persistence.validation.mode">
  ddl, callback
</property>

If you are using Hibernate specific configuration, with hibernate.cfg.xml, then the configs become:

 <event type="pre-update">
   <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>
<event type="pre-insert">
    <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>
<event type="pre-delete">
    <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>

答案 2 :(得分:1)

您需要使用Hibernate验证器。 这里是maven神器

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>

这里是入门文档。

http://hibernate.org/validator/documentation/getting-started/

答案 3 :(得分:1)

您必须使用@Range包中提供的 org.hibernate.validator.constraints 注释。

https://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/constraints/Range.html

import org.hibernate.validator.constraints.Range;
...

@Entity
@Table(name = "terminal")
public class Terminal {
    @Column(name = "cost")
    @Range(min=100, message="Should be of min length 10.")
    private Long cost;
    // get and set
}

我没有编译这段代码。所以它们可能是语法错误。

答案 4 :(得分:1)

你似乎在混合JPA和Hibernate特定的api。在配置文件中删除:

<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>

并添加

   <event type="pre-update">
     <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
   </event>
   <event type="pre-insert">
     <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
   </event>