this.getHibernateTemplate().find(hql) 空指针

来源:百度知道 编辑:UC知道 时间:2024/05/28 07:17:04
public class LoginServiceImpTest {
public static void main(String agrs[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
LoginServiceImp hw = (LoginServiceImp) ac.getBean("loginServicimp");
System.out.println(hw.checkLogin("1", "25"));
}
}
使用如上代码测试通过。发布后用页面调用出空指针。不过查询错误的Id 能通过,查正确的就空指针了。 前阵子还好的。郁闷了

是不是与更新有关系呢?
空指针代码如下:
public class LoginDaoImp extends BaseDaoImp implements LoginDao {

public List checkLogin(String u, String p) {
String hql="from Personnel where personnelId='"+u+"'";
return this.getHibernateTemplate().find(hql);
}

}
框架ssh
action 到service到dao 没有问题说明spring加载了并启动了。

System.out.println(this.getHibernateTemplate());
得到org.springframework.orm.hibernate3.HibernateTemplate@12221f6
那就是不是框架问题。

出现问题的表的结构如下:
person:person_I

楼主试试lazy="false" 就是延迟

这有篇延迟加载的介绍:

http://hi.baidu.com/gafeal/blog/item/55561824c490732cd40742b4.html

public List checkLogin(String u, String p) {
String hql="from Personnel where personnelId='"+u+"'";
//在这里加一行打印,看看是否得到了HiberanteTemplate对象,如果这里为空了,那么原因就应该是spring没有配置好,或者没有加载和启动它。
System.out.println(this.getHibernateTemplate());
return this.getHibernateTemplate().find(hql);
}

你看看是不是session超时了,可以试一下在web.xml下加入这么配置。
加入这一段试试。
<!--Hibernate Open Session in View Filter-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>