13

我正在使用 spring 对我的 DAO 进行单元测试。我正在使用 JPA + Hibernate。

对于我的测试,我使用以下 hsqldb 版本:

<dependency>
    <groupId>hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>1.8.0.10</version>
    <scope>test</scope>
</dependency>

这是我用于单元测试的 persistence.xml:

   <persistence-unit name="unit-test-pu" transaction-type="RESOURCE_LOCAL">   
      <properties>
         <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
         <property name="javax.persistence.jdbc.user" value="sa"/>
         <property name="javax.persistence.jdbc.password" value=""/>
         <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
         <property name="hibernate.archive.autodetection" value="class"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.use_sql_comments" value="true"/> 
         <property name="hibernate.format_sql" value="true"/>
         <property name="hibernate.hbm2ddl.auto" value="create"/>
      </properties>      
   </persistence-unit>

这是我的测试课:

@ContextConfiguration("/spring/test-context.xml")
@TestExecutionListeners({TransactionalTestExecutionListener.class})
@Transactional
public class BaseDaoTestCase extends AbstractJUnit4SpringContextTests { 

    @Autowired
    private MockEntityDao dao;

    @Test
    public void testSave_success() {
        MockEntity e = new MockEntity();
        dao.save(e);
        assertNotNull(e.getId());
    }
}

注意:这个想法是针对内存中的 HSQLDB 实例测试我的 DAO。每个测试方法都在 spring 管理的事务中运行。

不幸的是,当我运行它时,我从休眠中得到以下异常:

Caused by: org.hibernate.exception.GenericJDBCException: This function is not supported
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:146)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at $Proxy23.prepareStatement(Unknown Source)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:105)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:166)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:103)
    at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:55)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2764)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3275)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:757)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:732)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:736)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:854)
    ... 42 more
Caused by: java.sql.SQLException: This function is not supported
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.Util.notSupported(Unknown Source)
    at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138)
    ... 67 more

我的DAO的save()方法的实现如下:

entityManager.persist(entity);

我正在使用以下休眠版本:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.1.0.Final</version>
    <scope>compile</scope>
</dependency>

有没有人知道发生了什么?

4

3 回答 3

28

我有类似的问题。在我将我的依赖项(尤其是 hsqldb)更新为:

    <hibernate-core-version>4.1.5.Final</hibernate-core-version>
    <spring.version>3.1.2.RELEASE</spring.version>
    <hsqldb.version>2.2.8</hsqldb.version>
    ...
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>${hsqldb.version}</version>
    </dependency>

我的问题解决了。

于 2012-08-12T19:47:11.447 回答
0

您使用的是哪个模拟框架?我只是在学习使用模拟,所以这只是一个疯狂的猜测,但请确保你已经完成了初始化模拟所需的一切。也许问题与测试的设置方式有关。

于 2012-04-04T15:43:12.153 回答
-1

我在使用 JPA + Hibernate + HSQLDB 时遇到了同样的问题,但不是 spring。这是我的代码:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager em = emf.createEntityManager();
EntityTransaction tr = em.getTransaction();
tr.begin();
em.persist(new MyEntity());
tr.commit();
em.close();
emf.close();

这给了我同样的This function is not supported例外,但如果我在没有交易的情况下这样做,它不会。像这样:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager em = emf.createEntityManager();
em.persist(new MyEntity());
em.close();
emf.close();

现在的问题是我没有看到数据库上反映的更改。

于 2012-04-04T17:45:50.613 回答