如何从 Hibernate 获取更多调试消息? [英] How do I get more debug messages from Hibernate?

查看:20
本文介绍了如何从 Hibernate 获取更多调试消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管在 hibernate.cfg.xml 文件中设置了一些属性,但我还是无法从 Hibernate 获得更多控制台输出(以帮助调试).例如,添加行 <property name="show_sql">true</property> 实际上并没有在控制台中显示 SQL 语句.

I haven't been able to get any more console output (to help with debugging) from Hibernate despite setting a few properties in the hibernate.cfg.xml file. For example, adding the line <property name="show_sql">true</property> did not, in fact, show SQL statements in the console.

我也尝试过使用 log4j.properties 文件的内容 - 比如设置 log4j.logger.org.hibernate=debug - 但没有成功.我错过了什么?

I've also tried playing around with the contents of the log4j.properties file - like setting log4j.logger.org.hibernate=debug - with no luck. What am I missing?

hibernate-service.xml 文件的内容是

the contents of the hibernate-service.xml file are

<server> 
    <mbean code="org.jboss.hibernate.jmx.Hibernate" 
       name="jboss.har:service=Hibernate_SMS"> 
        <attribute name="DatasourceName">java:/SMS_DS</attribute> 
        <attribute name="Dialect">org.hibernate.dialect.HSQLDialect</attribute> 
        <attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute> 
        <attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>
        <attribute name="ShowSqlEnabled">true</attribute>
    </mbean> 
</server>

不过,我不能 100% 确定这是否真的有任何影响.该 XML 文件位于处理我的数据库内容的 Eclipse 项目中,但似乎不在 JBoss 部署目录中.

I'm not 100% sure if this actually has any effect, though. That XML file is in the Eclipse project that handles my database stuff, but doesn't seem to be in the JBoss deploy directory.

编辑 2: 这绝对是作为 HAR 部署的.也就是说,我很确定我需要 hibernate.cfg.xml - 我记得当映射文档作为该文件中的条目被省略时遇到了问题.我认为 HAR 是使用 ant 生成的 - 在 build.xml 文件中有一个目标:

Edit 2: This is definitely deployed as a HAR. That said, I'm pretty sure that I need hibernate.cfg.xml - I recall having problems when a mapping document was omitted as an entry in that file. I think the HAR is generated using ant - there's a target for it in the build.xml file:

<target name="har" depends="prepare" description="Builds the Hibernate HAR file">
    <mkdir dir="${class.root}" />
    <mkdir dir="${distribution.dir}" />

    <jar destfile="${distribution.dir}/${har.name}">                    
        <!-- include the hbm.xml files  -->
        <fileset dir="${class.root}">
            <include name="**/*.hbm.xml"/>
            <include name="com/[redacted]/sms/data/dto/*.class"/>
            <include name="com/[redacted]/sms/data/dto/base/*.class"/>
        </fileset>

        <!-- include jboss-service.xml -->
        <metainf dir="${hibernate.dir}">
            <include name="hibernate-service.xml"/>
        </metainf>
    </jar>
</target>

但是当我在 hibernate-service.xml 文件中使用格式不正确的 xml 进行 ant 构建时,它不会失败.更新 即使完全删除文件也不会导致构建失败.这里有什么想法吗?结束更新

but when I do the ant build with incorrectly-formed xml in the hibernate-service.xml file, it doesn't fail. Update Even deleting the file entirely doesn't cause a build to fail. Any ideas here? End Update

听起来让 Hibernate 输出 SQL 语句应该(如果一切设置正确)完全处理它 - 这是否意味着 log4j.properties 中的设置在这里不会产生影响?- 因为这实际上确实在运行 ant 时改变了输出.

It sounds like getting Hibernate to output SQL statements should (if everything's set up correctly) take care of it entirely - does that mean that the settings in log4j.properties won't make a difference here? - because that actually does change the output when running ant.

编辑 3: 在我的数据 har 遇到其他奇怪的问题后,我完全删除了 har 文件并使用 har 重建它蚂蚁构建目标.突然间一切正常!感谢 chessplay 的无私帮助.不能说我确切地知道最后做了什么,但我宁愿承认他的努力也不愿写下并接受我自己的答案;如果你不是他",我很抱歉.

Edit 3: After running into other weird issues with my data har, I deleted the har file entirely and rebuilt it using the har ant build target. Suddenly everything's working! Thanks to chessplay for his awesome helpfulness. Can't say I know exactly what did it in the end, but I'd rather acknowledge his efforts than write and accept my own answer; My apologies if you're not a "he."

推荐答案

正确的属性名称是 hibernate.show_sql 它绝对有效.

The correct property name is hibernate.show_sql and it definitely works.

确保您的 hibernate.cfg.xml 是正确的,并且会被接收;您的 log4j.properties 文件也是如此.我知道那些看起来很愚蠢的建议,但它们占了缺少日志输出"问题的 98%.

Make sure that your hibernate.cfg.xml is the right one and it is getting picked up; same goes for your log4j.properties file. I know those seem like dumb suggestions, but they account for 98% of "missing logging output" problems.

更新:我会更新答案而不是继续添加评论.您确实需要确保您的 hibernate-service.xml 被 JBoss 选中.检查的简单方法是向其中添加语法错误(例如在某些元素上省略右大括号)并查看 JBoss 在重启期间是否爆炸:-) 它应该打包到 har 中并部署为这是您构建过程的一部分,因此如果您意识到 JBoss 没有接收到它,那就是该查看的地方.我将摆脱 hibernate.cfg.xml 中的冲突设置(例如,您指定为 mbean 属性的所有内容都不应在 hibernate.cfg.xml 中复制).就此而言,您甚至需要 hibernate.cfg.xml 吗?如果部署为 HAR,你的映射应该被自动扫描/提取.

Update: I'll update the answer rather than keep adding comments. You do need to make sure your hibernate-service.xml is picked up by JBoss. The easy way to check is to add a syntax error to it (like omit a closing brace on some element) and see if JBoss blows up during restart :-) It should be packaged into har and deployed as part of your build process, so if you realize that it's not being picked up by JBoss, that's the place to look. I would get rid of conflicting settings in hibernate.cfg.xml (e.g. everything that you're specifying as mbean attributes should not be replicated in hibernate.cfg.xml). For that matter, do you even need hibernate.cfg.xml? If deployed as HAR, your mappings should be automatically scanned / picked up.

这篇关于如何从 Hibernate 获取更多调试消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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