开发者

In-MemoryDB: create schema in 'setUp()' of Unit Testing: Netbeans (6.5.1) Hibernate (3) Junit(3), HSQL (1.8)

What are the steps needed to setup an in-memory DB, build the schema automatically with Hibernate's 'hbm2ddl' tool within a Junit (3) 'setUp()' using Netbeans 6.5.1 ? I'm not using Hibernate annotations - just a mapping file.

For the actual code, I want to use an on-disk database of course. [that is the Junits live a separate 'test' package]

So I think this is getting there:

  1. Create a standard Java Project in Netbeans 6.5.1 , add in the Hiberate library.
  2. Create the POJOs, hibernate.cfg and hibernate mapping file.
  3. Copy the cfg and mapping file to the test package.

The setup method looks like this:

 protected void setUp() throws Exception {
         Configuration config = new Configuration();
         config.configure(开发者_高级运维);
         SchemaExport exporter;
         exporter=new SchemaExport(config);
         exporter.create(true, true);
    }


  1. Create a standard Java Project in Netbeans 6.5.1 , add in the Hiberate library.
  2. Create the POJOs, hibernate.cfg and hibernate mapping file.
  3. Copy the cfg and mapping file to the test package.

The outline of the test case looks like this:

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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