Tuesday, May 11, 2010

Hibernate with MySQL Setup

Recently, I followed the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I ran into some issues and my previous posts explain how I resolved them.

In this post, I want to step you through the Hibernate setup with a MySQL DB. It is very simple and straight forward.

Prerequisite: You need to have MySQL installed/setup.

Step 1: Hibernate Configuration File
Here is the code for hibernate.cfg.xml


"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">



com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/tutorial
tutor
passwrd


1


org.hibernate.dialect.MySQL5InnoDBDialect


thread


org.hibernate.cache.NoCacheProvider


true


update









Step 2: Maven Dependency

If you are using Maven, here is the dependency you need to add (to the list of dependencies)


mysql
mysql-connector-java
5.0.5



Step 3: Create Database and add permission for Hibernate to access it
You will have to create a database (or use an existing one).
In this case my database name is 'tutorial'.
The user accessing this database with username 'tutor'@'localhost' and
password is 'passwrd'
In your console/command prompt run the following commands:

> mysql -u root -p
mysql> CREATE DATABASE tutorial;
mysql> CREATE USER 'tutor'@'localhost' IDENTIFIED BY 'passwrd';
mysql> GRANT ALL PRIVILEGES ON tutorial.* TO 'tutor'@'localhost'
-> WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;


That's all you need to start using MySQL DB with Hibernate.

Hibernate Tutorial - Fix 6 of 6 - Using Proper Version of javassist JAR Files

Preface:
I tried to follow the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I was using Apache Maven version 2.2.1 and I had to make a lot of modifications to the tutorial code (specially in pom.xml) to make it work. I want to post these fixes/code updates and maybe it might help someone who is having problems walking through the tutorial.

There were so many issues and the post was getting big so I am splitting it into different posts. you can skip reading the 'preface' section of the other posts in this series.

Problem 6 of 6:
Error in hibernate mapping file


I was able to Store and display Events. I added the Person and all the mappings to represent the many-to-many relationship between the Person and the Event. Then I tried to execute the EventManager once more, to create and add a Person to an Event, by running the following command:

mvn -e exec:java -Dexec.mainClass="org.hibernate.tutorial.EventFactory" -Dexec.args="addpersontoevent"

I was getting the following error:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building First hibernate tutorials
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[INFO] [exec:java {execution: default-cli}]
>>>>>>>>>>>>>>>>>> args[0]: add
>>>>>>>>>>>>>>>>>> in createAndStoreEvent()...
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

An association from the table PERSON_EVENTS refers to an unmapped class: events.Person
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. null
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. null
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:338)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
at org.hibernate.tutorial.util.HibernateUtil.(HibernateUtil.java:9)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:46)
at org.hibernate.tutorial.EventManager.main(EventManager.java:22)
... 6 more
Caused by: org.hibernate.MappingException: An association from the table PERSON_EVENTS refers to an unmapped class: events.Person
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1285)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1203)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 9 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 11 18:52:58 PDT 2010
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------


The culprit here is the usage of wrong class name in the Event.hbm.xml. The tutorial code is:






It should be:







That's all the bugs I found in the tutorial. I also setup MySQL DB and configured hibernate to use it instead of HSQLDB. Please see my next post for those details.

Hibernate Tutorial - Fix 5 of 6 - Using Proper Version of javassist JAR Files

Preface:
I tried to follow the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I was using Apache Maven version 2.2.1 and I had to make a lot of modifications to the tutorial code (specially in pom.xml) to make it work. I want to post these fixes/code updates and maybe it might help someone who is having problems walking through the tutorial.

There were so many issues and the post was getting big so I am splitting it into different posts. you can skip reading the 'preface' section of the other posts in this series.

Problem 5 of 6:
Using Proper Version of javassist jar files


Still trying to add an Event, by executing the EventManager, using the maven command:

mvn exec:java -Dexec.mainClass="org.hibernate.tutorial.EventFactory" -Dexec.args="store"

And the errors continued. The new error I was getting:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building First hibernate tutorials
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[WARNING] While downloading javassist:javassist:3.0
This artifact has been relocated to jboss:javassist:3.0.


[INFO] [exec:java {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

javassist.util.proxy.MethodFilter
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. null
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. null
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:338)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:31)
at org.hibernate.tutorial.util.HibernateUtil.(HibernateUtil.java:12)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:37)
at org.hibernate.tutorial.EventManager.main(EventManager.java:25)
... 6 more
Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
at org.hibernate.bytecode.javassist.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:49)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:203)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:181)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.(AbstractEntityTuplizer.java:158)
at org.hibernate.tuple.entity.PojoEntityTuplizer.(PojoEntityTuplizer.java:76)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:325)
at org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:457)
at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:131)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:261)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
... 9 more
Caused by: java.lang.ClassNotFoundException: javassist.util.proxy.MethodFilter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248
... 22 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 11 18:15:28 PDT 2010
[INFO] Final Memory: 6M/16M
[INFO] ------------------------------------------------------------------------


Again, it is clear from the logs that the necessary class is not found and i fixed this by simply changing the javassist dependency in the pom.xml.


javassist
javassist
3.4.GA



Thats out of the way, I was able to successfully execute the EventManager. Here is the success log:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building First hibernate tutorials
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[INFO] [exec:java {execution: default-cli}]
Hibernate: insert into EVENTS (EVENT_ID, EVENT_DATE, title) values (null, ?, ?)
Hibernate: call identity()
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue May 11 18:18:42 PDT 2010
[INFO] Final Memory: 6M/16M
[INFO] ------------------------------------------------------------------------

Hibernate Tutorial - Fix 4 of 6 - Maven Unable to Find the Hibernate Mapping File

Preface:
I tried to follow the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I was using Apache Maven version 2.2.1 and I had to make a lot of modifications to the tutorial code (specially in pom.xml) to make it work. I want to post these fixes/code updates and maybe it might help someone who is having problems walking through the tutorial.

There were so many issues and the post was getting big so I am splitting it into different posts. you can skip reading the 'preface' section of the other posts in this series.

Problem 4 of 6:
Maven unable to find the hibernate mapping file.


I was able to launch HSQL Server. Next step is to add an Event, by executing the EventManager, using the maven command:

mvn exec:java -Dexec.mainClass="org.hibernate.tutorial.EventFactory" -Dexec.args="store"

And I was getting the following error:
I was getting the following error:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building First hibernate tutorials
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[WARNING] While downloading javassist:javassist:3.0
This artifact has been relocated to jboss:javassist:3.0.


[INFO] [exec:java {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

resource: org/hibernate/tutorial/domain/Event.hbm.xml not found
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. null
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. null
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:338)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:31)
at org.hibernate.tutorial.util.HibernateUtil.(HibernateUtil.java:12)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:37)
at org.hibernate.tutorial.EventManager.main(EventManager.java:25)
... 6 more
Caused by: org.hibernate.MappingNotFoundException: resource: org/hibernate/tutorial/domain/Event.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:596)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1621)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1589)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1568)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
... 9 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 11 17:47:03 PDT 2010
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------


I moved the hibernate mapping files in to the 'resources' folder, saving the package directory structure.
C:\Users\Krishna\workspace\hibernate\src\main\resources\org\hibernate\tutorial\domain\Event.hbm.xml
Note: C:\Users\Krishna\workspace\hibernate is my project root.

Hibernate Tutorial - Fix 3 of 6 - Using Proper Version of slf4j JAR Files

Preface:
I tried to follow the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I was using Apache Maven version 2.2.1 and I had to make a lot of modifications to the tutorial code (specially in pom.xml) to make it work. I want to post these fixes/code updates and maybe it might help someone who is having problems walking through the tutorial.

There were so many issues and the post was getting big so I am splitting it into different posts. you can skip reading the 'preface' section of the other posts in this series.

Problem 2 of 6:
Using Proper Version of slf4j JAR Files


Still trying to launch the HSQL Serverby running the maven command:

mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/tutorial"

And I was getting the following error:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building First hibernate tutorials
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[WARNING] While downloading javassist:javassist:3.0
This artifact has been relocated to jboss:javassist:3.0.


[INFO] [exec:java {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. null
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. null
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:338)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:31)
at org.hibernate.tutorial.util.HibernateUtil.(HibernateUtil.java:12)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:37)
at org.hibernate.tutorial.EventManager.main(EventManager.java:25)
... 6 more
Caused by: java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
at org.slf4j.LoggerFactory.(LoggerFactory.java:60)
at org.hibernate.cfg.Configuration.(Configuration.java:151)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
... 9 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 11 17:21:02 PDT 2010
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------


This is due to a change in access modifier from one version of slf4j to the other.
I fixed it by using slf4j jars of version 1.5.3 (instead of 1.5.6). Make the following change in the pom.xml.


org.slf4j
slf4j-simple
1.5.3



Well, that fixed the earlier error but it was not able to find other necessary classes. The next error I got was:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building First hibernate tutorials
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[WARNING] While downloading javassist:javassist:3.0
This artifact has been relocated to jboss:javassist:3.0.


[INFO] [exec:java {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

name
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. null
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. null
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:338)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:31)
at org.hibernate.tutorial.util.HibernateUtil.(HibernateUtil.java:12)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:37)
at org.hibernate.tutorial.EventManager.main(EventManager.java:25)
... 6 more
Caused by: java.lang.NoSuchFieldError: name
at org.slf4j.impl.SimpleLogger.(SimpleLogger.java:83)
at org.slf4j.impl.SimpleLoggerFactory.getLogger(SimpleLoggerFactory.java:67)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:103)
at org.hibernate.cfg.Configuration.(Configuration.java:151)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
... 9 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 11 17:45:08 PDT 2010
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------


I fixed that by adding another dependency as follows:


org.slf4j
slf4j-api
1.5.3



now my pom.xml looks like this:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
org.hibernate.tutorials
hibernate-tutorial
1.0-SNAPSHOT
First hibernate tutorials


org.hibernate
hibernate-core
3.3.1.GA


javax.servlet
servlet-api
[2.4,)


org.slf4j
slf4j-simple
1.5.3


org.slf4j
slf4j-api
1.5.3


javassist
javassist
[3.0,)


junit
junit
3.8.1
test


hsqldb
hsqldb
1.8.0.10



${artifactId}


Hibernate Tutorial - Fix 2 of 6 - Missing JAR for HSQLDB Server

Preface:
I tried to follow the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I was using Apache Maven version 2.2.1 and I had to make a lot of modifications to the tutorial code (specially in pom.xml) to make it work. I want to post these fixes/code updates and maybe it might help someone who is having problems walking through the tutorial.

There were so many issues and the post was getting big so I am splitting it into different posts. you can skip reading the 'preface' section of the other posts in this series.

Problem 2 of 6:
Missing jar for HSQLDB Server


To Launch the HSQL Server, the tutorial tells you to run the maven command:

mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/tutorial"

And I was getting the following error:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building First hibernate tutorials
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[WARNING] While downloading javassist:javassist:3.0
This artifact has been relocated to jboss:javassist:3.0.


[INFO] [exec:java {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. org.hsqldb.Server

[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. org.hsqldb.Server
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. org.hsqldb.Server
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:338)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: java.lang.ClassNotFoundException: org.hsqldb.Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:276)
at java.lang.Thread.run(Thread.java:619)
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 11 15:52:06 PDT 2010
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------

It is clear from the logs that the necessary class is not found and I fixed this by simply adding a dependency in the pom.xml.

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
org.hibernate.tutorials
hibernate-tutorial
1.0-SNAPSHOT
First hibernate tutorials


org.hibernate
hibernate-core
3.3.1.GA


javax.servlet
servlet-api
[2.4,)


org.slf4j
slf4j-simple
1.5.6


javassist
javassist
[3.0,)


junit
junit
3.8.1
test


hsqldb
hsqldb
1.8.0.10



${artifactId}


Hibernate Tutorial - Fix 1 of 6 - Missing Version Information In The POM

I tried to follow the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I was using Apache Maven version 2.2.1 and I had to make a lot of modifications to the tutorial code (specially in pom.xml) to make it work. I want to post these fixes/code updates and maybe it might help someone who is having problems walking through the tutorial.

There were so many issues and the post was getting big so I am splitting it into different posts. you can skip reading the 'preface' section of the other posts in this series.

Problem 1 of 6:
Missing version information in the pom.xml


I started the tutorial to go back to basics and brush-up on my hibernate knowledge, and I was really excited about learning Maven in the process. Just a few steps into the tutorial (section 1.1.5), I was trying to compile using the maven command:

mvn compile


And I hit my first road-block - I was getting the following error:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error building POM (may not be this project's POM).


Project ID: org.hibernate.tutorials:hibernate-tutorial
POM Location: C:\Users\Krishna\workspace\hibernate\pom.xml
Validation Messages:

[0] 'dependencies.dependency.version' is missing for org.hibernate:hibernate-core:jar
[1] 'dependencies.dependency.version' is missing for javax.servlet:servlet-api:jar
[2] 'dependencies.dependency.version' is missing for org.slf4j:slf4j-simple:jar
[3] 'dependencies.dependency.version' is missing for javassist:javassist:jar


Reason: Failed to validate POM for project org.hibernate.tutorials:hibernate-tutorial at C:\Users\Krishna\workspace\hibernate\pom.xml


[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.reactor.MavenExecutionException: Failed to validate POM for project org.hibernate.tutorials:hibernate-tutorial at C:\Users\Krishna\workspace\hibernate\pom.xml
at org.apache.maven.DefaultMaven.getProjects(DefaultMaven.java:404)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:272)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.project.InvalidProjectModelException: Failed to validate POM for project org.hibernate.tutorials:hibernate-tutorial at C:\Users\Krishna\workspace\hibernate\pom.xml
at org.apache.maven.project.DefaultMavenProjectBuilder.processProjectLogic(DefaultMavenProjectBuilder.java:1077)
at org.apache.maven.project.DefaultMavenProjectBuilder.buildInternal(DefaultMavenProjectBuilder.java:880)
at org.apache.maven.project.DefaultMavenProjectBuilder.buildFromSourceFileInternal(DefaultMavenProjectBuilder.java:508)
at org.apache.maven.project.DefaultMavenProjectBuilder.build(DefaultMavenProjectBuilder.java:200)
at org.apache.maven.DefaultMaven.getProject(DefaultMaven.java:604)
at org.apache.maven.DefaultMaven.collectProjects(DefaultMaven.java:487)
at org.apache.maven.DefaultMaven.getProjects(DefaultMaven.java:391)
... 12 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue May 11 15:18:01 PDT 2010
[INFO] Final Memory: 1M/15M
[INFO] ------------------------------------------------------------------------


I fixed it by adding the necessary version information. Here is the updated pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
org.hibernate.tutorials
hibernate-tutorial
1.0-SNAPSHOT
First hibernate tutorials

${artifactId}



org.hibernate
hibernate-core
3.3.1.GA


javax.servlet
servlet-api
[2.4,)


org.slf4j
slf4j-simple
1.5.6


javassist
javassist
[3.0,)


junit
junit
3.8.1
test



Monday, April 5, 2010

List of recomended Books

Here are some must read books that I recommend every programmer must read:

1) The Pragmatic Programmer: From Journeyman to Master.

2) Getting Real

3) Head First design patterns

* I will be updating this list from time to time.

How to become a better programmer

Like most good things, becoming a better programmer is not a simple thing. It is a choice that you have to make. Then commit yourself to the continuous process of self improvement. Knowledge of the programming language is just one facet. I strongly recommend that you start by reading the book "The Pragmatic Programmer: From Journeyman to Master".

The best blog entry I found on this topic is "4 ways to become a better programmer" on programming4scientists.com. The author summarizes the several things you need to do in to four categories - Reading, writing, reviewing and contributing.

Another blog entry I found is "Becoming a Better Programmer". The list here again starts with the book "The Pragmatic Programmer".

Enough said about what I found and what I think one should do to better them selves. I am in the process my self and this blog is part of it.

What is this blog about?

This blog is about how to become a better programmer. I think it is a continuous process and hence the name 'Kaizen Programmer'.

I am creating this blog to document the different resources that I come across as well as the tips and tricks of programming. This documentation is for myself to begin with and maybe help someone who is looking for resources in this area.