您的位置:首页 > 其它

10.7 hibernate 空值异常,session管理,hql批量处理

2009-10-07 23:02 453 查看
hibernate 空值异常

字段 ---》属性(基本数据类型)(包装类型,字符,日期)
基本数据类型(int,double,char等)
优:性能好
缺点:不能接受空字段的值
包装类:如Double,String,Date,Integer(就是java.lang.*这种包装类)
优:能包装空值在内的所有有效数值
缺点:资源开销大

提示:如果数据封装不上pojo类,有可能就是数据类型选择的错误!
----------------------------------------------------------------------------
这样的错误:
<c:forEach var="dt" items="${requestScope.list}">
<tr>
<td>${dt.empno}</td>
<td>${dt.ename}</td>
<td>${dt.job}</td>
<td>${dt.sal}</td>
<td>${dt.dept.deptno}</td>//可以显示的,它在emp表中作为外键<many-to-one name="dept".. ><column

name="DEPTNO"/></many-to-one>
<td>${dt.dept.dname}</td>//这个不可以显示,它跟emp无任何关联。是dept独有的。会报session已经关闭。以下

有解决办法。
</tr>
</c:forEach>

session是线程不安全,不能被多个线程同时使用
web-线程->请求处理 一个请求可以对应一个session线程。

利用java.lang.ThreadLocal 会为每一个当前线程分配一个独立的存储空间
Map<key,value>会为这一个线程保存独有的线程信息
以下为代码:
--------------------------------------------------------
package cn.com.csuinfosoft.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static SessionFactory sessionFactory = null;

private static ThreadLocal threadLocal = new ThreadLocal();

static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static Session getSession() {
Session session = (Session)threadLocal.get();//获取当前线程对应的内存空间中,存储的对象
if(session == null) {
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}

public static void closeSession() {
Session session = (Session)threadLocal.get();
if(session != null) session.close();
threadLocal.set(null);
}

}

//在实现类中通过 Session session=HibernateUtil.getSession()这样得到的session是在同一个请求中保持不变的


然后当我们需要在请求结束关闭这样个一session可以写filter文件:
-------------------------
package cn.com.csuinfosoft.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import cn.com.csuinfosoft.hibernate.HibernateUtil;

public class HibernateFilter implements Filter {

public void destroy() {

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
try {
System.out.println("============ Filter try ====== Thread:" +

Thread.currentThread().getName());
filterChain.doFilter(request, response);
} finally {
System.out.println("============ Filter finally ======Thread:" +

Thread.currentThread().getName());
HibernateUtil.closeSession(); //每次请求结束前关闭Session
}

}

public void init(FilterConfig filterConfig) throws ServletException {

}

}


最后将这样一个filter配置在web.xml中,是相当容易的。
----------------------------

补充:SessionFactory可以在监听器控制下, 是要在程序卸载的时候关闭掉。

在过去所学的线程中Thread 中获得当前线程方法为currentThread()

hibernate的配置优化
<property name="jdbc.batch_size">30</property> 这个用于管理数据库批量处理的,如批量删除
<property name="jdbc.fetch_size">50</property> 在jdbc抓取数据时采用分批抓取,一次可以抓取50条

做一个简单 批量删除 实现,不是很难哦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: