SSH获取SESSION时报NullPointException

08-05-16 lxbhxwl

我的JAVA文件和配置如下:

// 在此输入java代码
package org.hxwl.tm.DAOImpl;

import java.util.List;

import org.hibernate.Query;
import org.hxwl.tm.IDAO.IAdDAO;
import org.hxwl.tm.vo.Ad;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AdDAOImpl extends HibernateDaoSupport implements IAdDAO {
	
	@Override
	public boolean isEmpty() throws Exception {
		boolean flag = false;
		String hsql = "from Ad";
		Query q = super.getSession().createQuery(hsql);
		List list = q.list();
		if(list.size() > 0){
			flag = true;
		}
		return flag;
	}

	@Override
	public List findAll() throws Exception {
		String hsql = "from Ad as a order by  a.id desc";
		Query q = super.getSession().createQuery(hsql);
		List list = q.list();
		return list;
	}

	@Override
	public void delete(int id) throws Exception {
		String hsql = "delete from Ad where id = ?";
		Query q = super.getSession().createQuery(hsql);
		q.setInteger(0,id);
		q.executeUpdate();
	}

	@Override
	public void save(Ad ad) throws Exception {
		System.out.println(ad.getDate());
		super.getSession().save(ad);
	}

	@Override
	public void update(Ad ad) throws Exception {
		String hsql = "update Ad set dept=?,name=?,sourse=?,date=? where id=?";
		Query q = super.getSession().createQuery(hsql);
		
		q.setString(0, ad.getDept());
		q.setString(1, ad.getName());
		q.setString(2, ad.getSourse());
		q.setDate(3, ad.getDate());
		q.setInteger(4, ad.getId());
		q.executeUpdate();
	}

	@Override
	public Ad findById(int id) throws Exception {
		String hsql = "from Ad as a where a.id = ?";
		Query q = super.getSession().createQuery(hsql);
		q.setInteger(0, id);
		List list = q.list();
		Ad ad = (Ad)list.get(0);
		return ad;
	}
	
	public Ad findNewAd(){
		String hsql2 = "from Ad as a where a.id = ?";
		String hsql1 = "select MAX(id) from Ad";
		Query q1 = super.getSession().createQuery(hsql1);
		int id = new Integer(q1.list().get(0).toString());
		System.out.println(id);
		Query q2 = super.getSession().createQuery(hsql2);
		q2.setInteger(0, id);
		Ad ad = (Ad)q2.list().get(0);
		return ad;
	}
}

<p>

lxbhxwl
2008-05-16 16:02

// 在此输入java代码

package org.hxwl.tm.DAOImpl;

import java.util.List;

