开发者

HSQLDB not throwing correct type of ConstraintViolationException with Spring JPA/Hibernate

开发者 https://www.devze.com 2023-04-11 11:22 出处:网络
I have a Spring based JPA2 app with the Hibernate provider on HSQLDB. I\'m trying to elegantly handle exceptions but am finding I am getting hibernate exceptions not JPA ones. For example:

I have a Spring based JPA2 app with the Hibernate provider on HSQLDB. I'm trying to elegantly handle exceptions but am finding I am getting hibernate exceptions not JPA ones. For example:

   import javax.validation.ConstraintViolationException;
   ...

    @Override
    @Transactional
    public Result<Document> save(Document document) {
        try {
            store.persist(document);
            return new Success<Document>(document);
        }
        catch (EntityExistsException e) {
            return new Errors<Document>(DocumentError.DOCUMENT_ALREADY_EXISTS);
        }
        catch (ConstraintViolationException e) {
            return new Errors<Document>(DocumentError.CONSTRAINT_VOILATION);
        }
        catch (PersistenceException e) {
            return new Errors<Document>(DocumentError.UNKOWN_EXCEPTION);
        }
    }

Instead of javax.validation.ConstraintViolationException being thrown I catch a PersistenceException:

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [Document]

I have configured the JpaAdapter in my Spring configuration:

       <bean id="dataSource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource"
              p:driverClassName="org.hsqldb.jdbcDriver"
              p:url="jdbc:hsqldb:file:target/db/intox"
              p:username="sa"
              p:password=""
                />

        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
              p:entityManagerFactory-ref="entityManagerFactory"/>

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManager开发者_开发知识库FactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter" ref="jpaAdapter"/>
            <property name="loadTimeWeaver">
                <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
            </property>
            <property name="persistenceUnitName" value="p"/>
        </bean>

        <bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>

        <bean id="jpaAdapter"
              class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
              p:database="HSQL"
              p:showSql="false"/>

Can anyone suggest how or why I am getting Hibernate exceptions rather than JPA (javax.persistence) ones?

Additional info:

Spring 3.1.0.M2

Hibernate 3.5.4-Final

Hibernate Validator 4.1.0.Final

HSQLDB 2.0.0


HSQLDB is database which does not throw Java Bean validation exceptions. Type of exception is correct - nothing strange in that. Hibernate created PersistenceException and gave more specific exception as cause: org.hibernate.exception.ConstraintViolationException. From javadoc we can find following explanation: "indicating that the requested DML operation resulted in a violation of a defined integrity constraint". Maybe you have even longer stacktrace logged somewhere, which have also exception thrown by JDBC-driver included?

Without knowing more about mappings, and values in Document you try to insert, it is impossible to say which integrity constraint is violated.

Other possibility for this Exception is that entity actually exists already, because Hibernate have bug HHH-4131 . In that bug report it is told, that some versions of Hibernate throws PersistenceException instead of specified EntityExistsException.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号