HSQLDB + JUnit + Hibernate:java.sql.SQLException:无效的模式名称
Posted
技术标签:
【中文标题】HSQLDB + JUnit + Hibernate:java.sql.SQLException:无效的模式名称【英文标题】:HSQLDB + JUnit + Hibernate: java.sql.SQLException: invalid schema name 【发布时间】:2015-05-18 14:49:26 【问题描述】:我想用 JUnit 和 Hibernate 运行内存数据库 HSQLDB 的测试,但是当方法尝试将更改提交到 DB 时,出现此异常:(无 Spring 框架)
Caused by: java.sql.SQLException: invalid schema name: SRH in statement [select liquidacio0_.idLiquidacion as idLiquid1_180_0_, liquidacio0_.fkEstado as fkEstado180_0_, liquidacio0_.FECHA_LIQ as FECHA3_180_0_, liquidacio0_.DIAS_LIQUIDAR as DIAS4_180_0_, liquidacio0_.MES_DIAS as MES5_180_0_, liquidacio0_.MES_LIQUIDACION as MES6_180_0_, liquidacio0_.LIQUIDACION_TVAC as LIQUIDAC7_180_0_, liquidacio0_.FECHA_ESTADO as FECHA8_180_0_, liquidacio0_.fkSolicitud as fkSolici9_180_0_, liquidacio0_.idTvac as idTvac180_0_, liquidacio0_.NUMLIQ as NUMLIQ180_0_, liquidacio0_.legajo as legajo180_0_, liquidacio0_.periodo as periodo180_0_ from srh.srh_liquidacion liquidacio0_ where liquidacio0_.idLiquidacion=?]
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.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:505)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:423)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1547)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
这里发生异常:
sesionActiva.getTransaction().commit();
我的 hibernateTest.cfg.xml:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:mem:testdb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!--<property name="hibernate.default_schema">srh</property>-->
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="pkgClasesDeNegocio/pkgAdministracionNovedadAsistencia/Liquidacion.hbm.xml"/>
//And others
</session-factory>
</hibernate-configuration>
我的 Liquidacion.hbm.xml:
<hibernate-mapping>
<class catalog="srh" name="pkgClasesDeNegocio.pkgAdministracionNovedadAsistencia.Liquidacion" table="srh_liquidacion">
<id name="idLiquidacion" type="java.lang.Integer">
<column name="idLiquidacion"/>
<generator class="identity"/>
</id>
<many-to-one class="pkgClasesDeNegocio.pkgAdministracionNovedadAsistencia.Estado" lazy="false" fetch="select" name="estado">
<column name="fkEstado" not-null="true"/>
</many-to-one>
<property name="fechaLiquidacion" type="date">
<column length="10" name="FECHA_LIQ"/>
</property>
<property name="cantDiasALiquidar">
<column name="DIAS_LIQUIDAR"/>
</property>
<property name="mesDeDias" type="string">
<column length="50" name="MES_DIAS"/>
</property>
<property name="mesDeLiquidacion" type="string">
<column length="50" name="MES_LIQUIDACION"/>
</property>
<property name="fechaLiquidacionTvac" type="string">
<column length="50" name="LIQUIDACION_TVAC"/>
</property>
<property name="fechaEstado" type="date">
<column length="10" name="FECHA_ESTADO"/>
</property>
<many-to-one class="pkgClasesDeNegocio.pkgAdministracionNovedadAsistencia.SolicitudVacaciones" lazy="false" fetch="select" name="solicitud">
<column name="fkSolicitud" not-null="true"/>
</many-to-one>
<property name="idTvac">
<column name="idTvac"/>
</property>
<property name="nroLiquidacion">
<column name="NUMLIQ"/>
</property>
<property name="legajo">
<column name="legajo"/>
</property>
<property name="periodo">
<column name="periodo"/>
</property>
</class>
【问题讨论】:
【参考方案1】:尝试删除 catalog="srh"。这可能是问题所在:http://forums.mysql.com/read.php?39,137564,137629#msg-137629
【讨论】:
【参考方案2】:将架构名称更改为 PUBLIC 并且它工作(PUBLIC 应该是架构的名称)-;) ,所有其他架构名称都不起作用并给出“无效架构 ..name”错误。感谢我在这里找到的旧帖子 https://sourceforge.net/p/unitils/discussion/570578/thread/a887838e/
【讨论】:
以上是关于HSQLDB + JUnit + Hibernate:java.sql.SQLException:无效的模式名称的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 HSQLDB 内存数据库设置为 Maven 项目执行 Junit 测试套件?
HSQLDB Junit 测试对 DB2 和 SQL Server NOLOCK 查询失败
Junit 测试用例 - HSQLDB count(...) over (partition ... order by ...)