HibernateDaoSupport 中无法获取 session 在线等
DAO代码如下/***查询留言*/publicListchaLiuyan(Integerid){StringSQL="selectmingzi,neirong,creat...
DAO代码如下
/**
* 查询留言
*/
public List chaLiuyan(Integer id) {
String SQL = "select mingzi,neirong,createtime from Liuyan where company_ID = '"+id+"'";
String HQL = "from Liuyan where company_ID = '" + id + "' order by createtime desc";
System.out.println("++++++"+super.getSession(true));//取不到session 值为null
Session session = super.getSession(true);
try {
List list = session.createQuery(HQL).list();
if (list.size() != 0) {
return list;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
session.close();
}
return null;
}
DAO在Spring中已经注入了
<bean id="companyDAO" class="com.xinxi.dao.CompanyDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userInfoDAO" class="com.xinxi.dao.UserInfoDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="liuyanDAO" class="com.xinxi.dao.LiuyanDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
现在问题是
另外2个DAO可以获取Session,而这个LiuyanDAO 却无法获取Session在package窗口中,LiuyanDAO这个类上面已经有了注入Spring的标志了,就是那个蓝色的S
异常信息如下:
java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:165)
at com.xinxi.dao.LiuyanDAO.chaLiuyan(LiuyanDAO.java:51)
at com.xinxi.biz.LiuyanBIZ.chaLiuyan(LiuyanBIZ.java:31)
at com.xinxi.web.struts.action.CompanyAction.findone(CompanyAction.java:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
剩下的异常信息就不贴了
null ->session打印出来是null 展开
/**
* 查询留言
*/
public List chaLiuyan(Integer id) {
String SQL = "select mingzi,neirong,createtime from Liuyan where company_ID = '"+id+"'";
String HQL = "from Liuyan where company_ID = '" + id + "' order by createtime desc";
System.out.println("++++++"+super.getSession(true));//取不到session 值为null
Session session = super.getSession(true);
try {
List list = session.createQuery(HQL).list();
if (list.size() != 0) {
return list;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
session.close();
}
return null;
}
DAO在Spring中已经注入了
<bean id="companyDAO" class="com.xinxi.dao.CompanyDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userInfoDAO" class="com.xinxi.dao.UserInfoDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="liuyanDAO" class="com.xinxi.dao.LiuyanDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
现在问题是
另外2个DAO可以获取Session,而这个LiuyanDAO 却无法获取Session在package窗口中,LiuyanDAO这个类上面已经有了注入Spring的标志了,就是那个蓝色的S
异常信息如下:
java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:165)
at com.xinxi.dao.LiuyanDAO.chaLiuyan(LiuyanDAO.java:51)
at com.xinxi.biz.LiuyanBIZ.chaLiuyan(LiuyanBIZ.java:31)
at com.xinxi.web.struts.action.CompanyAction.findone(CompanyAction.java:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
剩下的异常信息就不贴了
null ->session打印出来是null 展开
展开全部
把你的LiuyanDAO整个类贴上来看看,还有你的session是调用父类的getSession()方法得来的,那么你的父类是什么?LiuyanDAO和其他几个DAO都是继承或实现的一个类或接口吗? 框架上的问题仅看出错的地方有可能看不出来的。
还有,我想父类的getSession()方法中有个sessionFactory的bean吧,通过这个sessionFactory才能得到session,看看你的sessionFactory的类,不过你的其他的Dao都是通过它得来的,估计这里问题不大。
还有,我想父类的getSession()方法中有个sessionFactory的bean吧,通过这个sessionFactory才能得到session,看看你的sessionFactory的类,不过你的其他的Dao都是通过它得来的,估计这里问题不大。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询