Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 656
MartinTeeVarga Avatar asked Dec 12 '13 05:12

MartinTeeVarga


1 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

like image 59
HDave Avatar answered Oct 06 '22 13:10

HDave