Drop-and-create-tables in OpenJPA

Posted by Roger Keays, 6 March 2008, 1:56 PM

Toplink had a useful drop-and-create-tables options which made unit testing a lot easier because you always new you were testing a fresh database. Well, I've been trying to do the same thing in OpenJPA and couldn't figure it out until I found this JIRA issue: OPENJPA-94.

The nearest equivalent in OpenJPA is the following obscure setting:

openjpa.jdbc.SynchronizeMappings=buildSchema(SchemaAction='add,deleteTableContents')

It is actually faster than the Toplink method because the tables aren't recreated, they are just updated and emptied. It'd just be nice if it had a sensible name, and appeared in the docs somewhere!

Comment posted by: Mike P on 14 March 2008, 5:34 AM

I've found just rolling back the transaction at the end of each unit test does the trick quite nicely.  Something like this:

    @BeforeClass
    public static void initialize()
        throws Exception
    {
        em = (EntityManager) initialContext.lookup( "java:/EntityManager" );
        tm = (TransactionManager) initialContext.lookup( "java:/TransactionManager" );
    }
   
   
    @Before
    public void setUp()
        throws Exception
    {
        // Begin transaction
        tm.begin();
    }
 
    @After
    public void tearDown()
        throws Exception
    {
        // Rollback transaction
        tm.rollback();
    }

 

Add a comment

Please visit http://www.ilikespam.com/blog/drop-and-create-tables-in-openjpa to add your comments.