views:

305

answers:

1

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  A: 
  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();
    }
monojohnny