Tuesday, May 4, 2010

slf4j, Another Challenge in the Hibernate Tutorial

As I have succeeded to finally run the HSQLDB server using the maven plugin exec and java goal, I only needed to add the configuration files (hibernate.cfg.xml, Event.hbm.xml) and a utility class HibernateUtil.java in their correct location for Hibernate to work properly as mentionned in the tutorial book. Then the EventManager class will be the entry point for the application.
slf4j is a tool (slf4j-api.jar) that serves as a facade for other logging framework. These logging framework can be java.util.logging (which comes with the J2SE api), log4j, Jakarta Common Logging. What is interesting with slf4j is that you can use objects from slf4j-api.jar in your application, and then at runtime (or deployment time), the end-user of your application can decide to include the logging framework of his choice in the application classpath. The logging framework should of course be compatible with slf4j.
The Hibernate tutorial pom.xml file includes the following dependency to include slf4j in the tutorial application:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
When you deploy the tutorial application with this configuration, it throws an IllegalAccessError. Here, slf4j FAQ really helped (follow this link).
Basically Hibernate transitive dependency in Maven refers to an old version of the slf4j API which makes use of a property in StaticLoggerBinder class that has been made private since version 1.5.6. The best way to work around this is to explicitely add a dependency to slf4j-api which will overwrite the one from Hibernate. Elements for slf4j-api are:
groupId = org.slf4j
artifactId = slf4j-api
version = 1.6.0-RC0.
Then you can specify the binding element:
groupId = org.slf4j
artifactId = slf4j-simple (or slf4j-log4j12, slf4j-jdk14...)
version = 1.x.xyyy
Available versions and bindings can be found in Google using the following keywords "http://www.ibiblio.org/ maven2 slf4j".
slf4j-simple is only used in small application and logs event in stdout.

No comments: