How to use SchemaExportTool with JPA and Hibernate 4.3

爷,独闯天下 提交于 2019-12-01 21:21:38

问题


At Hibernate 4.3 Ejb3Configuration class was removed. This class was commonly used for creating hibernate configuration file from a persistence unit (persistence.xml file) to SchemaExport tool.

As a simple alternative to export schema to .sql file I'm using the following code:

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);
    }

At this piece of code, i'm basically using schemaexport object created by HibernateSessionFactoryImpl. The drawback is that every time it´s executed the database schema is recreated. Is there any other simple way to use SchemaExporTool with Hibernate 4.3 and JPA? It seems that the real question is how to create Hibernate Configuration Object from a persistenceunit?


回答1:


I ran into the same Problem. I ended up by using the internal PersistenceXmlParser of Hibernate to access information in the persistence.xml file and creating the Configuration object manually:

public static void main(String[] args) {

    PersistenceXmlParser parser = new PersistenceXmlParser(new ClassLoaderServiceImpl(), PersistenceUnitTransactionType.RESOURCE_LOCAL);
    List<ParsedPersistenceXmlDescriptor> allDescriptors = parser.doResolve(new HashMap<>());

    for (ParsedPersistenceXmlDescriptor descriptor : allDescriptors) {

        Configuration cfg = new Configuration();
        cfg.setProperty("hibernate.hbm2ddl.auto", "create");
        cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
        cfg.setProperty("hibernate.id.new_generator_mappings", "true");

        List<String> managedClassNames = descriptor.getManagedClassNames();
        for (String className : managedClassNames) {
            try {
                cfg.addAnnotatedClass(Class.forName(className));
            } catch (ClassNotFoundException e) {
                System.out.println("Class not found: " + className);
            }
        }

        SchemaExport export = new SchemaExport(cfg);
        export.setDelimiter(";");
        export.setOutputFile("C:\\dev\\" + descriptor.getName() + "_create_schema.sql");
        export.setFormat(true);
        export.execute(true, false, false, false);

    }
}


来源:https://stackoverflow.com/questions/23310617/how-to-use-schemaexporttool-with-jpa-and-hibernate-4-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!