r/javahelp • • Dec 02 '20

Hibernate: Does it eventually use SQL somewhere that I can see?

I'm debugging a massive application that uses Hibernate, which I am not familiar with.

I'm trying to find out why the application ( many confusing layers above Hibernate ) is sending out error messages about there being no data, when I can see the data in the database ( Postgres ).

Does Hibernate eventually somewhere deep down make SQL queries that I can see? Some other commands I can see such that I can learn what it is asking for so I can tell why it isn't finding it?

16 Upvotes

8 comments sorted by

•

u/AutoModerator Dec 02 '20

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

8

u/wildjokers Dec 02 '20 edited Dec 02 '20

Hibernate does generate SQL and it will show you the SQL it is generating if you set the appropriate property (show_sql=true) or configure your logging library. We can't really tell you how to do that until you tell us what framework you are using and/or how you are configuring Hibernate. Are you using Spring Boot with JPA and Spring Data? Are you just using JavaEE? What logging library are you using?

Instead of setting the special property for hibernate you can also just configure a logger if you are using something like Logback (or log4j, or whatever). This assumes you have a FILE and a STDOUT appender. Hibernate's SQL logging will just log ? for the bind variables in the queries. If you actually want to see the values it is binding then the org.hibernate.type logger will log the actual values. Do note though that the type logging is very noisy so best to only send it to a FILE instead of STDOUT

 <logger name="org.hibernate.SQL" additivity="false" level="debug">
    <appender-ref ref="FILE"/>
    <appender-ref ref="STDOUT"/>
</logger>

<logger name="org.hibernate.type" additivity="false" level="trace">
    <appender-ref ref="FILE"/>
</logger>

Configuring your logging library is the better approach since show_sql=true just prints to stdout and not to the app's log file from the logging library.

Some resources (great info at these two links but prepare yourself for battle against popups):

1

u/[deleted] Dec 02 '20

I am guessing "FILE" means that I should substitute in a path to a real file?

is ref="STDOUT" fine for getting output int stdout/my eclipse console when I run the application?

1

u/wildjokers Dec 02 '20 edited Dec 02 '20

No, FILE is a named appender. In this case it is a RollingFileAppender. Same with STDOUT except it is a ConsoleAppender.

Here is a full logback config file with the appropriate appenders and loggers configured:

<configuration scan="true" scanPeriod="30 seconds">
<variable scope="local" name="defaultPattern" value="%cyan(%date{HH:mm:ss.SSS}) [Thread.%t] %yellow(%-40c{1}) %highlight(%-5p) %msg%n"/>
<variable scope="local" name="filePattern" value="%date{HH:mm:ss.SSS} [Thread.%t] %-40c{1} %-5p %msg%n"/>
<variable scope="local" name="mainLogFileName" value="${user.home}/application.log"/>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>${defaultPattern}</pattern>
    </encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${mainLogFileName}</file>
    <append>true</append>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${mainLogFileName}.%d{yyyy-MM-dd}.gz</fileNamePattern>
        <maxHistory>3</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>${filePattern}</pattern>
    </encoder>
</appender>

<logger name="org.hibernate.SQL" additivity="false" level="debug">
    <appender-ref ref="FILE"/>
    <appender-ref ref="STDOUT"/>
</logger>

<!-- Very noisy so send to FILE only -->
<logger name="org.hibernate.type" additivity="false" level="trace">
    <appender-ref ref="FILE"/>
</logger>

<root level="info">
    <appender-ref ref="FILE"/>
    <appender-ref ref="STDOUT"/>
</root>
</configuration>

For your reading pleasure ;-) http://logback.qos.ch/manual/index.html

And yes stuff written to ConsoleAppender should show up in the console of your IDE.

If your console doesn't support colors you will need to remove those colors I have in the defaultPattern.

7

u/Tacos314 Dec 02 '20

Yes, there is even a flag to set that will log the sql.

2

u/Jtonna Dec 02 '20

This may have what you're looking for https://www.baeldung.com/sql-logging-spring-boot

1

u/TheeDean Dec 02 '20

p6Spy is what you're looking for... easy to setup and logs pure sql

-1

u/Kfct Dec 02 '20

You probably want to isolate which class file the error is popping up in first. This question's a bit vague as is.