hibernate5 根据xml获取ddl sql语句

这篇具有很好参考价值的文章主要介绍了hibernate5 根据xml获取ddl sql语句。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

package com.kongjs.kda;

import lombok.val;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.dialect.Dialect;
//import org.hibernate.relational.SchemaManager;
//import org.hibernate.relational.internal.SchemaManagerImpl;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;

import java.util.EnumSet;

public class HibernateCoreTest {
    public static void main1(String[] args) {
        //ServiceRegistry standardRegistry =
        //        new StandardServiceRegistryBuilder().build();

        //MetadataSources sources = new MetadataSources(standardRegistry);

// alternatively, we can build the MetadataSources without passing
// a service registry, in which case it will build a default
// BootstrapServiceRegistry to use.  But the approach shown
// above is preferred
// MetadataSources sources = new MetadataSources();

// add a class using JPA/Hibernate annotations for mapping
        ///sources.addAnnotatedClass(MyEntity.class);

// add the name of a class using JPA/Hibernate annotations for mapping.
// differs from above in that accessing the Class is deferred which is
// important if using runtime bytecode-enhancement
        //sources.addAnnotatedClassName("org.hibernate.example.Customer");

// Read package-level metadata.
        //sources.addPackage("hibernate.example");

// Read package-level metadata.
        //sources.addPackage(MyEntity.class.getPackage());

// Adds the named hbm.xml resource as a source: which performs the
// classpath lookup and parses the XML
        //sources.addResource("org/hibernate/example/Order.hbm.xml");

// Adds the named JPA orm.xml resource as a source: which performs the
// classpath lookup and parses the XML
        //sources.addResource("org/hibernate/example/Product.orm.xml");

// Read all mapping documents from a directory tree.
// Assumes that any file named *.hbm.xml is a mapping document.
        //sources.addDirectory(new File("."));

// Read mappings from a particular XML file
        //sources.addFile(new File("./mapping.xml"));

// Read all mappings from a jar file.
// Assumes that any file named *.hbm.xml is a mapping document.
        //sources.addJar(new File("./entities.jar"));
    }

    public static void main(String[] args) {
        //ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().build();

        //MetadataSources sources = new MetadataSources(standardRegistry);

        //MetadataBuilder metadataBuilder = sources.getMetadataBuilder();

// Use the JPA-compliant implicit naming strategy
       // metadataBuilder.applyImplicitNamingStrategy(
        //        ImplicitNamingStrategyJpaCompliantImpl.INSTANCE);

// specify the schema name to use for tables, etc when none is explicitly specified
//        metadataBuilder.applyImplicitSchemaName("my_default_schema");

// specify a custom Attribute Converter
//        metadataBuilder.applyAttributeConverter(myAttributeConverter);

   //     Metadata metadata = metadataBuilder.build();


        StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                .configure("org/hibernate/example/hibernate.cfg.xml")
                .build();

        Metadata metadata = new MetadataSources(standardRegistry)
                //.addAnnotatedClass(MyEntity.class)
                //.addAnnotatedClassName("org.hibernate.example.Customer")
                .addResource("org/hibernate/example/Order.hbm.xml")
                //.addResource("org/hibernate/example/Product.orm.xml")
                .getMetadataBuilder()
                //.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
                .build();

        //SchemaManager schemaManager = new SchemaManagerImpl();
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.create(EnumSet.of(TargetType.SCRIPT),null);
//        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
//        MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ).buildMetadata();
//        SchemaExport schemaExport =  new SchemaExport(metadata);
//        schemaExport.create(true, true);
        //SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
                //.applyBeanManager()
        //        .build();
    }
}

根据以上查看源码后,发现实现可以这样写
仅改一改参数就可以迁移hibernate6文章来源地址https://www.toymoban.com/news/detail-860191.html

package com.kongjs.kda;

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.ExceptionHandlerCollectingImpl;
import org.hibernate.tool.schema.internal.ExceptionHandlerHaltImpl;
import org.hibernate.tool.schema.spi.*;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

public class HibernateDDL {
    public static SourceDescriptor sourceDescriptor = new SourceDescriptor() {
        public SourceType getSourceType() {
            return SourceType.METADATA;
        }

        public ScriptSourceInput getScriptSourceInput() {
            return null;
        }
    };
    public static void main(String[] args) {
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .configure("org/hibernate/example/hibernate.cfg.xml")
                .build();
        TargetDescriptor targetDescriptor = new TargetDescriptor() {
            @Override
            public EnumSet<TargetType> getTargetTypes() {
                return null;
            }

            @Override
            public ScriptTargetOutput getScriptTargetOutput() {
                return null;
            }
        };
        Map config = new HashMap(serviceRegistry.getService(ConfigurationService.class).getSettings());
        //config.put("hibernate.hbm2ddl.delimiter", this.delimiter);
        //config.put("hibernate.format_sql", this.format);
        //config.put("hibernate.hbm2ddl.import_files", this.importFiles);
        SchemaManagementTool tool = serviceRegistry.getService(SchemaManagementTool.class);
        ExceptionHandler exceptionHandler = true ? ExceptionHandlerHaltImpl.INSTANCE : new ExceptionHandlerCollectingImpl();
        ExecutionOptions executionOptions = SchemaManagementToolCoordinator.buildExecutionOptions(config, (ExceptionHandler)exceptionHandler);
        SourceDescriptor sourceDescriptor = new SourceDescriptor() {
            public SourceType getSourceType() {
                return SourceType.METADATA;
            }

            public ScriptSourceInput getScriptSourceInput() {
                return null;
            }
        };
        Metadata metadata = new MetadataSources(serviceRegistry)
                //.addAnnotatedClass(MyEntity.class)
                //.addAnnotatedClassName("org.hibernate.example.Customer")
                .addResource("org/hibernate/example/Order.hbm.xml")
                //.addResource("org/hibernate/example/Product.orm.xml")
                .getMetadataBuilder()
                //.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
                .build();

        tool.getSchemaDropper(config).doDrop(metadata, executionOptions, sourceDescriptor, targetDescriptor);

        //tool.getSchemaCreator(config).doCreation(metadata, executionOptions, ContributableMatcher.NONE,sourceDescriptor, targetDescriptor);

    }
}


