views:

286

answers:

3

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).

+1  A: 

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.

William
Thanks. I also tried hibernate.format_sql=true. By just opening and closing the session, the schema is produced, but no output. I'm really stumped.
User1
A: 

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

ChssPly76
A: 

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
Tommy