Struts + Hibernate: @SessionTarget not working

2020-01-27 06:50发布

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>

1条回答
够拽才男人
2楼-- · 2020-01-27 07:24

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 {}
查看更多
登录 后发表回答