1

设置内存数据库需要哪些步骤,使用 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);
    }
4

1 回答 1

1
  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();
    }
于 2009-12-12T17:44:54.890 回答