views:

1020

answers:

2

Hi, 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"&gt;
<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.

A: 

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
A: 

Please

Can U send me an example with those jars. I have tried but i am getting the follwing error:

com.googlecode.s2hibernate.lang.SessionInjectionException: Error! Please, check your JDBC/JDNI Configurations and Database Server avaliability. at /hib/listAllEmployees - Method: sample.EventsAction.execute() Could not open or inject a Hibernate Session in ValueStack: null

com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor.intercept(SessionTransactionInjectorInterceptor.java:177)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
BlackIce
In the end, i gave up on the hibernate plugin and used spring instead, and found it to be much nicer, if a bit heavier. I wrote a tutorial about it here, hopefully it can help you:http://www.scribd.com/doc/25244173/Java-Struts-Hibernate-Tutorial
Chris