关于SSH HibernateTemplate的值为空的问题
数据库是mysql。pojo类为:
package com.dream_jn.pojo;
import java.sql.Timestamp;
/**
* Communication entity. @author MyEclipse Persistence Tools
*/
public class Communication implements java.io.Serializable {
// Fields
private Integer communicationId;
private Integer formId;
private Integer toId;
private String message;
private Timestamp date;
private String state;
// Constructors
/** default constructor */
public Communication() {
}
/** full constructor */
public Communication(Integer formId, Integer toId, String message,
Timestamp date, String state) {
this.formId = formId;
this.toId = toId;
this.message = message;
this.date = date;
this.state = state;
}
// Property accessors
public Integer getCommunicationId() {
return this.communicationId;
}
public void setCommunicationId(Integer communicationId) {
this.communicationId = communicationId;
}
public Integer getFormId() {
return this.formId;
}
public void setFormId(Integer formId) {
this.formId = formId;
}
public Integer getToId() {
return this.toId;
}
public void setToId(Integer toId) {
this.toId = toId;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public Timestamp getDate() {
return this.date;
}
public void setDate(Timestamp date) {
this.date = date;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
}
dao层:package com.dream_jn.dao;
import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.dream_jn.pojo.Communication;
public class CommunicationDAO {
private static final Logger log = LoggerFactory
.getLogger(CommunicationDAO.class);
// property constants
public static final String FORM_ID = "formId";
public static final String TO_ID = "toId";
public static final String MESSAGE = "message";
public static final String STATE = "state";
private HibernateTemplate temp;
public HibernateTemplate getTemp() {
return temp;
}
public void setTemp(HibernateTemplate temp) {
// System.out.println("EmployeeinfoDAO.setTemp()"+temp);
this.temp = temp;
}
protected void initDao() {
// do nothing
}
public void save(Communication transientInstance) {
log.debug("saving Communication instance");
try {
temp.save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Communication persistentInstance) {
log.debug("deleting Communication instance");
try {
temp.delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Communication findById(java.lang.Integer id) {
log.debug("getting Communication instance with id: " + id);
System.out.println("3");
try {
Communication instance = (Communication) temp
.get("com.dream_jn.pojo.Communication", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Communication instance) {
log.debug("finding Communication instance by example");
try {
List results = temp.findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Communication instance with property: "
+ propertyName + ", value: " + value);
try {
String queryString = "from Communication as model where model."
+ propertyName + "= ?";
return temp.find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByFormId(Object formId) {
return findByProperty(FORM_ID, formId);
}
public List findByToId(Object toId) {
return findByProperty(TO_ID, toId);
}
public List findByMessage(Object message) {
return findByProperty(MESSAGE, message);
}
public static Logger getLog() {
return log;
}
public static String getFormId() {
return FORM_ID;
}
public static String getToId() {
return TO_ID;
}
public static String getMessage() {
return MESSAGE;
}
public static String getState() {
return STATE;
}
public List findByState(Object state) {
return findByProperty(STATE, state);
}
public List findAll() {
log.debug("finding all Communication instances");
try {
String queryString = "from Communication";
return temp.find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Communication merge(Communication detachedInstance) {
log.debug("merging Communication instance");
try {
Communication result = (Communication) temp
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Communication instance) {
log.debug("attaching dirty Communication instance");
try {
temp.saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Communication instance) {
log.debug("attaching clean Communication instance");
try {
temp.lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static CommunicationDAO getFromApplicationContext(
ApplicationContext ctx) {
return (CommunicationDAO) ctx.getBean("CommunicationDAO");
}
}
以上三个都是在配置Spring的时候自动生成的。
applicationContext.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/coll_project">
</property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/dream_jn/pojo/Communication.hbm.xml</value></list>
</property></bean>
<!-- 声明使用事务Bean -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 声明使用注解方式,调用事务管理 -->
<tx:annotation-driven transaction-manager="txManager" />
<!-- Hibernate 访问的模板代码,可以不用写 重复代码 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="CommunicationDAO"
class="com.dream_jn.dao.CommunicationDAO">
<property name="temp">
<ref bean="hibernateTemplate" />
</property>
</bean>
</beans>
之后想用一个TestAction.class类来测试:
package com.dream_jn.action;
import com.dream_jn.pojo.Communication;
import com.dream_jn.dao.CommunicationDAO;
public class TestAction {
public static void main(String[] args)throws Exception{
CommunicationDAO Comdao=null;
@SuppressWarnings("null")
Communication com = Comdao.findById(1);
System.out.println(com.getFormId());
}
}
但是一直是空指针的错误:Exception in thread "main" java.lang.NullPointerException
at com.dream_jn.action.TestAction.main(TestAction.java:14
另外调试的时候也是显示hibernateTemplate也是null求大神给点意见:
[解决办法]
extends HibernateDaoSupport