在内存DB中从MySQL切换到Hypersonic时的SQL语法异常

时间:2012-11-12 07:29:16

标签: java sql spring hibernate maven

我正在尝试为我的应用程序运行测试,但不使用生产数据库并在内存中使用Hypersonic。

我遇到的问题是,在最终使用我希望它使用的applicationContext.xml和persistence.xml文件后,我收到链接到CatalogItem类的测试的以下错误。

问题是我被特别告知我不应该修改类本身中的任何内容,因为它们可以在MySQL中运行得很好。我希望问题出在我的.xml文件中。

测试的persistence.xml

<!-- Classes declaration omitted -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


    <!-- FOR UNIT TESTING ONLY -->

    <context:property-placeholder location="classpath*:META-INF/spring/test-database.properties"/>

    <bean id="externalProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
       <property name="ignoreResourceNotFound"><value>true</value></property>
       <property name="locations">
          <list>
            <value>classpath*:META-INF/conf/configuration.properties</value>
          </list>
       </property>
    </bean>

    <context:spring-configured/>

    <context:component-scan base-package="com.mms.pone.portal">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

     <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <property name="testWhileIdle" value="false"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="maxActive" value="1" />

        <property name="minEvictableIdleTimeMillis" value="1800000"/>
    </bean>

    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>

        <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml" />
    </bean>    
</beans>

database.properties:

########################################################
#                                                      #
#  This configuration is used for unit-testing only.   #
#                                                      #
########################################################
# Hypersonic
database.driverClassName=org.hsqldb.jdbcDriver

database.url=jdbc\:hsqldb\:file\:c\:/Project

database.username=sa
database.password=

运行mvn时得到的堆栈跟踪

