开发者

Dependency Injection failing - hibernate with struts2 using the full-hibernate-plugin

开发者 https://www.devze.com 2022-12-12 10:10 出处:网络
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'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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消