首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

spring+Hibernate,super.getHibernateTemplate()空值错误

2012-03-19 
spring+Hibernate,super.getHibernateTemplate()空值异常Java codepackage com.daoimport java.util.List

spring+Hibernate,super.getHibernateTemplate()空值异常

Java code
package com.dao;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class UserDao extends HibernateDaoSupport {    public boolean doIsHas1(String str, String columnName) {        // TODO Auto-generated method stub        String sql = "from User u where u."+columnName+"=?";        List list = super.getHibernateTemplate().find(sql, str);        return (list.size() > 0);    }}

Java code
package com.entity;/** * User generated by MyEclipse Persistence Tools */public class User  implements java.io.Serializable {    // Fields         private Integer id;     private Integer type;     private String name;     private String identitynum;     private Byte sex;     private Integer age;     private String country;     private Integer married;     private String birthAddress;     private String address;     private String telephone;     private String email;     private Integer qqnum;     private String pwdquestion;     private String pwdanswer;     private String birthday;     private String nation;     private String password;    // Constructors    /** default constructor */    public User() {    }    /** minimal constructor */    public User(String name, String telephone, String password) {        this.name = name;        this.telephone = telephone;        this.password = password;    }        /** full constructor */    public User(Integer type, String name, String identitynum, Byte sex, Integer age, String country, Integer married, String birthAddress, String address, String telephone, String email, Integer qqnum, String pwdquestion, String pwdanswer, String birthday, String nation, String password) {        this.type = type;        this.name = name;        this.identitynum = identitynum;        this.sex = sex;        this.age = age;        this.country = country;        this.married = married;        this.birthAddress = birthAddress;        this.address = address;        this.telephone = telephone;        this.email = email;        this.qqnum = qqnum;        this.pwdquestion = pwdquestion;        this.pwdanswer = pwdanswer;        this.birthday = birthday;        this.nation = nation;        this.password = password;    }       // Property accessors    public Integer getId() {        return this.id;    }        public void setId(Integer id) {        this.id = id;    }    public Integer getType() {        return this.type;    }        public void setType(Integer type) {        this.type = type;    }    public String getName() {        return this.name;    }        public void setName(String name) {        this.name = name;    }    public String getIdentitynum() {        return this.identitynum;    }        public void setIdentitynum(String identitynum) {        this.identitynum = identitynum;    }    public Byte getSex() {        return this.sex;    }        public void setSex(Byte sex) {        this.sex = sex;    }    public Integer getAge() {        return this.age;    }        public void setAge(Integer age) {        this.age = age;    }    public String getCountry() {        return this.country;    }        public void setCountry(String country) {        this.country = country;    }    public Integer getMarried() {        return this.married;    }        public void setMarried(Integer married) {        this.married = married;    }    public String getBirthAddress() {        return this.birthAddress;    }        public void setBirthAddress(String birthAddress) {        this.birthAddress = birthAddress;    }    public String getAddress() {        return this.address;    }        public void setAddress(String address) {        this.address = address;    }    public String getTelephone() {        return this.telephone;    }        public void setTelephone(String telephone) {        this.telephone = telephone;    }    public String getEmail() {        return this.email;    }        public void setEmail(String email) {        this.email = email;    }    public Integer getQqnum() {        return this.qqnum;    }        public void setQqnum(Integer qqnum) {        this.qqnum = qqnum;    }    public String getPwdquestion() {        return this.pwdquestion;    }        public void setPwdquestion(String pwdquestion) {        this.pwdquestion = pwdquestion;    }    public String getPwdanswer() {        return this.pwdanswer;    }        public void setPwdanswer(String pwdanswer) {        this.pwdanswer = pwdanswer;    }    public String getBirthday() {        return this.birthday;    }        public void setBirthday(String birthday) {        this.birthday = birthday;    }    public String getNation() {        return this.nation;    }        public void setNation(String nation) {        this.nation = nation;    }    public String getPassword() {        return this.password;    }        public void setPassword(String password) {        this.password = password;    }} 


XML code
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.entity.User" table="user" catalog="wanhai">        <id name="id" type="java.lang.Integer">            <column name="Id" />            <generator class="native"></generator>        </id>        <property name="type" type="java.lang.Integer">            <column name="type" />        </property>        <property name="name" type="java.lang.String">            <column name="name" not-null="true" />        </property>        <property name="identitynum" type="java.lang.String">            <column name="identitynum" length="20" />        </property>        <property name="sex" type="java.lang.Byte">            <column name="sex" />        </property>        <property name="age" type="java.lang.Integer">            <column name="age" />        </property>        <property name="country" type="java.lang.String">            <column name="country" />        </property>        <property name="married" type="java.lang.Integer">            <column name="married" />        </property>        <property name="birthAddress" type="java.lang.String">            <column name="birth_address" />        </property>        <property name="address" type="java.lang.String">            <column name="address" />        </property>        <property name="telephone" type="java.lang.String">            <column name="telephone" not-null="true" />        </property>        <property name="email" type="java.lang.String">            <column name="email" />        </property>        <property name="qqnum" type="java.lang.Integer">            <column name="qqnum" />        </property>        <property name="pwdquestion" type="java.lang.String">            <column name="pwdquestion" />        </property>        <property name="pwdanswer" type="java.lang.String">            <column name="pwdanswer" />        </property>        <property name="birthday" type="java.lang.String">            <column name="birthday" />        </property>        <property name="nation" type="java.lang.String">            <column name="nation" />        </property>        <property name="password" type="java.lang.String">            <column name="password" not-null="true" />        </property>    </class></hibernate-mapping>

Java code
package com;import com.dao.UserDao;public class Test {    public static void main(String[] args) {        UserDao ud = new UserDao();        ud.doIsHas1("aaaa", "name");    }} 


applicationContext.xml
XML code
<?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.0.xsd">    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation"            value="classpath:hibernate.cfg.xml">        </property>    </bean>        <bean id="userDao" class="com.dao.UserDao">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    </beans>

hibernate.cfg.xml
XML code
<?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"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration>    <session-factory>        <property name="connection.username">root</property>        <property name="connection.url">            jdbc:mysql://localhost:3306/wanhai        </property>        <property name="dialect">            org.hibernate.dialect.MySQLDialect        </property>        <property name="myeclipse.connection.profile">wanhai</property>        <property name="connection.password">admin</property>        <property name="connection.driver_class">            com.mysql.jdbc.Driver        </property>        <mapping resource="com/entity/User.hbm.xml" />    </session-factory></hibernate-configuration>



以上是代码,在运行是报空值异常, super.getHibernateTemplate()获取为null
求帮忙解决

[解决办法]
Java code
/**         * Constructor for IsafGenericDaoSupport.         * @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for         */        public UserDao(SessionFactory sessionFactory) {            setSessionFactory(sessionFactory);        }        /**         * Create a HibernateTemplate for the given SessionFactory.         * Only invoked if populating the DAO with a SessionFactory reference!         * <p>Can be overridden in subclasses to provide a HibernateTemplate instance         * with different configuration, or a custom HibernateTemplate subclass.         * @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for         * @return the new HibernateTemplate instance         * @see #setSessionFactory         */        @Autowired(required = true)        public HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {            return new HibernateTemplate(sessionFactory);        } 

热点排行