You are here: TWiki > Guides Web > Java > SpringHibernateFaq r3 - 09 May 2006 - 09:52 - JesseSuen


Start of topic | Skip to actions

Spring Hibernate FAQ

When starting Tomcat with Spring's ContextLoaderListener? setup in my web.xml, my app will fail to start. After checking the Tomcat logs, I see this message "SEVERE: Error listenerStart".

  • Most of the time this is a problem in your applicationContext.xml or your Hibernate mapping (.hbm.xml) file. If you aren't sure, you can enable log4j logging to find out what exactly is causing the problem. Set the logging level to WARN. This will help you pinpoint what exactly is wrong with your xml files.

How do I enable log4j logging in Tomcat?

  • Make sure you have a log4-1.x.jar in your WEB-INF/lib/ directory.
  • In your $CATALINA_HOME/shared/classes/ directory, create a log4j.properties file. Set the log4j.rootCategory to WARN or ERROR. You can use this as an example for your log4j.properties file:
log4j.defaultInitOverride = false

log4j.rootCategory=WARN, console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

When trying to get Hibernate to insert my newly created User object into the database using getHibernateTemplate().saveOrUpdate(), I get the following exception: "org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1"

  • You are probably trying to use an int or a long primitive type as your userId primary key. Since int and long are primitive types, Java will initialize it to 0. Hibernate will think you want to update the user row where id=0, which does not exist. Hibernate expects 1 change to be made but to detects 0 and will throw an Exception. The solution to this is that you should use java.lang.Long class as the id attribute of your objects. Since the attribute will be initialized to null, Hibernate will know that you are trying to create a new entry and insert it accordingly.

Hibernate doesn't insert any entry into the many-to-many link table when saving my object.

Check your inverse="true" setting for the related classes in your hibernate mapping files.

After adding objects to hibernate will successfully insert the row in my many-to-many link table. However, when I try to get the objects from using my DAO, hibernate will delete the rows in the link table.

Make sure your getter method should return the same collection instance as was assigned by Hibernate to the setter method. http://www.hibernate.org/116.html#A15

LazyInitializationException?: Failed to lazily initialize a collection - no session or session was closed

A cheap solution to this is to declare lazy="false" when defining the class in your hibernate mapping file. It may also be necessary to declare lazy="false" for the collections as well, even though lazy="false" is declared at the level tag. lazy="false" is not the ideal solution to this problem.

-- JesseSuen - 28 Apr 2006

Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r3 < r2 < r1 | More topic actions
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback