org.hibernate.impl.SessionImpl.get(SessionImpl)

代码

package com.me.common.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.me.common.dao.BaseDao;
import com.me.util.ToolsUtil;
import com.me.vo.PageVo;

public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
	private Class<T> clazz;
	
	/* 
	 * 添加对象
	 */
	@Override
	public boolean insert(T entity) {
		Session session = this.getSession();
		session.save(entity);
		return true;
	}

	/* 
	 * 更新对象
	 */
	@Override
	public boolean update(T entity) {
		Session session = this.getSession();
		session.update(entity);
		return true;
	}

	/* 
	 * 根据Id查找
	 */
	@SuppressWarnings("unchecked")
	@Override
	public T queryById(Serializable id) {
		Session session = this.getSession();
		T object = (T)session.get(clazz, id);
		return object;
	}

	/* 
	 *无条件查找
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryByPage(PageVo pageVo) {
		Session session = this.getSession();
		Query query = session.createQuery("from " + clazz.getName());
		List<T> objects = query.setFirstResult((pageVo.getPageNo()-1)*pageVo.getPageCount()).setMaxResults(pageVo.getPageSize()).list();
		return objects;
	}

	/**
	 * 条件交集查找
	 * @param condition
	 * @param pageVo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryByConditionIntersection (Map<String, Object> condition, PageVo pageVo) {
		if(condition.isEmpty())
			return queryByPage(pageVo);
		else{
			Session session = this.getSession();
			Query query = session.createQuery("from "+clazz.getName()+ ToolsUtil.convertConditionIntersection(condition));
			List<T> objects = (List<T>) query.setFirstResult((pageVo.getPageNo()-1)*pageVo.getPageCount()).setMaxResults(pageVo.getPageCount());
			return objects;
		}
	}
	
	/**
	 * 条件并集查找
	 * @param condition
	 * @param pageVo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryByConditionUnion(Map<String, Object> condition, PageVo pageVo) {
		if(condition.isEmpty())
			return queryByPage(pageVo);
		else{
			Session session = this.getSession();
			Query query = session.createQuery("from "+clazz.getName()+ ToolsUtil.convertConditionUnion(condition));
			List<T> objects = (List<T>) query.setFirstResult((pageVo.getPageNo()-1)*pageVo.getPageCount()).setMaxResults(pageVo.getPageCount());
			return objects;
		}
	}
}

所有的数据库操作类都将继承该BaseDao

错误:

java.lang.NullPointerException
	at org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
	at com.me.common.dao.impl.BaseDaoImpl.queryById(BaseDaoImpl.java:51)
	at com.me.service.impl.UserServiceImpl.queryUserById(UserServiceImpl.java:32)
	at com.me.controller.UserController.queryUser(UserController.java:25)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)

解决方案:

我所遇到的这个问题,是因为我不了解反射。

没有加:

        /**
	* 通过反射泛型获取Class类型,getGenericSuperclass()方法获取对象的泛型的父类类型信息,
	 * getActualTypeArguments()[0]方法得到T的真实类型
	 * 
	 */
	@SuppressWarnings("unchecked")
	public BaseDaoImpl() {
		ParameterizedType pt = (ParameterizedType) this.getClass()
				.getGenericSuperclass();
		this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
	}

现在正在认真理解中......

你可能感兴趣的:(org.hibernate.impl.SessionImpl.get(SessionImpl))