0

i'm trying to use hibernate in a struts2 example using the struts2 full hibernate plugin (http://code.google.com/p/full-hibernate-plugin-for-struts2).

I've placed all the Jars in my lib folder:

antlr-2.7.6.jar
commons-collections-3.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
dom4j-1.6.1.jar
freemarker-2.3.15.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
jtds-1.2.4.jar
log4j-1.2.15.jar
ognl-2.7.3.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-test-2.5.6.jar
spring-web-2.5.6.jar
struts2-core-2.1.8.jar
struts2-fullhibernatecore-plugin-1.5-GA.jar
struts2-spring-plugin-2.1.8.jar
xwork-core-2.1.6.jar

And here is the code to my action class:

package sample;
import org.hibernate.Session;
import data.*;
import java.util.*;

public class Events {
  org.hibernate.Session hibernateSession;

  public void setHibernateSession(org.hibernate.Session hibernateSession) throws Exception {
    this.hibernateSession = hibernateSession;
  }
  public void sethibernateSession(org.hibernate.Session hibernateSession) throws Exception {
    this.hibernateSession = hibernateSession;
  }

  @SuppressWarnings("unchecked")
  public String execute() {

    List<Event> events = hibernateSession.createQuery("from Event").list();

    for (Event theEvent : events) {
      // blah blah...
    }

    return "success";
  }
}

I've supposedly configured the full hibernate plugin to inject the hibernate session into my action class, with this configuration in the struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="hibernatePlugin.sessionTarget" value="hibernateSession" />
  <constant name="hibernatePlugin.transactionTarget" value="hibernateTransaction" />
  ..etc..
</struts>

But my action class always falls over when it tries to access the hibernateSession variable, it is always null. So the dependency injection is failing it seems.

Any ideas why? Please help, thanks a lot. Also ask if you need to see any other config files.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Chris
  • 39,719
  • 45
  • 189
  • 235
  • Okay i finally got log4j working (my properties file was in the wrong folder!) and i'm getting this: Caught Exception while registering Interceptor class com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor - interceptor - jar:file:/C:/chris/eclipse_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Sample/WEB-INF/lib/struts2-fullhibernatecore-plugin-1.5-GA.jar!/struts-plugin.xml:14:161 – Chris Nov 19 '09 at 04:14
  • Seems to be crashing while loading this line of the struts-plugin.xml: – Chris Nov 19 '09 at 04:18
  • Found this further down the stack trace: Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration at com.googlecode.s2hibernate.struts2.plugin.interceptors.GenericInterceptor.setStrutsDevMode(GenericInterceptor.java:33) – Chris Nov 19 '09 at 04:23
  • So now i add the hibernate annotations jar file to my lib folder, and get a different exception now: ClassNotFoundException: org.hibernate.annotations.common.reflection.ReflectionManager – Chris Nov 19 '09 at 04:25
  • Okay i fixed that by adding the 'hibernate-commons-annotations.jar' library that was in the annotations package... now i'm getting: java.lang.NoClassDefFoundError: javax/persistence/JoinTable – Chris Nov 19 '09 at 04:41
  • Okay i just added EVERY jar i found in the hibernate annotations download, so now i've got this error: Error! Please, check your JDBC/JDNI Configurations and Database Server avaliability. at /sample/Events - Method: sample.Events.execute() Could not open or put a Hibernate Transaction in ValueStack: Error setting expression 'hibernateTransaction' with value 'org.hibernate.transaction.JDBCTransaction@1798928 – Chris Nov 19 '09 at 04:46
  • Okay my action class didn't have a setter for the hibernate transaction for the DI to use, now it does and it works! Here: public void setHibernateTransaction(org.hibernate.Transaction hibernateTransaction) { this.hibernateTransaction = hibernateTransaction; } – Chris Nov 19 '09 at 05:28
  • So its all fixed now. A bit disappointed in the lack of help here, i'm finding as a rule its a lot easier to find help with C# problems than java problems on the net. Just an observation. – Chris Nov 19 '09 at 05:34
  • Oh yeah, forgot to mention, i removed a whole bunch of spring jars, i think that helped (not sure). – Chris Nov 19 '09 at 05:42

1 Answers1

0

Okay this was a conglomeration of problems/solutions:

  1. log4j.properties needed to be in the '\src' folder (root of the classpath)
  2. Removed all the spring related jars
  3. Added ALL the jars from the hibernate-annotations download to the lib folder
  4. Added a setHibernateTransaction setter to my action class for the DI to inject into.
Chris
  • 39,719
  • 45
  • 189
  • 235