【问题标题】:In-MemoryDB: create schema in 'setUp()' of Unit Testing: Netbeans (6.5.1) Hibernate (3) Junit(3), HSQL (1.8)In-MemoryDB:在单元测试的“setUp()”中创建模式:Netbeans (6.5.1) Hibernate (3) Junit(3), HSQL (1.8)
【发布时间】:2010-12-26 00:14:27
【问题描述】:

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

【问题讨论】:

    标签: hibernate netbeans junit hsqldb hbm2ddl


    【解决方案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();
        }
    

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多