日期:2014-05-19  浏览次数:20680 次

Spring getHibernateTemplate().get(User.class,pKey)返回空
查询返回空指针异常!
我把代码贴上,希望大家解答详细一些.我是新手...

public class UserDao extends HibernateDaoSupport{

//根据主键查找用户
  public User Find(String pKey) throws DataAccessException {
  //return (User)getHibernateTemplate().get(User.class,pKey);
  User user = null;
  try{
  user = (User)getHibernateTemplate().get(User.class,pKey);
  }catch(Exception e){
  e.printStackTrace();
  }
  return user;
  }  
   
  //存储用户到数据库中
  public String Create(User user) throws DataAccessException {
  //getHibernateTemplate().merge(user);
  return (String)getHibernateTemplate().save(user);  
  }
}

Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans
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.5.xsd">


<bean id="jshSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" 
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/jsh"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="jshSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/tanling/model/bo/User.hbm.xml</value>
</list>
</property>
</bean>
</beans>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <context-param>
  <param-name>javax.faces.CONFIG_FILES</param-name>
  <param-value>/WEB-INF/faces-config.xml</param-value>
 </context-param>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
 <listener>
  <display-name>context</display-name>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>