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")));
    }
}
相关推荐
xlsw_1 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Hacker_LaoYi1 小时前
【渗透技术总结】SQL手工注入总结
数据库·sql
独行soc1 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍06-基于子查询的SQL注入(Subquery-Based SQL Injection)
数据库·sql·安全·web安全·漏洞挖掘·hw
神仙别闹2 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭2 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫3 小时前
泛型(2)
java
超爱吃士力架3 小时前
邀请逻辑
java·linux·后端
南宫生3 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石3 小时前
12/21java基础
java
李小白663 小时前
Spring MVC(上)
java·spring·mvc