Programmatic SchemaExport / SchemaUpdate with Hibernate 5 and Spring 4

后端 未结 4 801
囚心锁ツ
囚心锁ツ 2020-12-10 01:28

With Spring 4 and Hibernate 4, I was able to use Reflection to get the Hibernate Configuration object from the current environment, using this code:

@Autowir         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-10 02:08

    Well, my go to on this:

    public class SchemaTranslator {
        public static void main(String[] args) throws Exception {
            new SchemaTranslator().run();
        }
        private void run() throws Exception {    
            String packageName[] = { "model"};    
            generate(packageName);
        }   
        private List> getClasses(String packageName) throws Exception {
            File directory = null;
            try {
                ClassLoader cld = getClassLoader();
                URL resource = getResource(packageName, cld);
                directory = new File(resource.getFile());
            } catch (NullPointerException ex) {
                throw new ClassNotFoundException(packageName + " (" + directory + ") does not appear to be a valid package");
            }
            return collectClasses(packageName, directory);
        }
        private ClassLoader getClassLoader() throws ClassNotFoundException {
            ClassLoader cld = Thread.currentThread().getContextClassLoader();
            if (cld == null) {
                throw new ClassNotFoundException("Can't get class loader.");
            }
            return cld;
        }
        private URL getResource(String packageName, ClassLoader cld) throws ClassNotFoundException {
            String path = packageName.replace('.', '/');
            URL resource = cld.getResource(path);
            if (resource == null) {
                throw new ClassNotFoundException("No resource for " + path);
            }
            return resource;
        }
        private List> collectClasses(String packageName, File directory) throws ClassNotFoundException {
            List> classes = new ArrayList<>();
            if (directory.exists()) {
                String[] files = directory.list();
                for (String file : files) {
                    if (file.endsWith(".class")) {
                        // removes the .class extension
                        classes.add(Class.forName(packageName + '.' + file.substring(0, file.length() - 6)));
                    }
                }
            } else {
                throw new ClassNotFoundException(packageName + " is not a valid package");
            }
            return classes;
        }
        private void generate(String[] packagesName) throws Exception {
            Map settings = new HashMap();
            settings.put("hibernate.hbm2ddl.auto", "drop-create");
            settings.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL94Dialect");
            MetadataSources metadata = new MetadataSources(
                    new StandardServiceRegistryBuilder()
                            .applySettings(settings)
                            .build());    
            for (String packageName : packagesName) {
                System.out.println("packageName: " + packageName);
                for (Class clazz : getClasses(packageName)) {
                    System.out.println("Class: " + clazz);
                    metadata.addAnnotatedClass(clazz);
                }
            }
            SchemaExport export = new SchemaExport(
                    (MetadataImplementor) metadata.buildMetadata()
            );
            export.setDelimiter(";");
            export.setOutputFile("db-schema.sql");
            export.setFormat(true);
            export.execute(true, false, false, false);
        }
    }
    

提交回复
热议问题