In book “Java Persistence with Hibernate” I came across a line that says:
Hibernate produces all trivial CRUD SQL at startup. It caches the SQL statements
internally for future use, thus avoiding any runtime cost of SQL generation for the
most common operations.
To see the startup log, it says:
Enable DEBUG logging for the org.hibernate.persister.entity package, and watch (or search) the
Hibernate startup log.
I have a trivial application with one entity Message that i’m persisting in the database.
I have enable show_sql=true in my config file. You could see my config file below:
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/ajax</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">XXXX</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- Show and print nice SQL on stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- List of XML mapping files -->
<mapping resource="org/myapp/hibernate/first/Message.hbm.xml" />
</session-factory>
My Log4J.properties:
# Direct log messages to stdout
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
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Hibernate logging options (INFO only shows startup messages)
log4j.logger.org.hibernate=DEBUG
# Log JDBC bind parameter runtime arguments
log4j.logger.org.hibernate.type=DEBUG
log4j.logger.org.hibernate.persister.entity=DEBUG
log4j.logger.org.hibernate.SQL=DEBUG
With these settings, I could see the SQL getting printed on console when I save/update my entity.
but what I do not see is Start up log (that I mentioned at top). Hibernate did generate the SQL, but not at the start of application, it happened only when I started dealing with Entities.
What is wrong here?
It sounds like you’re expecting that at start up, you’ll see a series of SQL queries roll by. That’s not what happens.
While Hibernate is computing the needed sql queries, it’s not actually running them, so you don’t see them via the “show_sql=true” logging directive.
But if you have errors in named queries, for example, it will throw an exception at start up, which is one of the reasons I find using named queries very useful.