Tests in error:
  testDeleteAsset(com.mms.pone.portal.domain.AssetIntegrationTest): org.hibernate.exception.SQLGrammarException: could not load an entity: [com.mms.pone.portal.domain.Asset#64]; nested exception is ja
vax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.mms.pone.portal.domain.Asset#64]
  testFindCatalogItem(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceExc
eption: org.hibernate.exception.SQLGrammarException: could not execute query
  testCountAllCatalogItems(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.Persisten
ceException: org.hibernate.exception.SQLGrammarException: could not execute query
  testFindAllCatalogItems(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.Persistenc
eException: org.hibernate.exception.SQLGrammarException: could not execute query
  testFindCatalogItemEntries(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.Persist
enceException: org.hibernate.exception.SQLGrammarException: could not execute query
  testUpdateCatalogItemUpdate(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.Persis
tenceException: org.hibernate.exception.SQLGrammarException: could not execute query
  testSaveCatalogItem(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceExc
eption: org.hibernate.exception.SQLGrammarException: could not execute query
  testDeleteCatalogItem(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceE
xception: org.hibernate.exception.SQLGrammarException: could not execute query
  testFlush(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceException: or
g.hibernate.exception.SQLGrammarException: could not execute query

CatalogItemIntegrationTest的日志输出:

-------------------------------------------------------------------------------
Test set: com.mms.pone.portal.domain.CatalogItemIntegrationTest
-------------------------------------------------------------------------------
Tests run: 9, Failures: 0, Errors: 8, Skipped: 0, Time elapsed: 0.024 sec <<< FAILURE!
testFlush(com.mms.pone.portal.domain.CatalogItemIntegrationTest)  Time elapsed: 0.015 sec  <<< ERROR!
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:326)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.findCatalogItemEntries_aroundBody6(CatalogItem_Roo_Jpa_ActiveRecord.aj:31)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.ajc$interMethod$com_mms_pone_portal_domain_CatalogItem_Roo_Jpa_ActiveRecord$com_mms_pone_portal_domain_CatalogItem$findCatalogItemEntries(CatalogItem_Roo_Jpa_ActiveRecord.aj:1)
    at com.mms.pone.portal.domain.CatalogItem.findCatalogItemEntries(CatalogItem.java:1)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItem_Roo_Jpa_ActiveRecord$com_mms_pone_portal_domain_CatalogItem$findCatalogItemEntries(CatalogItem_Roo_Jpa_ActiveRecord.aj)
    at com.mms.pone.portal.service.CatalogItemServiceImpl_Roo_Service.ajc$interMethod$com_mms_pone_portal_service_CatalogItemServiceImpl_Roo_Service$com_mms_pone_portal_service_CatalogItemServiceImpl$findCatalogItemEntries(CatalogItemServiceImpl_Roo_Service.aj:35)
    at com.mms.pone.portal.service.CatalogItemServiceImpl.findCatalogItemEntries(CatalogItemServiceImpl.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$init(CatalogItemDataOnDemand_Roo_DataOnDemand.aj:159)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand.init(CatalogItemDataOnDemand.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$init(CatalogItemDataOnDemand_Roo_DataOnDemand.aj)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$getRandomCatalogItem(CatalogItemDataOnDemand_Roo_DataOnDemand.aj:146)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand.getRandomCatalogItem(CatalogItemDataOnDemand.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$getRandomCatalogItem(CatalogItemDataOnDemand_Roo_DataOnDemand.aj)
    at com.mms.pone.portal.domain.CatalogItemIntegrationTest_Roo_IntegrationTest.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemIntegrationTest_Roo_IntegrationTest$com_mms_pone_portal_domain_CatalogItemIntegrationTest$testFlush(CatalogItemIntegrationTest_Roo_IntegrationTest.aj:74)
    at com.mms.pone.portal.domain.CatalogItemIntegrationTest.testFlush(CatalogItemIntegrationTest.java:1)
    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:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    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:601)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:104)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:255)
    ... 48 more
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2545)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
    at org.hibernate.loader.Loader.list(Loader.java:2271)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:459)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
    ... 48 more
Caused by: java.sql.SQLException: Table not found in statement [select top ? catalogite0_.id as id6_, catalogite0_.active as active6_, catalogite0_.category_id as category14_6_, catalogite0_.deliveryTime as delivery3_6_, catalogite0_.descriptionLong as descript4_6_, catalogite0_.descriptionShort as descript5_6_, catalogite0_.ean as ean6_, catalogite0_.externalFormId as external7_6_, catalogite0_.manufacturer_id as manufac15_6_, catalogite0_.name as name6_, catalogite0_.remarks as remarks6_, catalogite0_.sapMatNr as sapMatNr6_, catalogite0_.supplierAid as supplie11_6_, catalogite0_.supplier_catalog_id as supplier16_6_, catalogite0_.type as type6_, catalogite0_.version as version6_ from CatalogItem catalogite0_]
    at org.hsqldb.jdbc.Util.throwError(Unknown Source)
    at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
    at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:281)
    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:313)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1700)
    at org.hibernate.loader.Loader.doQuery(Loader.java:801)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
    at org.hibernate.loader.Loader.doList(Loader.java:2542)
    ... 56 more
testFindCatalogItem(com.mms.pone.portal.domain.CatalogItemIntegrationTest)  Time elapsed: 0 sec  <<< ERROR!
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:326)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.findCatalogItemEntries_aroundBody6(CatalogItem_Roo_Jpa_ActiveRecord.aj:31)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.ajc$interMethod$com_mms_pone_portal_domain_CatalogItem_Roo_Jpa_ActiveRecord$com_mms_pone_portal_domain_CatalogItem$findCatalogItemEntries(CatalogItem_Roo_Jpa_ActiveRecord.aj:1)
    at com.mms.pone.portal.domain.CatalogItem.findCatalogItemEntries(CatalogItem.java:1)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItem_Roo_Jpa_ActiveRecord$com_mms_pone_portal_domain_CatalogItem$findCatalogItemEntries(CatalogItem_Roo_Jpa_ActiveRecord.aj)
    at com.mms.pone.portal.service.CatalogItemServiceImpl_Roo_Service.ajc$interMethod$com_mms_pone_portal_service_CatalogItemServiceImpl_Roo_Service$com_mms_pone_portal_service_CatalogItemServiceImpl$findCatalogItemEntries(CatalogItemServiceImpl_Roo_Service.aj:35)
    at com.mms.pone.portal.service.CatalogItemServiceImpl.findCatalogItemEntries(CatalogItemServiceImpl.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$init(CatalogItemDataOnDemand_Roo_DataOnDemand.aj:159)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand.init(CatalogItemDataOnDemand.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$init(CatalogItemDataOnDemand_Roo_DataOnDemand.aj)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$getRandomCatalogItem(CatalogItemDataOnDemand_Roo_DataOnDemand.aj:146)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand.getRandomCatalogItem(CatalogItemDataOnDemand.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$getRandomCatalogItem(CatalogItemDataOnDemand_Roo_DataOnDemand.aj)

2 个答案:

答案 0 :(得分:1)

未找到“CatalogItem”表:

Table not found in statement [select top ? [...] from CatalogItem catalogite0_]

所以也许您对属性“hibernate.hbm2ddl.auto”的设置不正确。您可以尝试“hibernate.hbm2ddl.auto = update”,也许它有帮助。或者您可以使用dbmaintain - http://dbmaintain.org/overview.html - 而不是“hibernate.hbm2ddl.auto”。无论如何,它更安全,更灵活。

如果这没有帮助,请尝试启用SQL日志记录并查看生成的SQL语句。将以下属性添加到persistence.xml:

<property name="hibernate.show_sql" value="true"/> 
<property name="hibernate.format_sql" value="true"/> <property name="use_sql_comments" value="true"/> 

编辑如果使用Squirrel等工具检查HSQL数据库,也可能会有所帮助:http://squirrel-sql.sourceforge.net/您可以检查是否所有表都是使用它正确创建的。

答案 1 :(得分:0)

虽然不是我最初想要的,但在切换到H2-IN-MEMORY之后测试运行得很好......