我正在使用HSQL在我的java应用程序上运行许多单元测试。我正在使用Spring + Hibernate。从MySQL切换到HSQL时遇到问题。测试在MySQL上完美运行,但每当我改为HSQL时,我都会遇到以下异常:
Caused by: org.hsqldb.HsqlException: invalid schema name: LMS
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.SchemaManager.getSchemaHsqlName(Unknown Source)
at org.hsqldb.SchemaManager.getSchemaName(Unknown Source)
at org.hsqldb.Session.getSchemaName(Unknown Source)
at org.hsqldb.SchemaManager.getTable(Unknown Source)
at org.hsqldb.ParserDQL.readTableName(Unknown Source)
at org.hsqldb.ParserDQL.readSimpleRangeVariable(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
我的Spring配置如下:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver" />
<property name="url" value="jdbc:hsqldb:file:lms" />
<property name="username" value="SA"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.dreamteam.lms.**.*</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>-->
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
</props>
</property>
</bean>
我的一个班级的样本注释:
@Entity
@Table(name = "answer", catalog = "lms")
public class Answer implements Cloneable, Serializable, IPojoGenEntity, IAnswer {
.
.
任何见解都将受到赞赏。
此致 克里斯
答案 0 :(得分:8)
制作“create-schema.sql”文件
CREATE SCHEMA lms;
添加“dataSourceInitializer”bean
<bean id="dataSourceInitializer" class="org.springframework.jdbc.datasource.init.DataSourceInitializer">
<property name="dataSource" ref="dataSource" />
<property name="databasePopulator">
<bean class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
<property name="continueOnError" value="true" />
<property name="scripts">
<list>
<value>classpath:SQL/create-schema.sql</value>
</list>
</property>
</bean>
</property>
</bean>
将“depends-on”属性设置为“sessionFactory”bean
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" depends-on="dataSourceInitializer">
...
答案 1 :(得分:5)
我在测试期间使用以下bean来创建模式。
public class HSQLSchemaCreator {
private String schemaName;
private DataSource dataSource;
public HSQLSchemaCreator(String schemaName, DataSource dataSource) {
this.schemaName = schemaName;
this.dataSource = dataSource;
}
@PostConstruct
public void postConstruct() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE SCHEMA " + schemaName + " AUTHORIZATION DBA");
}
}
弹簧配置:
<bean id="hsqlSchemaCreator" class="....HSQLSchemaCreator">
<constructor-arg name="schemaName" value="..."/>
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<!-- Override entityManagerFactory to depend on hsqlSchemaCreator for tests -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="hsqlSchemaCreator">
依旧......
答案 2 :(得分:4)
只是为了记录,我设法通过简单地从我的Hibernate实体中删除'catalog'属性来解决这个问题。因此,
@Entity
@Table(name = "answer", catalog = "lms")
成了
@Entity
@Table(name = "answer")
答案 3 :(得分:2)
模式,表,列等的名称在MySQL中不区分(至少不是默认情况下)区分大小写。 HSQLDB区分大小写,但它还将查询中未引用的所有标识符转换为大写。
您可以通过在任何地方将模式名称更改为LMS来快速测试这是您的问题(首先在数据库中)。您可以在此处找到有关HSQLDB和Hibernate的更多详细信息:HSQLDB No such table Exception
答案 4 :(得分:0)
我不确定为什么这行得通,但至少对我来说,在表名和模式周围加方括号对我有用:
@Table(name = "schema.tableName")
成为
@Table(name = "[schema].[tableName]")