Struts + Hibernate:@SessionTarget无法正常工作 [英] Struts + Hibernate: @SessionTarget not working

查看:241
本文介绍了Struts + Hibernate:@SessionTarget无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用> struts2-fullhibernatecore-plugin-2.2.2-GA.jar 会话中注入我的DAO类如下:

  public class UserDAO {
@SessionTarget
Session session;

@TransactionTarget
交易交易;

公共列表<用户> getUsers(){
return session.createQuery(from user)。list();


$ / code $ / pre

但是我得到了

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

根据调试输出, session transaction UserDAO 中的c> null



部分档案可供参考:

User.java
$ p code $ @
@Table(name =user)
public class User实现Serializable {

@Id
@GeneratedValue
private int id;

@Column
私人字符串名称;

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 UserDAO userDAO = new UserDAO();

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

}

hibernate.cfg.xml

 <?xml version = 1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate Configuration DTD 3.0 // ENhttp://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>根< / property>
< property name =hibernate.connection.pool_size> 10< / property>

< mapping class =com.wudi.model.User/>
< / session-factory>
< / hibernate-configuration>


解决方案

如果您想使用Hibernate Session和Transaction注入功能,您的动作映射包需要扩展包 hibernate-default



有关<$ c $的更多详细信息c> hibernate-default package



该插件提供了一个名为 hibernate-default 。这有三个拦截器堆栈用于注入功能:


  • basicStackHibernate :像Struts2 basickStack (无验证!),但具有Hibernate会话和事务注入功能。

  • defaultStackHibernate :与Struts2 defaultStack 类似,但没有Struts2验证方法(注释和XML)。而不是使用Hibernate验证框架。 c $ c> +插件的 basicStackHibernate




扩展了 hibernate-default 包,所以如果需要的话,可以使用所有默认的Struts2配置。

hibernate-default 包是抽象的,所以你可以用其他扩展它。例如:

 < package name =defaultextends =hibernate-default,json-default> 

在注释中使用 hibernate-default 配置使用

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


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>

解决方案

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 {}

这篇关于Struts + Hibernate:@SessionTarget无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