Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

java - Struts + Hibernate: @SessionTarget not working

I am using struts2-fullhibernatecore-plugin-2.2.2-GA.jar to inject a session in my DAO class like below:

public class UserDAO {
    @SessionTarget
    Session session;

    @TransactionTarget
    Transaction transaction;

    public List<User> getUsers() {
        return session.createQuery("from user").list();
    }
}

But I got

java.lang.NullPointerException
  com.wudi.DAO.UserDAO.getUsers(UserDAO.java:28)
  com.wudi.action.UserListAction.execute(UserListAction.java:24)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  java.lang.reflect.Method.invoke(Method.java:606)
  ...

According to debugging output, session and transaction in the UserDAO are null.

Some files for reference:

User.java:

@Entity
@Table(name = "user")
public class User implements Serializable {

    @Id
    @GeneratedValue
    private int id;

    @Column
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

UserListAction.java:

public class UserListAction extends ActionSupport {
    private List<User> users;
    private UserDAO userDAO = new UserDAO();

    @Override
    public String execute() throws Exception {
        users = userDAO.getUsers();
        return SUCCESS;
    }

}

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sample?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.pool_size">10</property>

    <mapping class="com.wudi.model.User" />
  </session-factory>
</hibernate-configuration>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use the Hibernate Session and Transaction injection capability, your action mapping package need to extend the package hibernate-default.

More details about hibernate-default package

The plugin provides a mapping package called hibernate-default. And this has three interceptor stacks indicated for injection capabilities:

  • basicStackHibernate: Like Struts2basickStack (NO validations!), but with Hibernate session and transaction injections capability.

  • defaultStackHibernate: Like Struts2 defaultStack, but without Struts2 validation methods (annotation and XML). Uses Hibernate Validation framework instead.

  • defaultStackHibernateStrutsValidation: Struts2 defaultStack+ plugin's basicStackHibernate.

This package extends the hibernate-default package, so all default Struts2 configurations can be used if you need.

hibernate-default package is abstract, so you can extends this with other. For example:

<package name="default" extends="hibernate-default,json-default" >

To use hibernate-default with an annotation configuration use

@ParentPackage("hibernate-default")
@InterceptorRef("basicStackHibernate")
public class YourAction extends ActionSupport {}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...