内存数据库:在单元测试的“setUp()”中创建模式:Netbeans (6.5.1) Hibernate (3) Junit(3)、HSQL (1.8)

StackOverflow https://stackoverflow.com/questions/1893737

设置内存数据库、使用 Netbeans 6.5.1 在 Junit (3) 'setUp()' 中使用 Hibernate 的 'hbm2ddl' 工具自动构建架构需要哪些步骤?我没有使用 Hibernate 注释 - 只是一个映射文件。

对于实际代码,我当然想使用磁盘数据库。[那是 Junits 生活在一个单独的“测试”包中]

所以我认为这已经实现了:

  1. 在Netbeans 6.5.1中创建一个标准Java项目,添加Hiberate库。
  2. 创建 POJO、hibernate.cfg 和 hibernate 映射文件。
  3. 将cfg和映射文件复制到测试包中。

设置方法如下所示:

 protected void setUp() throws Exception {
         Configuration config = new Configuration();
         config.configure();
         SchemaExport exporter;
         exporter=new SchemaExport(config);
         exporter.create(true, true);
    }
有帮助吗?

解决方案

  1. 在Netbeans 6.5.1中创建一个标准Java项目,添加Hiberate库。
  2. 创建 POJO、hibernate.cfg 和 hibernate 映射文件。
  3. 将cfg和映射文件复制到测试包中。

测试用例的概要如下:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
...
public class DatabaseTest extends TestCase {
    private static Configuration config;
    private static SessionFactory sessionFactory;
    private static Session session;
...
    @Override
    protected void setUp() throws Exception {
         config = new Configuration();
         config.configure();
         SchemaExport exporter;
         exporter=new SchemaExport(config);
         exporter.create(true, true);
         sessionFactory = config.buildSessionFactory();
         session=sessionFactory.openSession();
    }
...
    @Override
    protected void tearDown() throws Exception {
        session.close();
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top