怪异的getHibernateTemplate().find()空指针问题

这个问题很怪.我在Junit下测试我的代码时,getHibernateTemplate().find()没有一点问题.当我把它放到Struts1.2的action下就报空指针异常.后来我改用Hibernate的session, 在query的时候还是出现同样的问题.关键只是在action中才出现,自己的测试代码中不出现.希望大家帮我看看.
异常如下:
exception
java.lang.NullPointerException
org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121)
org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:830)
org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
org.hibernate.type.EntityType.resolve(EntityType.java:303)
org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
org.hibernate.loader.Loader.doQuery(Loader.java:717)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
org.hibernate.loader.Loader.doList(Loader.java:2145)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
org.hibernate.loader.Loader.list(Loader.java:2024)
org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)
org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:849)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:840)
org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:836)
com.ace.core.dao.impl.PersonDaoBean.getTPersonByName(PersonDaoBean.java:110)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy7.getTPersonByName(Unknown Source)
com.ace.core.business.impl.PersonManagerBean.login(PersonManagerBean.java:110)
com.ace.core.struts.action.LoginAction.execute(LoginAction.java:53)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

这是我的applicationContext.xml:
[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

hibernate.cfg.xml

























PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED,readOnly











PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED,readOnly











[/code]

PerManagerBean的Login方法:
[code="java"]
public Person login(String name, String password) {
TPerson tPerson = personDao.getTPersonByName(name);
String md5Password = "";
try {
md5Password = MD5HashUtil.hashCode(password);
if(!(tPerson.getPassword().equals(md5Password))){
return null;
}
TRole tRole = tPerson.getTRole();
if(tRole.getId() == 2){
AdminBean admin = getAdminResouce();
admin.setTPerson(tPerson);
return admin;
}else if(tRole.getId() == 3){
UserBean user = getUserResouce();
user.setTPerson(tPerson);
return user;
}else if(tRole.getId() == 1){
SuperAdminBean superAdmin = getSuperAdminResouce();
superAdmin.setTPerson(tPerson);
return superAdmin;
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
[/code]

PersonDaoBean的getTPersonByName方法:
[code="java"]
public TPerson getTPersonByName(String name){
List list = getHibernateTemplate().find("from TPerson tp where tp.name=?",name);
if(list.size() != 0){
TPerson tPerson = (TPerson) list.get(0);
return tPerson;
}else
return null;
}
[/code]

2个回答

你的DAO继承了HIBERNATEDAOSUPPORT了吗?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!