JPA仪器仪表

发布于 2024-08-03 19:53:37 字数 821 浏览 3 评论 0原文

在 JPA 中打开 EntityManagerFactory 时是否可以看到生成的 DDL?我似乎遇到了一些问题,但没有产生错误。我没有看到任何类型的日志文件,也没有输出写入 StdOut 或 StdErr。我在 src/main/resources 中有一个 log4j.properties:


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger=debug, stdout

log4j.logger.org.hibernate=debug
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.engine.CascadingAction=debug
log4j.logger.org.hibernate.tool.hbm2ddl=debug

我什至不确定该文件是否被识别。有什么方法可以让我找到答案吗?如何更好地了解 Hibernate 和/或 JPA 的操作?我基本上没有输出(除了我的程序中的 System.out.println() )。

Is it somehow possible to see the DDL produced when opening a EntityManagerFactory in JPA? I seem to be having some problems but no errors are produced. I don't see any sort of log file and no output is written to StdOut or StdErr. I have a log4j.properties in src/main/resources:


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger=debug, stdout

log4j.logger.org.hibernate=debug
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.engine.CascadingAction=debug
log4j.logger.org.hibernate.tool.hbm2ddl=debug

I'm not even sure if the file is recognized. Is there a way I can find that out? How can I get better insight into the actions of Hibernate and/or JPA? I basically have no output (except the System.out.println()'s in my program).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的痛♀有谁懂 2024-08-10 19:53:37

尝试在持久性单元内的 persistence.xml 中添加以下内容。

<properties>
    <property name="hibernate.show_sql" value="true"/>
</properties>

不太确定它是否会给您提供您正在寻找的信息,但应该会给您一些额外的输出。

Try adding the following in your persistence.xml, within the persistence unit.

<properties>
    <property name="hibernate.show_sql" value="true"/>
</properties>

Not too sure whether it will give you the info you're looking for but should give you some extra output.

じ违心 2024-08-10 19:53:37

要查明您的 log4j.properties 文件是否已被选中,您可以在其中故意引入错误(例如,拼错 ConsoleAppender 类名)。如果文件确实被拾取,Log4j 应该爆炸或者至少在控制台中显示错误。

就 DDL 而言,我使用 Hibernate 和 Spring 而不是 JPA,所以 YMMV,但是:

  1. “hibernate.show_sql”属性和“org.hibernate.SQL”调试级别都对 DDL 没有任何影响。
  2. 如果您使用 SchemaExport 工具(或者 EntityManagerFactory 在内部使用它),将“org.hibernate.tool.hbm2ddl”设置为 DEBUG 将仅打印 DDL。
  3. Hibernate 的配置(这是模式创建/更新脚本生成的根入口点)根本不打印脚本。

最好的选择是查看 EntityManagerFactory 代码,找出脚本的执行位置,并查看其周围是否有任何日志记录;然后你就会知道你需要在 log4j.properties 中配置什么包

To find out whether your log4j.properties file is picked up you can introduce a deliberate error into it (misspell ConsoleAppender class name, for example). Log4j should either blow up or at least show an error in a console if the file is indeed picked up.

As far as DDL goes,I'm using Hibernate with Spring rather than JPA so YMMV, but:

  1. Neither "hibernate.show_sql" property nor "org.hibernate.SQL" debug level have any effect on DDL.
  2. Setting "org.hibernate.tool.hbm2ddl" to DEBUG will only print DDL if you're using SchemaExport tool (or if EntityManagerFactory is using it internally).
  3. Hibernate's Configuration (which is the root entry point for schema create / update script generation) does NOT print the scripts at all.

Your best bet would be to take a look at EntityManagerFactory code to figure out where the script is executed and see if there's any logging going on around it; you would then know what package you need to configure in your log4j.properties

鸵鸟症 2024-08-10 19:53:37

这并不是说这解决了您的第一个问题,但我总是将我的应用程序名称添加到 .ConversionPattern 中。这样我就可以快速查明更改是否已应用,并将多个应用程序记录到同一文件中

log4j.appender.stdout.layout.ConversionPattern=MyApp1 - %d{ABSOLUTE} %5p %c{1}:%L - %m%n

Not that this solves your first question, but I always add mye applications name to the .ConversionPattern. That way I can quickly find out if the changes are applied, and log more than one application to the same file

log4j.appender.stdout.layout.ConversionPattern=MyApp1 - %d{ABSOLUTE} %5p %c{1}:%L - %m%n
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文