从Hibernate生成一个SQL脚本 [英] Generate a SQL script from Hibernate

查看:472
本文介绍了从Hibernate生成一个SQL脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的Java Swing应用程序使用Hibernate 4.3.5-Final,并且执行了许多 UDPATE INSERT DELETE (HQL或 Criteria )。

现在,我想要做的是导出一个在我的数据库上完成的所有修改的SQL脚本,但我不知道该怎么做。该脚本只需要包含修改(而不是创建表),并将它们放在 .sql 文件中(导出的文件路径将由用户选择)



你有什么想法可以解决我的问题吗?

预先感谢!



:有些论坛讨论 p6spy ,它可以回答我的问题吗?

p6spy 应该在这里帮助。



,下面应该为你做的工作:在你的应用程序中启用 p6spy


  1. 查看官方文档: http://p6spy.github.io/p6spy/

之后您基本上有2个选项:


  1. BatchF ileLogger ,通过以下方式启用它: appender = com.p6spy.engine.spy.appender.BatchFileLogger in spy.properties (但尚未公布,请参阅: https:// >


    1. 实现自定义 com.p6spy.engine.spy.appender.MessageFormattingStrategy ,这将返回仅限于sql(请参阅: https://stackoverflow.com/a/23521623/1581069 关于实现的想法)并配置它 spy.properties via: logMessageFormat = FooFormat

    2. set sqlexpression 以匹配您需要的查询 - 限制 CREATE / ALTER / ... TABLE / SEQUENCE /...(请参阅官方文档: http://p6spy.github.i o / p6spy / 2.0 / configandusage.html

    仍然有一些棘手的问题,例如:



    $ ul
  2. databaseDialectDateFormat 属性(可以在不修改的情况下重播输出)。有关一些常用数据库的灵感,请参阅p6spy自身的单元测试: https://github.com/p6spy/p6spy/tree/master/src/test/resources/com/p6spy/engine/spy


  3. I'm using Hibernate 4.3.5-Final for my Java Swing application and I do many UDPATE, INSERT and DELETE with it (in HQL or with Criteria).

    Now, what I'm trying to do is to export a SQL script of all modifications done on my database but I don't know how to do that. This script need to contain only the modifications (and not the creation of the tables) and put them on a .sql file (exported file path will be chosen by the user)

    Do you have any ideas that can solve my problem ?

    Thank you in advance !

    [Edit] : Some forums talk about p6spy, can it answer to my problem ?

    解决方案

    p6spy should help here.

    In general, following should do the job for you:

    1. enable p6spy in your app (see official docs: http://p6spy.github.io/p6spy/)

    afterwards you have basically 2 options:

    1. use provided: BatchFileLogger, enabling it via: appender=com.p6spy.engine.spy.appender.BatchFileLogger in spy.properties (it's however undocumented yet, see: https://github.com/p6spy/p6spy/issues/119)

    OR

    1. implement custom com.p6spy.engine.spy.appender.MessageFormattingStrategy, that would be returning sql only (see: https://stackoverflow.com/a/23521623/1581069 for idea on implementation) and configure it in spy.properties via: logMessageFormat=FooFormat
    2. set sqlexpression to match the queries you need - restricting CREATE/ALTER/... TABLE/SEQUENCE/... (see official docs: http://p6spy.github.io/p6spy/2.0/configandusage.html)

    still there are some tricky points, like:

    这篇关于从Hibernate生成一个SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