import org.hibernate.Query;
import org.hxwl.tm.IDAO.ITeacherDAO;
import org.hxwl.tm.vo.Teacher;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class TeacherDAOImpl extends HibernateDaoSupport implements ITeacherDAO {

	@Override
	public void delete(String name) throws Exception {
		String hsql = "delete from Teacher as t where t.name = ?";
		Query q = super.getSession().createQuery(hsql);
		q.setString(0, name);
		q.executeUpdate();
	}

	@Override
	public List findAll() throws Exception {
		String hsql = "from Teacher";
		Query q = super.getSession().createQuery(hsql);
		List list = q.list();
		return list;
	}

	@Override
	public Teacher findByName(String name) throws Exception {
		String hsql = "from Teacher as t where t.name = ?";
		Query q = super.getSession().createQuery(hsql);
		q.setString(0, name);
		Teacher teacher = (Teacher)q.list().get(0);
		return teacher;
	}

	@Override
	public void save(Teacher teacher) throws Exception {
		System.out.println("测试insert  DAOImpl");
		System.out.println(teacher.getName());
		System.out.println(teacher.getBirth());
		super.getSessionFactory().openSession().save(teacher);
//		super.getSession().save(teacher);
	}

	@Override
	public boolean isExists(String name) throws Exception {
		String hsql = "from Teacher as t where t.name = ?";
		Query q = super.getSession().createQuery(hsql);
		q.setString(0, name);
		List list = q.list();
		if(list.size()>0)
			return true;
		else
			return false;
	}

	@Override
	public boolean login(Teacher teacher) throws Exception {
		boolean flag = false;
		String hsql = "from Teacher as t where t.name=? and t.password=?";
		Query q = super.getSession().createQuery(hsql);
		q.setString(0, teacher.getName());
		q.setString(1, teacher.getPassword());
		List all = q.list();
		if (all.size() > 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public void update(Teacher teacher) throws Exception {
		super.getSession().update(teacher);

	}

}

<p>

lxbhxwl
2008-05-16 16:04

// 在此输入java代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="dataSourse"
		class="org.springframework.jndi.JndiObjectFactoryBean"
		destroy-method="close">
		<property name="jndiName" value="java:comp/env/jdbc/tm"></property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSourse" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.SQLServerDialect
				</prop>
				<!-- 表示允许自动提交 -->
				<prop key="hibernate.connection.autocommit">true</prop>
				<!-- 显示sql语句 -->
				<prop key="hibernate.show_sql">false</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>org/hxwl/tm/vo/Admin.hbm.xml</value>
				<value>org/hxwl/tm/vo/Ad.hbm.xml</value>
				<value>org/hxwl/tm/vo/Teacher.hbm.xml</value>
				<value>org/hxwl/tm/vo/Stipend.hbm.xml</value>
				<value>org/hxwl/tm/vo/ResearchCase.hbm.xml</value>
				<value>org/hxwl/tm/vo/TeachResearchCase.hbm.xml</value>
				<value>org/hxwl/tm/vo/AuditionCase.hbm.xml</value>
				<value>org/hxwl/tm/vo/CheckWork.hbm.xml</value>
				<value>org/hxwl/tm/vo/EduCase.hbm.xml</value>
				<value>org/hxwl/tm/vo/Evection.hbm.xml</value>
				<value>org/hxwl/tm/vo/OperationCase.hbm.xml</value>
				<value>org/hxwl/tm/vo/RewardsAndPunishment.hbm.xml</value>
				<value>org/hxwl/tm/vo/TeacherValueByStudent.hbm.xml</value>
				<value>org/hxwl/tm/vo/TrainCase.hbm.xml</value></list>
		</property>
	</bean>

	<bean id="hibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate"
		abstract="false" lazy-init="default" autowire="default"
		dependency-check="default">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
<p>

lxbhxwl
2008-05-16 16:05

续:

<!-- interface -->
	<bean id="iAdminDAO" class="org.hxwl.tm.IDAO.IAdminDAO" abstract="true"></bean>
	<bean id="iAdDAO" class="org.hxwl.tm.IDAO.IAdDAO" abstract="true"></bean>
	<bean id="iTeacherDAO" class="org.hxwl.tm.IDAO.ITeacherDAO" abstract="true"></bean>

<!-- daoImpl -->
	<bean id="iAdminDAOImpl" class="org.hxwl.tm.DAOImpl.AdminDAOImpl"
		parent="iAdminDAO">
		<property name="hibernateTemplate">
			<ref bean="hibernateTemplate" />
		</property>
	</bean>
	<bean id="iAdDAOImpl" class="org.hxwl.tm.DAOImpl.AdDAOImpl"
		parent="iAdDAO">
		<property name="hibernateTemplate">
			<ref bean="hibernateTemplate" />
		</property>
	</bean>
	<bean id="iTeacherDAOImpl" class="org.hxwl.tm.DAOImpl.TeacherDAOImpl"
		parent="iTeacherDAO">
		<property name="hibernateTemplate">
			<ref bean="hibernateTemplate" />
		</property>
	</bean>

<!--  struts action  -->

<!-- others -->	
	<bean name="/index" class="org.hxwl.tm.struts.action.IndexAction">
		<property name="adDAOImpl">
			<ref bean="iAdDAOImpl" />
		</property>
	</bean>
	
<!-- admin -->	
	<bean name="/adminLogin"
		class="org.hxwl.tm.struts.action.AdminAction">
		<property name="admindao">
			<ref bean="iAdminDAOImpl" />
		</property>
	</bean>
	<bean name="/jsp/admin/admin"
		class="org.hxwl.tm.struts.action.AdminAction">
		<property name="admindao">
			<ref bean="iAdminDAOImpl" />
		</property>
	</bean>
	
<!-- showAd -->
	<bean name="/showAd" class="org.hxwl.tm.struts.action.ShowAdAction">
		<property name="adDAOImpl">
			<ref bean="iAdDAOImpl"/>
		</property>
	</bean>
	
<!-- ad -->
	<bean name="/jsp/admin/ad" class="org.hxwl.tm.struts.action.AdAction">
		<property name="adDAOImpl">
			<ref bean="iAdDAOImpl"/>
		</property>
	</bean>
	<bean name="/jsp/admin/listAd_manage" class="org.hxwl.tm.struts.action.ListAd_manageAction">
		<property name="adDAOImpl">
			<ref bean="iAdDAOImpl"/>
		</property>
	</bean>
	<bean name="/jsp/admin/updateAd" class="org.hxwl.tm.struts.action.UpdateAdAction">
		<property name="adDAOImpl">
			<ref bean="iAdDAOImpl"/>
		</property>
	</bean>
	
<!-- teacher -->
	<bean name="/jsp/admin/teacher" class="org.hxwl.tm.struts.action.TeacherAction">
		<property name="teacherDAOImpl">
			<ref bean="iTeacherDAOImpl"/>
		</property>
	</bean>
		
</beans>

<p>

lxbhxwl
2008-05-16 16:09

问题是:我在前台向数据库添加数据是,关于AD表的save(Ad ad)的方法是正确的,而关于TEACHER表的save(Teacher teacher)的方法时却报如下错误:

java.lang.NullPointerException
	at org.hxwl.tm.DAOImpl.TeacherDAOImpl.save(TeacherDAOImpl.java:42)

2Go 1 2 下一页