Hibernate6根据xml获取ddl sql语句

java 复制代码
package com.kongjs.dbmo.jdbc;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.tool.schema.SourceType;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.tool.schema.internal.ExceptionHandlerHaltImpl;
import org.hibernate.tool.schema.spi.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.*;

public class Hibernate6DDL {

    public static String getDDLSql(String dialect, File dir) {
        StringBuilder sb = new StringBuilder();
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySetting("hibernate.dialect", dialect)
                .applySetting("hibernate.temp.use_jdbc_metadata_defaults", false)
                .build();
        try {
            Metadata metadata = new MetadataSources(serviceRegistry)
                    .addDirectory(dir)
                    .buildMetadata();

            Map<String, Object> config = new HashMap<>(Objects.requireNonNull(serviceRegistry.getService(ConfigurationService.class)).getSettings());
            ExceptionHandler exceptionHandler = ExceptionHandlerHaltImpl.INSTANCE;
            ExecutionOptions executionOptions = SchemaManagementToolCoordinator.buildExecutionOptions(config, exceptionHandler);
            SchemaManagementTool tool = serviceRegistry.getService(SchemaManagementTool.class);

            SourceDescriptor sourceDescriptor = new SourceDescriptor() {
                @Override
                public SourceType getSourceType() {
                    return SourceType.METADATA;
                }

                @Override
                public ScriptSourceInput getScriptSourceInput() {
                    return null;
                }
            };

            TargetDescriptor targetDescriptor = new TargetDescriptor() {
                @Override
                public EnumSet<TargetType> getTargetTypes() {
                    return EnumSet.of(TargetType.SCRIPT);
                }

                @Override
                public ScriptTargetOutput getScriptTargetOutput() {
                    return new ScriptTargetOutput() {
                        @Override
                        public void prepare() {

                        }

                        @Override
                        public void accept(String command) {
                            sb.append(command);
                            sb.append("\n");
                        }

                        @Override
                        public void release() {

                        }
                    };
                }
            };

            Objects.requireNonNull(tool);

            SchemaCreator schemaCreator = tool.getSchemaCreator(config);
            schemaCreator.doCreation(metadata, executionOptions, ContributableMatcher.ALL, sourceDescriptor, targetDescriptor);

            SchemaDropper schemaDropper = tool.getSchemaDropper(config);
            schemaDropper.doDrop(metadata, executionOptions, ContributableMatcher.ALL, sourceDescriptor, targetDescriptor);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            StandardServiceRegistryBuilder.destroy(serviceRegistry);
        }
        return sb.toString();
    }

    public static final Map<String, String> dialects = new LinkedHashMap<>();

    public static void initDialect(String classpath) {
        try {
            ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resourcePatternResolver.getResources(classpath);
            for (Resource resource : resources) {
                String filename = resource.getFilename();
                if (filename == null) {
                    continue;
                }
                if (filename.contains("Dialect") && !filename.contains("Abstract")) {
                    int indexOf = filename.indexOf("Dialect");
                    String name = filename.substring(0, indexOf);
                    if (!StringUtils.hasLength(name)) {
                        continue;
                    }
                    if (!filename.contains("community")) {
                        dialects.put(name, "org.hibernate.dialect." + name + "Dialect");
                    } else {
                        dialects.put(name, "org.hibernate.community.dialect." + name + "Dialect");
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static {
        String path1 = "classpath*:/org/hibernate/dialect/**";
        //initDialect(path1);
        String path2 = "classpath*:/org/hibernate/community/dialect/**";
        //initDialect(path2);
    }

    public static void main(String[] args) throws FileNotFoundException {
        //System.out.println(dialects);
        //System.out.println(getCreatorSql("org.hibernate.dialect.PostgreSQLDialect",
                //new FileInputStream("C:\\Users\\Administrator\\IdeaProjects\\dbmo\\dbmo-start\\src\\test\\resources\\Event.hbm.xml")));
    }
}
相关推荐
程序媛-徐师姐5 分钟前
Java 基于SpringBoot+vue框架的老年医疗保健网站
java·vue.js·spring boot·老年医疗保健·老年 医疗保健
yngsqq6 分钟前
c#使用高版本8.0步骤
java·前端·c#
尘浮生15 分钟前
Java项目实战II基于微信小程序的校运会管理系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
安迁岚19 分钟前
【SQL Server】华中农业大学空间数据库实验报告 实验六 视图
数据库·sql·mysql·oracle·实验报告
小白不太白95020 分钟前
设计模式之 模板方法模式
java·设计模式·模板方法模式
Tech Synapse22 分钟前
Java根据前端返回的字段名进行查询数据的方法
java·开发语言·后端
xoxo-Rachel28 分钟前
(超级详细!!!)解决“com.mysql.jdbc.Driver is deprecated”警告:详解与优化
java·数据库·mysql
乌啼霜满天24930 分钟前
JDBC编程---Java
java·开发语言·sql
色空大师43 分钟前
23种设计模式
java·开发语言·设计模式