到了这里,关于hibernate5 根据xml获取ddl sql语句的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Oracle获取对象的DDL创建语句

    上面图示,仅展示了如何获取用户的DDL创建语句,其他对象(如,表,索引等)的获取操作同上。

    2024年04月13日
    浏览(25)
  • ☆常用的Sql语句汇总(DDL/DML)

    里面有表注释 数据库种类 sql 备注 mysql -- 获取所有表名、视图名 show tables -- 获取 dev_test_data数据库 所有表、视图信息 select * from information_schema.tables where table_schema=\\\'dev_test_data\\\' -- 获取表名、视图名 select table_name from information_schema.tables where table_schema=\\\'dev_test_data\\\' -- 只获取表信息

    2024年02月03日
    浏览(45)
  • sql语句中的ddl和dml

    操作数据库:CRUD C(create) 创建 *数据库创建出来默认字符集为utf8 如果要更改字符集就 Create database 名称 character set gbk(字符集) *创建数据库:create database 名称 *先检查是否有该数据库在创建 create database if not exists 名称 创建db4数据库,判断是否存在,并指定字符集为gbk

    2024年02月11日
    浏览(26)
  • Oracle、达梦:☆获取数据库对象、获取对象的DDL定义语句(达梦)

    以下方式在达梦DM数据库中都能跑通,Oracle未测试所有的方式。 数据库所有对象表:包括 表、视图、物化视图、函数、存储过程……等 ①、ALL_OBJECTS视图方式 OBJECT_TYPE 解释: oracle 的ALL_OBJECTS中的OBJECT_TYPE表示: TABLE :表示表,这是用于存储和管理数据库中数据的主要结构。

    2024年02月03日
    浏览(74)
  • 【MySQL】不允许你不会SQL语句之DDL

    目录 前言: 一.DDL数据库语句 1.1语句讲解 1.2总结 二.DDL表语句 2.1语句讲解 2.2总结 三.DDL字段语句 3.1语句讲解 3.2总结 四.MySQL数据类型 五.结尾   在从零到一入门MySQL一篇中,我们对数据库已经有了一定的了解,接下来让我们学习SQL语句吧!   如果你对数据库的创建,查找,

    2024年02月08日
    浏览(45)
  • 【MySQL基础|第一篇】——谈谈SQL中的DDL语句

    个人主页:兜里有颗棉花糖 欢迎 点赞👍 收藏✨ 留言✉ 加关注💓本文由 兜里有颗棉花糖 原创 收录于专栏【MySQL学习专栏】🎈 本专栏旨在分享学习MySQL的一点学习心得,欢迎大家在评论区讨论💌 SQL语句主要分为以下4类,请看: 分类 说明 DDL (Data Definition Language) 数据定义

    2024年02月09日
    浏览(27)
  • 【从删库到跑路】MySQL系列——详细讲解SQL的DDL,DML,DQL,DCL语句

    🎊专栏【MySQL】 🍔喜欢的诗句:更喜岷山千里雪 三军过后尽开颜。 🎆音乐分享【如愿】 大一同学小吉,欢迎并且感谢大家指出我的问题🥰 内容有点多,建议大家先看目录。 建立在关系模型基础上,由多张相互连接的二维表组成的数据库 🏀使用表存储数据,格式统一,

    2024年02月09日
    浏览(39)
  • MyBatis XML 映射文件中的 SQL 语句可以分为动态语句和静态语句

    目录 静态查询: 动态查询: 静态更新: 动态更新: 静态删除: 动态删除: 动态语句和静态语句在 MyBatis 中的作用如下: 静态查询: 静态查询是指在 SQL 语句中执行固定的查询操作,查询的条件和内容是预先确定的,不会随着用户输入或其他条件的改变而改变。以下是一

    2024年01月18日
    浏览(50)
  • spring.jpa.hibernate.ddl-auto的配置

    spring.jpa.hibernate.ddl-auto  可以显式设置  spring.jpa.hibernate.ddl-auto  , 标准的 Hibernate 属性值有  none , validate , update , create , create-drop。 Spring Boot 会根据数据库是否是内嵌类型,选择一个默认值。具体的关系见下图: 内嵌类型 数据库名称 默认值 内嵌 hsqldb, h2, derby create-

    2024年02月11日
    浏览(55)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包