How to manually invoke create-drop from JPA on Hibernate?

I need this for an integration test. My environment is JBoss 7, EJB3 with JPA on Hibernate 4, H2 in-memory database and tests are run by Arquillian. I want to be able to drop the database and create it again will all tables based on persistence.xml and the entities. I know I can do it at the start of the application by specifying:

<property name="hibernate.hbm2ddl.auto" value="create-drop" /> 

But I need to do this manually from the code after the first drop and create happened.

Is it possible? What is the easiest way?

Answers


You can programatically do this in Hibernate.

config = new Configuration();
config.setProperty(org.hibernate.cfg.Environment.SHOW_SQL, "true");
config.setProperty(org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create");
....
SchemaExport export = new SchemaExport( config );
export.create( true, true );

The JavaDocs are here:

http://docs.jboss.org/hibernate/core/3.5/javadocs/org/hibernate/cfg/Configuration.html http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/tool/hbm2ddl/SchemaExport.html


You could use the Arquillian persistence extension to clear the database content after each test using the @UsingDataSet annotation on the test methods: https://github.com/arquillian/arquillian-extension-persistence


I think you'll need to create the scripts manually. You can then use ScriptRunner (copy the class in your project):

public class YourIntegrationTestClass {
    private String url = "test-db-url";
    private String user = "user";
    private String pass = "pass";

    // run this before the test
    public void prepareDB() {
        // executes a script stored in test/resources/cucumber
        try {
            // use your driver here
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, user, pass);
            ScriptRunner runner = new ScriptRunner(conn, false, true);

            // use your db creation script here
            runner.runScript(new BufferedReader(new FileReader("createDB.sql")));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    // run this after the tests
    public void dropDB() {
            // use your driver here
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, user, pass);
            ScriptRunner runner = new ScriptRunner(conn, false, true);

            // use your db drop script here
            runner.runScript(new BufferedReader(new FileReader("dropDB.sql")));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

Need Your Help

DivX Decoder libraries?

cocoa winapi divx

I saw, in an application from a major software vendor a dll called "DivXDecoder.dll" - which intrigued me as (a) it implies the existence of an easy to use divx library, and (b) I'd rather like to ...

How to get the IADSUser native object from directory entry through powershell?

windows powershell com directoryentry

I wanted to get the groups of a local user account in windows.This can be done if we get the native object from the directory entry. This is achieved in the following way through APIs :