我需要这个来进行集成测试。我的环境是 JBoss 7,Hibernate 4 上带有 JPA 的 EJB3,H2 内存数据库和测试由 Arquillian 运行。我希望能够删除数据库并再次创建它,所有表都基于 persistence.xml
和实体。我知道我可以在应用程序开始时指定:
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
但我需要在第一次放置和创建发生后通过代码手动执行此操作。
这可能吗?什么是最简单的方法?
最佳答案
您可以在 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 );
JavaDocs 在这里:
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
关于java - 如何在 Hibernate 上从 JPA 手动调用 create-drop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20535423/