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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With