当前位置: 首页 > 面试题库 >

使用Hibernate 4生成SQL DB创建脚本

郑鸿朗
2023-03-14
问题内容

我们当前正在使用Hibernate 3,并且使用Hibernate Tools来为数据库模式生成SQL脚本。

我们使用以下Ant任务

<hibernatetool destdir="${target}">
    <jpaconfiguration persistenceunit="@{persistenceUnit}" propertyfile="@{propertyfile}"/>
    <classpath refid="@{classpathid}"/>
    <!-- the file name is relative to $destdir -->
    <hbm2ddl outputfilename="@{output}" format="true" export="false" drop="false"/>
</hibernatetool>

我们想切换到Hibernate 4:如果没有Hibernate工具,如何实现类似的目标?


问题答案:

您可以直接使用SchemaExport类生成DDL脚本:

对于Hibernate 4:

    Configuration config = new Configuration();

    Properties properties = new Properties();

    properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
    properties.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/Test"); 
    properties.put("hibernate.connection.username", "username");
    properties.put("hibernate.connection.password", "password");
    properties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
    properties.put("hibernate.show_sql", "true");
    config.setProperties(properties);

    config.addAnnotatedClass(MyMappedPojo1.class);
    config.addAnnotatedClass(MyMappedPojo2.class);
    ..................

    SchemaExport schemaExport = new SchemaExport(config);
    schemaExport.setDelimiter(";");

    /**Just dump the schema SQLs to the console , but not execute them ***/
    schemaExport.create(true, false);

Hibernate 5的更新:

    Properties properties = new Properties();

    properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
    properties.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/Test"); 
    properties.put("hibernate.connection.username", "username");
    properties.put("hibernate.connection.password", "password");
    properties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
    properties.put("hibernate.show_sql", "true");

    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(properties).build();

    MetadataSources metadataSource = new MetadataSources(serviceRegistry);
    metadataSource.addAnnotatedClass(MyMappedPojo1.class);
    metadataSource.addAnnotatedClass(MyMappedPojo2.class);
    ...........

    Metadata meta = metadataSource.buildMetadata();

    SchemaExport schemaExport = new SchemaExport();
    schemaExport.setDelimiter(";");
    schemaExport.execute(EnumSet.of(TargetType.STDOUT), Action.CREATE, meta);


 类似资料:
  • 但我得到以下错误: 已迁移到新包的该类: 然而,我没有找到在MAVEN Build中继续生成DDL创建脚本的明确方法。 那又怎样?它不是应该支持的一个主要特性吗?怎么做?

  • 如何在Java中使用构建器创建一个火花会话?我试过这个: 但我在“getOrCreate”中遇到了一个例外。是否有任何方法可以使用JavaSparkContext/SparkContext创建spark会话? 线程“main”中的异常java.lang.NoSuchmetodError:6.applyUserGroupIession.scala:860MONtUser()Lorg/apache/h

  • 本文向大家介绍Shell创建用户并生成随机密码脚本分享,包括了Shell创建用户并生成随机密码脚本分享的使用技巧和注意事项,需要的朋友参考一下 创建随机数的方法: 在Linux中有一个设备/dev/urandom是用来产生随机数序列的。利用该设备我们可以根据在需要生成随机字符串。 比如我们要产生一个8位的字母和数字混合的随机密码,可以这样: 其实,linux已经提供有个系统环境变量了。 可能有疑问

  • 创建和使用脚本 游戏对象的行为由绑定的 组件 所控制。尽管 Unity 内置的组件非常灵活多样,但是你很快就会发现它们提供的功能远远不够,为了实现你所要的游戏功能,你需要超越它们才行。Unity 支持通过 脚本 创建属于你自己的组件。在组件中,随着时间的推移,你可以触发游戏事件、修改组件属性,还可以以任何你喜欢的方式来响应用户输入。 Unity 内置支持两种编程语言: C# 一种工业标准语言,类似

  • 在你完成模型后,你可以保存模型表结构、视图或外键成一个脚本文件。导出 SQL 功能为脚本生成一个 SQL 文件。选择 工具 -> 导出 SQL。 导出 SQL 的常规设置 文件 设置输出的文件名和位置。 对象 在现有的模型中选择你想导出的对象。 导出 SQL 的高级设置 下列选项是根据你所选择图表的数据库类型:MySQL、Oracle、PostgreSQL、SQLite、SQL Server 和

  • 问题内容: 我们正在尝试找到一种语法,以从日期函数的第三个参数生成DAY | WEEK | MONTH选项。 您知道在DECLARE中使用什么正确的语法,并且应该将其转换为有效的SQL。 问题答案: 以下是BigQuery标准SQL 这些都是文字,无法进行参数化 而且,您知道-动态SQL尚不可用 因此,不幸的是,以下是我今天能想到的唯一解决方案