部署时执行 SQL 脚本
在部署 JavaEE/JPA(eclipseLink) 项目后,我也需要执行小 sql 脚本(插入一些数据) glassFish - 你认为最好的方法是什么。我知道 hibernate 有非常干净的方法来做到这一点,不幸的是我正在使用 EclipseLink - 那么你认为启动后的构造和 singletonBean 会做什么,我认为 sql-maven-plugin 也是可能的 - 我也看到了使用的想法SessionCustomizer 最后一个非常好,但我只需要在部署时运行该脚本?还有其他想法如何让它变得又漂亮又干净吗?
i need to execute little sql script(insert some data) after deploying JavaEE/JPA(eclipseLink) project too glassFish - what do you thing would be the best way to do that. I know that hibernate has very clean way to do that unfortunately I'm using EclipseLink - so what do u think post Construct and singletonBean on start up would do, i think sql-maven-plugin is also possible - i also saw idea to use SessionCustomizer the last one is quite nice but i only need to run that scrip on deploy ? Any other ideas how to do it nice and clean ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该看看 DbMaintain。
您只需将 sql 文件添加到类路径并配置一下 DbMaintain 即可。部署后,DbMaintain 将执行您的脚本。
还有一个教程。
此致,
克里斯
Maybe you should have a look at DbMaintain.
You just have to add the sql-files to your classpath and configure DbMaintain a bit. After deployment DbMaintain will perform your scripts.
There is also a tutorial.
Best regards,
Chris
我使用 Wildfly,但我认为你可以在 Glassfish 中做同样的事情。
在持久性单元中添加元素:
在 META-INF 中使用您的查询创建 sql 文件。
第一个元素表示从哪里读取“插入/更新”查询。
第二个元素说“首先从实体创建表,然后执行 DDL 脚本”
第三个元素说从哪里读取 DDL 查询作为 ALTER TABLE/CREATE VIEW,...
注意:我不知道不知道这是 eclipselink 限制还是 Wildfly 问题,但查询必须是连续的:解析器在 处结束执行;或在回车符处,这样的查询:
变成 2 个查询,一个用于行,它们是错误的
I use Wildfly but i think you can do the same thing in Glassfish.
In persistence unit add the element:
In the META-INF create the sql-files with your queries.
The first element say from where read the "insert/update" query.
The second element say "First create the tables from entities, then execute DDL scripts"
The third element say from where to read the DDL query as ALTER TABLE/CREATE VIEW, ...
Pay attention : i don't know if it's a eclipselink limitation or a Wildfly problem, but the query must be in a row: the parser end the execution at ; or at carriage return so a query like:
became 2 queries, one for row and they are wrong