4
在休眠4.3 Ejb3Configuration類已被刪除。這個類通常用於從持久性單元(persistence.xml文件)到SchemaExport工具創建hibernate配置文件。如何使用SchemaExportTool與JPA和休眠4.3
作爲一個簡單的替代導出模式到SQL文件我使用以下代碼:
public static void export(String persistenceUnit, String exportFileName) {
Map<String, String> hash = new HashMap<String, String>();
hash.put("hibernate.hbm2ddl.auto", "create-drop");
EntityManagerFactory factory = Persistence.createEntityManagerFactory(
persistenceUnit, hash);
org.hibernate.jpa.internal.EntityManagerFactoryImpl hibFactory = (org.hibernate.jpa.internal.EntityManagerFactoryImpl) factory;
SessionFactoryImpl hibSessionFactory = hibFactory.getSessionFactory();
SchemaExport schema = ReflectUtils.getPrivateFieldValue(
hibSessionFactory, "schemaExport");
schema.setOutputFile(exportFileName);
schema.setFormat(false);
schema.setDelimiter(";");
schema.drop(true, false);
schema.create(true, false);
}
在這一段代碼,我基本上使用由HibernateSessionFactoryImpl創建的SchemaExport對象。缺點是每次執行時都會重新創建數據庫模式。有沒有其他簡單的方法與Hibernate 4.3和JPA一起使用SchemaExporTool?看來真正的問題是如何從persistenceunit創建Hibernate配置對象?
的可能的複製[如何導出爲休眠模式> 4.3(HTTP:/ /stackoverflow.com/questions/22733631/how-to-export-the-schema-for-hibernate-4-3) – oers
我寫了一個答案[這裏在stackoverflow](http://stackoverflow.com/a/37292442/ 644450),它不需要persistence.xml,也支持envers。 – oers