问题
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 ?
回答1:
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
来源:https://stackoverflow.com/questions/23651482/generate-a-sql-script-from-hibernate