SSH獲取SESSION時報NullPointException

lxbhxwl發表於2008-05-16
我的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 class="indent">

相關文章