问题描述
我为我的Java Swing应用程序使用Hibernate 4.3.5-Final,并且执行了许多 UDPATE
, INSERT
和 DELETE
(HQL或 Criteria
)。
现在,我想要做的是导出一个在我的数据库上完成的所有修改的SQL脚本,但我不知道该怎么做。该脚本只需要包含修改(而不是创建表),并将它们放在 .sql
文件中(导出的文件路径将由用户选择)
你有什么想法可以解决我的问题吗?
预先感谢!
:有些论坛讨论 p6spy
,它可以回答我的问题吗?
p6spy 应该在这里帮助。
,下面应该为你做的工作:在你的应用程序中启用 p6spy
(
- 查看官方文档:)
之后您基本上有2个选项:
- :,通过以下方式启用它:
appender = com.p6spy.engine.spy.appender.BatchFileLogger
inspy.properties
(但尚未公布,请参阅:关于实现的想法)并配置它spy.properties
via:logMessageFormat = FooFormat
- set
sqlexpression
以匹配您需要的查询 - 限制CREATE / ALTER / ... TABLE / SEQUENCE /...
(请参阅官方文档:)
仍然有一些棘手的问题,例如:
$ ul
databaseDialectDateFormat
属性(可以在不修改的情况下重播输出)。有关一些常用数据库的灵感,请参阅p6spy自身的单元测试: 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:
- enable
p6spy
in your app (see official docs: http://p6spy.github.io/p6spy/)
afterwards you have basically 2 options:
- use provided:
BatchFileLogger
, enabling it via:appender=com.p6spy.engine.spy.appender.BatchFileLogger
inspy.properties
(it's however undocumented yet, see: https://github.com/p6spy/p6spy/issues/119)
OR
- 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 inspy.properties
via:logMessageFormat=FooFormat
- set
sqlexpression
to match the queries you need - restrictingCREATE/ALTER/... TABLE/SEQUENCE/...
(see official docs: http://p6spy.github.io/p6spy/2.0/configandusage.html)
still there are some tricky points, like:
databaseDialectDateFormat
property (to be able to kind of replay the output without modifications). For inspiration for some of the common databases, see the unit tests of p6spy itself: https://github.com/p6spy/p6spy/tree/master/src/test/resources/com/p6spy/engine/spy
这篇关于从Hibernate生成一个SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!