我刚刚开始使用Hibernate,并开始着手解决问题。配置HSQL服务器进行休眠测试
目前我正在尝试设置一个测试环境,我可以使用HSQL内存实例来测试我的项目。
我遇到的错误是:
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: invalid schema name: TSG
这里是我的项目的相关部分:
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
org.hibernate作为.ejb.HibernatePersistence com.foo.api.models.tsg.AlgPpcAlgorithm OutputEntity
<persistence-unit name="TestingPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.foo.api.models.tsg.AlgPpcAlgorithmOutputEntity
</class>
<properties>
<property name="dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:tsg"/>
<property name="hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.autocommit" value="true"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
正如你所看到的,我有一个peristence-unit
生产(工作正常)和一个内存HSQL一个用来测试(我无法工作)。
一个例子Hibernate的实体:
package com.foo.api.models.tsg;
import javax.persistence.*;
import java.math.BigDecimal;
@IdClass(AlgPpcAlgorithmOutputEntityPK.class)
@Table(name = "alg_ppc_algorithm_output", schema = "", catalog = "tsg")
@Entity
public class AlgPpcAlgorithmOutputEntity {
private int parameterId;
@Column(name = "parameter_id")
@Id
public int getParameterId() {
return parameterId;
}
public void setParameterId(int parameterId) {
this.parameterId = parameterId;
}
private String matchType;
@Column(name = "matchType")
@Basic
public String getMatchType() {
return matchType;
}
// for brevity I have removed the rest of the implementation
// It was auto-generated by Hibernate and works fine in production.
}
最后,一个简单的TestCase类:
package tests.integration;
import com.foo.api.models.tsg.AlgPpcAlgorithmOutputEntity;
import com.foo.api.util.HibernateUtil;
import org.hsqldb.Server;
import org.hsqldb.persist.HsqlProperties;
import org.hsqldb.server.ServerConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tests.util.HSQLServerUtil;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import com.foo.api.KeywordManager;
import java.sql.Date;
import java.util.HashSet;
public class KeywordManagerTestCase {
private static final Logger LOG = LoggerFactory.getLogger(KeywordManagerTestCase.class);
private EntityManagerFactory eMF;
protected EntityManager eM;
@Before
public void setUp() throws Exception {
HsqlProperties props = new HsqlProperties();
props.setProperty("server.database.0", "mem:tsg");
props.setProperty("server.dbname.0", "tsg");
ServerConfiguration.translateDefaultDatabaseProperty(props);
Server hsqlServer = new Server();
hsqlServer.setRestartOnShutdown(false);
hsqlServer.setNoSystemExit(true);
hsqlServer.setProperties(props);
hsqlServer.setTrace(true);
LOG.info("Configured the HSQLDB server...");
hsqlServer.start();
LOG.info("HSQLDB server started on port " + hsqlServer.getPort() + "...");
LOG.info("Loading hibernate...");
if (eMF == null) {
eMF = Persistence.createEntityManagerFactory("TestingPersistenceUnit");
}
eM = eMF.createEntityManager();
}
/**
* shutdown the server.
* @throws Exception in case of errors.
*/
@After
public void tearDown() throws Exception {
eM.close();
HSQLServerUtil.getInstance().stop();
}
/**
* Demo test to see that the number of user records in the database corresponds the flat file inserts.
*/
@Test
public void testDemo1() {
AlgPpcAlgorithmOutputEntity entity = new AlgPpcAlgorithmOutputEntity();
entity.setParameterId(200);
entity.setMatchType("aa");
KeywordManager km;
eM.persist(entity);
HashSet<Integer> params = new HashSet<Integer>();
params.add(200);
km = new KeywordManager(eM, params, new Date[2]);
HashSet<AlgPpcAlgorithmOutputEntity> res = km.pullKeywords(params);
for (AlgPpcAlgorithmOutputEntity s : res) {
System.out.println(s.getMatchType());
}
}
}
我敢肯定,我已经设置的东西了在陌生的路上,但就像我说的 - 这是我的第一个刺。
这里是我想要做的事:
- 有测试配置(与所有的休眠类映射一起)为HSQL DB目前在persistence.xml中
- 有HSQL DB开始用于单元测试以及使用我的项目模式创建内存数据库(如persistence.xml中类元素下所述)。
- 创建实体对象并在测试时将它们添加到测试数据库中,以便我可以将该数据库用于我的集成测试。
我只是无法超越这个PersistenceException!
UPDATE
好了,我意识到,我并不需要安装在我的测试情况下,设置一个明确的HSQL服务器,因为这正是我persistence.xml
的persistence-unit
项是。此外,我试图改变目录,以便它匹配我的映射类使用的目录。我的测试案例的建立,现在看起来像:
private EntityManagerFactory eMF;
protected EntityManager eM;
@Before
public void setUp() throws Exception {
LOG.info("Loading hibernate...");
if (eMF == null) {
eMF = Persistence.createEntityManagerFactory("TestingPersistenceUnit");
}
eM = eMF.createEntityManager();
EntityTransaction eT = null;
eT = eM.getTransaction();
eT.begin();
Query q = eM.createNativeQuery("ALTER CATALOG PUBLIC RENAME TO TSG");
q.executeUpdate();
eT.commit();
// And also it seems I need to create the schema
eT = eM.getTransaction();
eT.begin();
q = eM.createNativeQuery("CREATE SCHEMA TSG AUTHORIZATION DBA");
q.executeUpdate();
eT.commit();
}
不过,我刚刚结束了一个新的错误,现在,具体如下:
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: ALG_PPC_ALGORITHM_OUTPUT
所以我得到的地方,但它似乎是表没有被创建。我想知道我的persistence.xml
是否有问题?
什么是您的数据库文件名? 'tsg'? –
@YogendraSingh我想使用内存数据库,所以我不想拥有数据库文件。但是,在生产(远程mysql服务器)上,数据库的*名称*是* tsg *。我是否指定了一些意思,意味着我无意中尝试使用基于文件的数据库? – Edwardr