How to use SchemaExportTool with JPA and Hibernate 4.3

后端 未结 1 1667
谎友^
谎友^ 2021-01-19 07:10

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) t

相关标签:
1条回答
  • 2021-01-19 08:06

    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);
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题