﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-鹰翔宇空-文章分类-hibernate</title><link>http://www.blogjava.net/TrampEagle/category/9797.html</link><description>学习和生活
</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 12:31:03 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 12:31:03 GMT</pubDate><ttl>60</ttl><item><title>一个HibernateUitl的类</title><link>http://www.blogjava.net/TrampEagle/articles/39877.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Fri, 07 Apr 2006 09:11:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/39877.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/39877.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/39877.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/39877.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/39877.html</trackback:ping><description><![CDATA[
		<span class="postbody">摘自：<a href="http://forum.javaeye.com/viewtopic.php?t=1751">http://forum.javaeye.com/viewtopic.php?t=1751</a><br /><br />我们之前写了一个HibernateUitl的类，专门用于Hibernate Session的维护，它的代码如下： <br /><br />package com.huangdong.demo.util; <br /><br />import net.sf.hibernate.HibernateException; <br />import net.sf.hibernate.Session; <br />import net.sf.hibernate.SessionFactory; <br />import net.sf.hibernate.cfg.Configuration; <br /><br />public class HibernateUtil { <br /><br />private static final SessionFactory sessionFactory; <br /><br />static { <br />try { <br />sessionFactory = <br />new Configuration().configure().buildSessionFactory(); <br />} catch (HibernateException ex) { <br />throw new RuntimeException( <br />"Exception building SessionFactory: " + ex.getMessage(), <br />ex); <br />} <br />} <br /><br />public static final ThreadLocal session = new ThreadLocal(); <br /><br />public static Session currentSession() throws HibernateException { <br />Session s = (Session) session.get(); <br />// Open a new Session, if this Thread has none yet <br />if (s == null) { <br />s = sessionFactory.openSession(); <br />session.set(s); <br />} <br />return s; <br />} <br /><br />public static void closeSession() throws HibernateException { <br />Session s = (Session) session.get(); <br />session.set(null); <br />if (s != null) <br />s.close(); <br />} <br />} <br />下面我们对这段代码中我们需要关注的内容进行细致的说明。首先，这个类的目标有两个： <br /><br />单一实例：在系统中全局使用一个唯一的SessionFactory实 例。主要的原因一是Factory只需要一个实例可以调用方法就可以；另一方面取得SessionFActory需要的时间太久，每次都实例化，会过分浪费系统CPU资源。 <br />每个线和使用自身对应的数据库连接session：这里是为每个线程建立了一个局部的变量来达到这个目的。 <br />需要Plugin所做的事 <br />单一实例的实线是依靠下面的代码： <br /><br />private static final SessionFactory sessionFactory; <br /><br />static { <br />try { <br />sessionFactory = <br />new Configuration().configure().buildSessionFactory(); <br />} catch (HibernateException ex) { <br />throw new RuntimeException( <br />"Exception building SessionFactory: " + ex.getMessage(), <br />ex); <br />} <br />} <br />一个局部静态变量sessionFactory是整个application使用的唯一的一个实例，它在类第一次调入内存时通过 <br /><br />sessionFactory = new Configuration().configure().buildSessionFactory(); <br />将自己实例化。这个实例化的过程比较漫长。很显然，这个操作与通常我们使用Servlet时要在Servlet调入内存时初始化的init()方法所能做到的事很相似。在Struts中提供了Plugin这么一个机制来扩充Struts的基础功能，其实Plugin的实现也是基于Servlet的init方法和destory方法的。 <br /><br />总结起来，就是在Web应用这个特殊的环境中，由其是Struts中（因为它使用的Servlet只是一个，或说只是一个类及该类的子类）我们完全可以利用Servlet的init/destory机制（也就是Plugin机制）来完成在Web应用启动时的SessionFactory初始化和Web应用停止时SessionFactory的清除工作。 <br /><br />但是同时也会发现，如果在Plugin中对SessionFactory进行实例化后，无法将该实例传输给使用者，但是Web应用环境中也给我们可以使用以下解决办法： <br /><br />JNDI <br />Plugin的静态方法/变量 <br />ServletContex t的Attribute <br />下面我们以使用JNDI为主说明这些SessionFactory使用的模式。 <br /><br />Plugin To JNDI <br />通过JNDI完成SessionFactory的初始化的思路基本上是这样的： <br /><br />在一个Plugin的init方法中初始化SessionFactory的实例 <br />初始化完成后将SessionFactory的实例bind到JNDI目录树的一个节点上 <br />返回init方法 <br />在所有要使用SessionFactory的地方通过JNDI lookup出sessionFactory的实例得到具体的session进行数据库操作 <br />在Plugin的destory方法是unbind节点，并将SessionFactory的实例清除 <br />以下是具体的代码片段，首先我们看看Plugin中的相关代码： <br /><br />/* <br />* 创建日期 2003-12-26 <br />*/ <br />package com.huangdong.demo.plugin; <br /><br />import javax.naming.Context; <br />import javax.naming.InitialContext; <br />import javax.naming.NamingException; <br />import javax.servlet.ServletException; <br /><br />import net.sf.hibernate.HibernateException; <br />import net.sf.hibernate.SessionFactory; <br />import net.sf.hibernate.cfg.Configuration; <br /><br />import org.apache.struts.action.ActionServlet; <br />import org.apache.struts.action.PlugIn; <br />import org.apache.struts.config.ModuleConfig; <br /><br />/** <br />* @author HD <br />*/ <br />public class InitHibernate implements PlugIn { <br /><br />private Context ctx; <br />private SessionFactory sessionFactory; <br />/* <br />* 插件销毁方法 <br />*/ <br />public void destroy() { <br />if (ctx != null) { <br />try { <br />// unbind JNDI 节点 <br />ctx.unbind("HibernateSessionFactory"); <br />} catch (NamingException e) { <br />e.printStackTrace(); <br />} <br />} <br />if (sessionFactory != null) { <br />try { <br />// 关闭sessionFactory <br />sessionFactory.close(); <br />} catch (HibernateException e) { <br />e.printStackTrace(); <br />} <br />sessionFactory = null; <br />} <br />} <br /><br />/* <br />* 插件初始化方法 <br />*/ <br />public void init(ActionServlet servlet, ModuleConfig config) <br />throws ServletException { <br />try { <br />// 获取SessionFactory的实例 <br />sessionFactory = <br />new Configuration().configure().buildSessionFactory(); <br />} catch (HibernateException ex) { <br />throw new RuntimeException( <br />"Exception building SessionFactory: " + ex.getMessage(), <br />ex); <br />} <br /><br />try { <br />// 取得容器上下文 <br />ctx = new InitialContext(); <br />// 将sessionFactory bind到JND树中 <br />ctx.bind("HibernateSessionFactory", sessionFactory); <br />} catch (NamingException ex) { <br />throw new RuntimeException( <br />"Exception binding SessionFactory to JNDI: " + ex.getMessage(), <br />ex); <br />} <br />} <br /><br />} <br />接下来我们改造一下原来的HibernateUitl类，我们新建一个HibernateUtilPlus类： <br /><br />/* <br />* 创建日期 2003-12-28 <br />* <br />*/ <br />package com.huangdong.demo.util; <br /><br />import javax.naming.Context; <br />import javax.naming.InitialContext; <br />import javax.naming.NamingException; <br /><br />import net.sf.hibernate.HibernateException; <br />import net.sf.hibernate.Session; <br />import net.sf.hibernate.SessionFactory; <br /><br />/** <br />* @author HD <br />*/ <br />public class HibernateUtilPlus { <br /><br />private static SessionFactory sessionFactory = null; <br />public static final ThreadLocal session = new ThreadLocal(); <br /><br />public static Session currentSession() throws HibernateException { <br />if (sessionFactory == null) { <br />// 如果sessionFactory实例为null则从JNDI中获取 <br />if (getSystemSessionFactory() == false) { <br />throw new HibernateException("Exception geting SessionFactory from JNDI "); <br />} <br />} <br />Session s = (Session) session.get(); <br />// Open a new Session, if this Thread has none yet <br />if (s == null) { <br />s = sessionFactory.openSession(); <br />session.set(s); <br />} <br />return s; <br />} <br /><br />public static void closeSession() throws HibernateException { <br />Session s = (Session) session.get(); <br />session.set(null); <br />if (s != null) <br />s.close(); <br />} <br /><br />private static boolean getSystemSessionFactory() { <br />try { <br />//从JNDI中取得SessionFactory的实例，如果出错返回false <br />Context inttex = new InitialContext(); <br />sessionFactory = <br />(SessionFactory) inttex.lookup("HibernateSessionFactory"); <br />} catch (NamingException e) { <br />return false; <br />} <br />return true; <br />} <br />} <br /><br />这里有一个getSystemSessionFactory方法专门从JNDI中获取SessionFactory的实例。 <br /><br />我们还是使用之前的TestServlet来测试，改过之后的TestServlet类如下： <br /><br />/* <br />* 创建日期 2003-12-26 <br />* <br />*/ <br />package com.huangdong.demo.bean; <br /><br />import java.util.Calendar; <br /><br />import net.sf.hibernate.HibernateException; <br />import net.sf.hibernate.Session; <br />import net.sf.hibernate.Transaction; <br /><br />import com.huangdong.demo.dao.SysUser; <br />import com.huangdong.demo.util.HibernateUtilPlus; <br /><br />/** <br />* @author HD <br />*/ <br />public class TestHibernate { <br />public TestHibernate() { <br /><br />} <br /><br />public boolean TestAdd() { <br />try { <br />Session session = HibernateUtilPlus.currentSession(); <br />Transaction tx = session.beginTransaction(); <br />SysUser user = new SysUser(); <br />user.setUsername("丫丫2"); <br />user.setUserpasword("uhkuhkqepdwqi"); <br />user.setLastlogin(Calendar.getInstance()); <br />session.save(user); <br />tx.commit(); <br />HibernateUtilPlus.closeSession(); <br />} catch (HibernateException e) { <br />e.printStackTrace(); <br />return false; <br />} <br />return true; <br />} <br />} <br />这里使用HibernateUtilPlus类来获取session。 <br /><br />最后我们需要将写好的Plugin配置到Struts中去，以让应用服务器启动时识别到这个Plugin的存在以初始化相关的内容。在WEB-INF文件夹下有一个名为struts-config.xml的配置文件，在其中加入Plugin的配置： <br /><br />&lt;?xml version="1.0" encoding="UTF-8"?&gt; <br />&lt;!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"&gt; <br />&lt;struts-config&gt; <br />&lt;data-sources /&gt; <br />&lt;form-beans &gt; <br />&lt;/form-beans&gt; <br /><br />&lt;global-exceptions /&gt; <br />&lt;global-forwards /&gt; <br />&lt;action-mappings &gt; <br />&lt;/action-mappings&gt; <br /><br />&lt;controller /&gt; <br />&lt;message-resources parameter="com.huangdong.demo.ApplicationResources" /&gt; <br />&lt;!--加入Plugin的配置，使用plug-in元素进行说明--&gt; <br />&lt;plug-in className="com.huangdong.demo.plugin.InitHibernate" /&gt; <br />&lt;/struts-config&gt; <br />再次运行Tomcat，进行测试。可以在 这里下载 通过JNDI Plugin的Eclipse示例。 <br /><br />另两种方法的探索 <br />在文章的最开始，我们提到了三种方法，除了上面仔细提到的还有另外两种： <br /><br />Plugin的静态方法/变量 <br />ServletContext的Attribute <br />这里我们简单说明这两种方法实现的原理，就不实际的完成具体代码了。具体的代码与测试还请读者自己完成。 <br /><br />Plugin的静态方法和变量 <br />使用这种方法与使用原有的HibernateUtil类的原理类似，但是可以将SessionFaction的实例化放在init方法中。如下： <br /><br />/* <br />* 创建日期 2003-12-26 <br />*/ <br />package com.huangdong.demo.plugin; <br /><br />import javax.servlet.ServletException; <br /><br />import net.sf.hibernate.HibernateException; <br />import net.sf.hibernate.SessionFactory; <br />import net.sf.hibernate.cfg.Configuration; <br /><br />import org.apache.struts.action.ActionServlet; <br />import org.apache.struts.action.PlugIn; <br />import org.apache.struts.config.ModuleConfig; <br /><br />/** <br />* @author HD <br />*/ <br />public class InitHibernate implements PlugIn { <br /><br />public static final SessionFactory sessionFactory; <br />/* <br />* 插件销毁方法 <br />*/ <br />public void destroy() { <br />sessionFactory = null; <br />} <br />} <br /><br />/* <br />* 插件初始化方法 <br />*/ <br />public void init(ActionServlet servlet, ModuleConfig config) <br />throws ServletException { <br />try { <br />// 获取SessionFactory的实例 <br />sessionFactory = <br />new Configuration().configure().buildSessionFactory(); <br />} catch (HibernateException ex) { <br />throw new RuntimeException( <br />"Exception building SessionFactory: " + ex.getMessage(), <br />ex); <br />} <br />} <br /><br />} <br />这段plugin代码很简单，在使用时也就直接使用这个Plugin的public变量来取得sessionFactroy实例了。 <br /><br />ServletContext的Attribute <br />这个方法的原理基于对Servlet的使用，在Struts 中的使用会麻烦。它的原理是这样的： <br /><br />首先在plugin中初始化好sessionFactory实例，使用这两句话将其放入servlet的context中： <br /><br />ServletContext context = servlet.getServletContext(); <br />context.setAttribute("SESSIONFACTORY", sessionFactory); <br /><br />使用的方法很简单了，就是扩展一个自定义的ActionServlet，在提交具体的Action前，将SessionFactory提取出来，放入调用Action的execute方法的参数request的属性中。代码如下： <br /><br />ServletContext context = servlet.getServletContext(); <br />context.getAttribute("SESSIONFACTORY", sessionFactory); <br /><br />request.setAttribute("SESSIONFACTORY", sessionFactory); <br />这样在具体的execute方法里可以通过： <br /><br />request.getAttribute("SESSIONFACTORY", sessionFactory); <br />来取到正确的SessionFactory。 <br /><br />这里的实现方法都是原理性的，具体还需要大家仔细了解Struts的实现方法来总结出自己最为习惯的使用策略。 <br /><br />留在最后话 <br />本文中的所有代码在以下环境中由作者实际测试完全没有问题： <br /><br />Eclipse 2.1.2 <br />Struts 1.1 <br />Hibernate 2.1.1 <br />Tomcat 4.1.29/Jetty 4.2.15/Orion 2.0.2 <br />com.tanghan.plugin_0.1.0.12.21 <br />JDK 1.4.2_02 For Windows/FreeBSD 4.8/FreeBSD 4.9 <br />FreeBSD 4.8/FreeBSD 4.9/Windows 2000/Windows XP <br />Oracle 9.2.0.1.0 <br />如果你对本文有什么意见和建议请 联系我 ，告诉我你的想法，另外也可以到 技术天空BBS的Java版 中讨论与Java相关的各种技术。</span>
<img src ="http://www.blogjava.net/TrampEagle/aggbug/39877.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-04-07 17:11 <a href="http://www.blogjava.net/TrampEagle/articles/39877.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate Annotations 实战</title><link>http://www.blogjava.net/TrampEagle/articles/31661.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Mon, 20 Feb 2006 08:07:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/31661.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/31661.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/31661.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/31661.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/31661.html</trackback:ping><description><![CDATA[原文引自：<A href="http://www.matrix.org.cn/resource/article/44/44141_Hibernate_Annotations.html">http://www.matrix.org.cn/resource/article/44/44141_Hibernate_Annotations.html</A><BR><BR>
<P><FONT size=4><STRONG>从 hbm.xml 到 Annotations</STRONG></FONT></P>
<P><STRONG><FONT size=4></FONT></STRONG>任何获得Matrix授权的网站，转载请保留以下作者信息和链接：<BR>作者：<FONT color=#0000ff>icess</FONT>(作者的blog:<A href="http://blog.matrix.org.cn/page/icess"><FONT color=#002c99>http://blog.matrix.org.cn/page/icess</FONT></A>)<BR>原文：<A href="http://www.matrix.org.cn/resource/article/44/44141_Hibernate_Annotations.html"><FONT color=#002c99>http://www.matrix.org.cn/resource/article/44/44141_Hibernate_Annotations.html</FONT></A><BR>&nbsp;　<BR>下面让我们先看一个通常用 hbm.xml 映射文件的例子. 有3个类 .HibernateUtil.java 也就是 Hibernate文档中推荐的工具类,Person.java 一个志没睦? Test.java 测试用的类.都在test.hibernate 包中. 每个类的代码如下:</P>
<P><FONT style="BACKGROUND-COLOR: #ffffff"><STRONG>HibernateUtil:</STRONG></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">01 package test.hibernate;<BR>02 <BR>03 import org.hibernate.HibernateException;<BR>04 import org.hibernate.Session;<BR>05 import org.hibernate.SessionFactory;<BR>06 import org.hibernate.cfg.Configuration;<BR>07 <BR>08 public class HibernateUtil {<BR>09&nbsp;&nbsp; public static final SessionFactory sessionFactory;<BR>10&nbsp;&nbsp; <BR>11&nbsp;&nbsp; static {<BR>12&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sessionFactory = new Configuration()<BR>14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .configure()<BR>15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .buildSessionFactory();<BR>16&nbsp;&nbsp;&nbsp;&nbsp; } catch (HibernateException e) {<BR>17&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated catch block<BR>18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new ExceptionInInitializerError(e);<BR>21&nbsp;&nbsp;&nbsp;&nbsp; }<BR>22&nbsp;&nbsp; }<BR>23&nbsp;&nbsp; <BR>24&nbsp;&nbsp; public static final ThreadLocal&lt;Session&gt; session = new ThreadLocal&lt;Session&gt;();<BR>25&nbsp;&nbsp; <BR>26&nbsp;&nbsp; public static Session currentSession() throws HibernateException {<BR>27&nbsp;&nbsp;&nbsp;&nbsp; Session s = session.get();<BR>28&nbsp;&nbsp;&nbsp;&nbsp; <BR>29&nbsp;&nbsp;&nbsp;&nbsp; if(s == null) {<BR>30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s = sessionFactory.openSession();<BR>31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.set(s);<BR>32&nbsp;&nbsp;&nbsp;&nbsp; }<BR>33&nbsp;&nbsp;&nbsp;&nbsp; <BR>34&nbsp;&nbsp;&nbsp;&nbsp; return s;<BR>35&nbsp;&nbsp; }<BR>36&nbsp;&nbsp; <BR>37&nbsp;&nbsp; public static void closeSession() throws HibernateException {<BR>38&nbsp;&nbsp;&nbsp;&nbsp; Session s = session.get();<BR>39&nbsp;&nbsp;&nbsp;&nbsp; if(s != null) {<BR>40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.close();<BR>41&nbsp;&nbsp;&nbsp;&nbsp; }<BR>42&nbsp;&nbsp;&nbsp;&nbsp; session.set(null);<BR>43&nbsp;&nbsp; }<BR>44 }</FONT></P>
<P><STRONG>Person:</STRONG></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">01 package test.hibernate;<BR>02 <BR>03 import java.util.LinkedList;<BR>04 import java.util.List;<BR>05 <BR>06 /**<BR>07&nbsp; * <BR>08&nbsp; */<BR>09 <BR>10 @SuppressWarnings("serial")<BR>11 public class Person implements java.io.Serializable {<BR>12 <BR>13&nbsp;&nbsp; // Fields<BR>14 <BR>15&nbsp;&nbsp; private Integer id;<BR>16 <BR>17&nbsp;&nbsp; private String name;<BR>18 <BR>19&nbsp;&nbsp; private String sex;<BR>20 <BR>21&nbsp;&nbsp; private Integer age;<BR>22 <BR>23&nbsp;&nbsp; private List list = new LinkedList();<BR>24 <BR>25&nbsp;&nbsp; // Collection accessors<BR>26 <BR>27&nbsp;&nbsp; public List getList() {<BR>28&nbsp;&nbsp;&nbsp;&nbsp; return list;<BR>29&nbsp;&nbsp; }<BR>30 <BR>31&nbsp;&nbsp; public void setList(List list) {<BR>32&nbsp;&nbsp;&nbsp;&nbsp; this.list = list;<BR>33&nbsp;&nbsp; }<BR>34 <BR>35&nbsp;&nbsp; /** default constructor */<BR>36&nbsp;&nbsp; public Person() {<BR>37&nbsp;&nbsp; }<BR>38 <BR>39&nbsp;&nbsp; /** constructor with id */<BR>40&nbsp;&nbsp; public Person(Integer id) {<BR>41&nbsp;&nbsp;&nbsp;&nbsp; this.id = id;<BR>42&nbsp;&nbsp; }<BR>43 <BR>44&nbsp;&nbsp; // Property accessors<BR>45 <BR>46&nbsp;&nbsp; public Integer getId() {<BR>47&nbsp;&nbsp;&nbsp;&nbsp; return this.id;<BR>48&nbsp;&nbsp; }<BR>49 <BR>50&nbsp;&nbsp; public void setId(Integer id) {<BR>51&nbsp;&nbsp;&nbsp;&nbsp; this.id = id;<BR>52&nbsp;&nbsp; }<BR>53 <BR>54&nbsp;&nbsp; public String getName() {<BR>55&nbsp;&nbsp;&nbsp;&nbsp; return this.name;<BR>56&nbsp;&nbsp; }<BR>57 <BR>58&nbsp;&nbsp; public void setName(String name) {<BR>59&nbsp;&nbsp;&nbsp;&nbsp; this.name = name;<BR>60&nbsp;&nbsp; }<BR>61 <BR>62&nbsp;&nbsp; public String getSex() {<BR>63&nbsp;&nbsp;&nbsp;&nbsp; return this.sex;<BR>64&nbsp;&nbsp; }<BR>65 <BR>66&nbsp;&nbsp; public void setSex(String sex) {<BR>67&nbsp;&nbsp;&nbsp;&nbsp; this.sex = sex;<BR>68&nbsp;&nbsp; }<BR>69 <BR>70&nbsp;&nbsp; public Integer getAge() {<BR>71&nbsp;&nbsp;&nbsp;&nbsp; return this.age;<BR>72&nbsp;&nbsp; }<BR>73 <BR>74&nbsp;&nbsp; public void setAge(Integer age) {<BR>75&nbsp;&nbsp;&nbsp;&nbsp; this.age = age;<BR>76&nbsp;&nbsp; }<BR>77 <BR>78 }</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff"><STRONG>Test:</STRONG></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">01 /*<BR>02&nbsp; * Created on <BR>03&nbsp; * @author <BR>04&nbsp; */<BR>05 package test.hibernate;<BR>06 <BR>07 import java.sql.SQLException;<BR>08 <BR>09 import org.hibernate.FlushMode;<BR>10 import org.hibernate.HibernateException;<BR>11 import org.hibernate.Session;<BR>12 import org.hibernate.Transaction;<BR>13 <BR>14 public class Test {<BR>15&nbsp;&nbsp; <BR>16&nbsp;&nbsp; public static void main(String [] args) {<BR>17&nbsp;&nbsp;&nbsp;&nbsp; Session s = HibernateUtil.currentSession();<BR>18&nbsp;&nbsp;&nbsp;&nbsp; <BR>19&nbsp;&nbsp;&nbsp;&nbsp; Transaction tx = s.beginTransaction();&nbsp;&nbsp;&nbsp; <BR>20&nbsp;&nbsp;&nbsp;&nbsp; <BR>21 //&nbsp;&nbsp;&nbsp; Person p = (Person) s.load(Person.class, 1);<BR>22 //&nbsp;&nbsp;&nbsp; System.out.println(p.getName());<BR>23&nbsp;&nbsp;&nbsp;&nbsp; Person p = new Person();<BR>24&nbsp;&nbsp;&nbsp;&nbsp; <BR>25&nbsp;&nbsp;&nbsp;&nbsp; p.setAge(19);<BR>26&nbsp;&nbsp;&nbsp;&nbsp; p.setName("icerain");<BR>27&nbsp;&nbsp;&nbsp;&nbsp; p.setSex("male");<BR>28&nbsp;&nbsp;&nbsp;&nbsp; s.save(p);<BR>29&nbsp;&nbsp;&nbsp;&nbsp; s.flush();<BR>30&nbsp;&nbsp;&nbsp;&nbsp; /*<BR>31&nbsp;&nbsp;&nbsp;&nbsp; Person p2 = (Person) s.get(Person.class, new Integer(1));<BR>32&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(p2.getName());<BR>33&nbsp;&nbsp;&nbsp;&nbsp; p2.setName("ice..");<BR>34&nbsp;&nbsp;&nbsp;&nbsp; s.saveOrUpdate(p2);<BR>35&nbsp;&nbsp;&nbsp;&nbsp; s.flush();<BR>36&nbsp;&nbsp;&nbsp;&nbsp; Person p3 = (Person) s.get(Person.class, new Integer(2));<BR>37&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(p3.getName());<BR>38&nbsp;&nbsp;&nbsp;&nbsp; s.delete(p3);<BR>39&nbsp;&nbsp;&nbsp;&nbsp; */<BR>40&nbsp;&nbsp;&nbsp;&nbsp; <BR>41&nbsp;&nbsp;&nbsp;&nbsp; tx.commit();&nbsp; <BR>42&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>43&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(p.getName());<BR>44&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated catch block<BR>46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>47&nbsp;&nbsp;&nbsp;&nbsp; }<BR>48&nbsp;&nbsp;&nbsp;&nbsp; <BR>49&nbsp;&nbsp;&nbsp;&nbsp; HibernateUtil.closeSession();<BR>50&nbsp;&nbsp; }<BR>51 }</FONT></P>
<P><STRONG>hibernate.cfg.xml</STRONG> 配置文件如下,利用mysql 数据库.</P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;hibernate-configuration&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;session-factory&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="hibernate.connection.driver_class"&gt;org.gjt.mm.mysql.Driver&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="hibernate.connection.password"&gt;你的数据库密码&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost/数据库名&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="hibernate.connection.username"&gt;用户名&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="show_sql"&gt;true&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="hibernate.transaction.factory_class"&gt;org.hibernate.transaction.JDBCTransactionFactory&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="hibernate.transaction.auto_close_session"&gt;false&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;mapping resource="test/hibernate/annotation/Person.hbm.xml"/&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/session-factory&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/hibernate-configuration&gt;</FONT></P>
<P>其中 配置了&lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;属性 自动导入数据库ddl.生产的ddl sql语句如下</P>
<P><FONT style="BACKGROUND-COLOR: #ffcc99">create table person (id integer not null auto_increment, name varchar(255), sex varchar(255), age integer, person integer, primary key (id))</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffcc99">alter table person add index FKC4E39B5511C4A5C2 (person), add constraint FKC4E39B5511C4A5C2 foreign key (person) references person (id)</FONT></P>
<P>而<STRONG>Person.hbm.xml</STRONG> 文件如下:</P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;?xml version="1.0"?&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;hibernate-mapping&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;class name="test.hibernate.Person" table="person"&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;id name="id" type="integer"&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;column name="id" /&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;generator class="native"&gt;&lt;/generator&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/id&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="name" type="string"&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;column name="name" /&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="sex" type="string"&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;column name="sex" /&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;property name="age" type="integer"&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;column name="age" /&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/property&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;bag name="list" cascade="all"&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;key column="person"&gt;&lt;/key&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;one-to-many class="test.hibernate.Person"/&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/bag&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/class&gt;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">&lt;/hibernate-mapping&gt;</FONT></P>
<P>下面让我们看看利用 Hibernate Annotations 如何做,只要三个类 不再需要 hbm.xml配置文件:</P>
<P>还要把用到的两个jar文件 放入的类路径中. 具体如何做,请参考&nbsp; Hibernate Annotations 中文文档</P>
<P><A href="http://hibernate.6644.net/"><FONT color=#002c99>http://hibernate.6644.net</FONT></A></P>
<P>HibernateUtil.java 也就是 Hibernate文档中推荐的工具类,Person.java 一个持久化的类, Test.java 测试用的类.都在test.hibernate.annotation 包中. 每个类的代码如下:</P>
<P><STRONG>HibernateUtil</STRONG></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">01 package test.hibernate.annotation;<BR>02 <BR>03 import org.hibernate.HibernateException;<BR>04 import org.hibernate.Session;<BR>05 import org.hibernate.SessionFactory;<BR>06 import org.hibernate.cfg.AnnotationConfiguration;<BR>07 import org.hibernate.cfg.Configuration;<BR>08 <BR>09 public class HibernateUtil {<BR>10&nbsp;&nbsp; public static final SessionFactory sessionFactory;<BR>11&nbsp;&nbsp; <BR>12&nbsp;&nbsp; static {<BR>13&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sessionFactory = new AnnotationConfiguration()&nbsp;&nbsp; //注意: 建立 SessionFactory于前面的不同<BR>15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .addPackage("test.hibernate.annotation")<BR>16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .addAnnotatedClass(Person.class)<BR>17&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .configure()<BR>19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .buildSessionFactory();<BR>20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //new Configuration().configure().buildSessionFactory();<BR>21&nbsp;&nbsp;&nbsp;&nbsp; } catch (HibernateException e) {<BR>22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated catch block<BR>23&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new ExceptionInInitializerError(e);<BR>26&nbsp;&nbsp;&nbsp;&nbsp; }<BR>27&nbsp;&nbsp; }<BR>28&nbsp;&nbsp; <BR>29&nbsp;&nbsp; public static final ThreadLocal&lt;Session&gt; session = new ThreadLocal&lt;Session&gt;();<BR>30&nbsp;&nbsp; <BR>31&nbsp;&nbsp; public static Session currentSession() throws HibernateException {<BR>32&nbsp;&nbsp;&nbsp;&nbsp; Session s = session.get();<BR>33&nbsp;&nbsp;&nbsp;&nbsp; <BR>34&nbsp;&nbsp;&nbsp;&nbsp; if(s == null) {<BR>35&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s = sessionFactory.openSession();<BR>36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.set(s);<BR>37&nbsp;&nbsp;&nbsp;&nbsp; }<BR>38&nbsp;&nbsp;&nbsp;&nbsp; <BR>39&nbsp;&nbsp;&nbsp;&nbsp; return s;<BR>40&nbsp;&nbsp; }<BR>41&nbsp;&nbsp; <BR>42&nbsp;&nbsp; public static void closeSession() throws HibernateException {<BR>43&nbsp;&nbsp;&nbsp;&nbsp; Session s = session.get();<BR>44&nbsp;&nbsp;&nbsp;&nbsp; if(s != null) {<BR>45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.close();<BR>46&nbsp;&nbsp;&nbsp;&nbsp; }<BR>47&nbsp;&nbsp;&nbsp;&nbsp; session.set(null);<BR>48&nbsp;&nbsp; }<BR>49 }</FONT></P>
<P><STRONG>Person:</STRONG></P>
<P><FONT style="BACKGROUND-COLOR: #ffff99">01 package test.hibernate.annotation;<BR>02 <BR>03 import java.util.LinkedList;<BR>04 import java.util.List;<BR>05 <BR>06 import javax.persistence.AccessType;<BR>07 import javax.persistence.Basic;<BR>08 import javax.persistence.Entity;<BR>09 import javax.persistence.GeneratorType;<BR>10 import javax.persistence.Id;<BR>11 import javax.persistence.OneToMany;<BR>12 import javax.persistence.Table;<BR>13 import javax.persistence.Transient;<BR>14 <BR>15 /**<BR>16&nbsp; * <BR>17&nbsp; */<BR>18 <BR>19 @SuppressWarnings("serial")<BR>20 @Entity(access = AccessType.PROPERTY) //定义该类为实体类<BR>21 @Table&nbsp;&nbsp; //映射表<BR>22 public class Person implements java.io.Serializable {<BR>23 <BR>24&nbsp;&nbsp; // Fields<BR>25 <BR>26&nbsp;&nbsp; private Integer id;<BR>27 <BR>28&nbsp;&nbsp; private String name;<BR>29 <BR>30&nbsp;&nbsp; private String sex;<BR>31 <BR>32&nbsp;&nbsp; private Integer age;<BR>33 <BR>34&nbsp;&nbsp; private List list = new LinkedList();<BR>35 <BR>36&nbsp;&nbsp; // Constructors<BR>37&nbsp;&nbsp; /** default constructor */<BR>38&nbsp;&nbsp; public Person() {<BR>39&nbsp;&nbsp; }<BR>40 <BR>41&nbsp;&nbsp; /** constructor with id */<BR>42&nbsp;&nbsp; public Person(Integer id) {<BR>43&nbsp;&nbsp;&nbsp;&nbsp; this.id = id;<BR>44&nbsp;&nbsp; }<BR>45 <BR>46&nbsp;&nbsp; // Property accessors<BR>47&nbsp;&nbsp; @Id<BR>48&nbsp;&nbsp; public Integer getId() {<BR>49&nbsp;&nbsp;&nbsp;&nbsp; return this.id;<BR>50&nbsp;&nbsp; }<BR>51 <BR>52&nbsp;&nbsp; public void setId(Integer id) {<BR>53&nbsp;&nbsp;&nbsp;&nbsp; this.id = id;<BR>54&nbsp;&nbsp; }<BR>55 <BR>56&nbsp;&nbsp; @Basic<BR>57&nbsp;&nbsp; public String getName() {<BR>58&nbsp;&nbsp;&nbsp;&nbsp; return this.name;<BR>59&nbsp;&nbsp; }<BR>60 <BR>61&nbsp;&nbsp; public void setName(String name) {<BR>62&nbsp;&nbsp;&nbsp;&nbsp; this.name = name;<BR>63&nbsp;&nbsp; }<BR>64 <BR>65&nbsp;&nbsp; @Basic<BR>66&nbsp;&nbsp; public String getSex() {<BR>67&nbsp;&nbsp;&nbsp;&nbsp; return this.sex;<BR>68&nbsp;&nbsp; }<BR>69 <BR>70&nbsp;&nbsp; public void setSex(String sex) {<BR>71&nbsp;&nbsp;&nbsp;&nbsp; this.sex = sex;<BR>72&nbsp;&nbsp; }<BR>73 <BR>74&nbsp;&nbsp; @Basic<BR>75&nbsp;&nbsp; public Integer getAge() {<BR>76&nbsp;&nbsp;&nbsp;&nbsp; return this.age;<BR>77&nbsp;&nbsp; }<BR>78 <BR>79&nbsp;&nbsp; public void setAge(Integer age) {<BR>80&nbsp;&nbsp;&nbsp;&nbsp; this.age = age;<BR>81&nbsp;&nbsp; }<BR>82&nbsp;&nbsp; @Transient&nbsp; //由于本例不打算演示集合映射 所有声明该属性为 Transient <BR>83&nbsp;&nbsp; public List getList() {<BR>84&nbsp;&nbsp;&nbsp;&nbsp; return list;<BR>85&nbsp;&nbsp; }<BR>86 <BR>87&nbsp;&nbsp; public void setList(List list) {<BR>88&nbsp;&nbsp;&nbsp;&nbsp; this.list = list;<BR>89&nbsp;&nbsp; }<BR>90 <BR>91 }</FONT></P>
<P>注意该实体类中的属性都使用了默认值.</P>
<P>Test.java 代码同上</P>
<P>不需要了 hbm.xml 映射文件, 是不是简单了一些 .给人认为简化了一些不是主要目的.主要是可以了解一下 EJB3 的持久化机制 ,提高一下开发效率才是重要的.</P>
<P>好了 .本例就完了 . 感觉怎么样了 .欢迎你来批批.</P>
<P>PS:</P>
<P>生成的数据库表 和 程序执行后的 数据库情况如下</P>
<P><FONT style="BACKGROUND-COLOR: #99ccff">mysql&gt; describe person;<BR>+--------+--------------+------+-----+---------+----------------+<BR>| Field&nbsp; | Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | Null | Key | Default |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Extra |<BR>+--------+--------------+------+-----+---------+----------------+<BR>| id&nbsp;&nbsp;&nbsp;&nbsp; | int(11)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | NO&nbsp;&nbsp; | PRI | NULL&nbsp;&nbsp;&nbsp; | auto_increment |<BR>| name&nbsp;&nbsp; | varchar(255) | YES&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; | NULL&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<BR>| sex&nbsp;&nbsp;&nbsp; | varchar(255) | YES&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; | NULL&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<BR>| age&nbsp;&nbsp;&nbsp; | int(11)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | YES&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; | NULL&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<BR>| person | int(11)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | YES&nbsp; | MUL | NULL&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<BR>+--------+--------------+------+-----+---------+----------------+<BR>5 rows in set (0.00 sec)</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #99ccff">mysql&gt; select * from person;<BR>+----+---------+------+------+--------+<BR>| id | name&nbsp;&nbsp;&nbsp; |&nbsp; sex |&nbsp; age | person |<BR>+----+---------+------+------+--------+<BR>|&nbsp; 1 | icerain | male |&nbsp;&nbsp; 19 |&nbsp;&nbsp; NULL |<BR>+----+---------+------+------+--------+<BR>1 row in set (0.03 sec)</FONT></P><img src ="http://www.blogjava.net/TrampEagle/aggbug/31661.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-20 16:07 <a href="http://www.blogjava.net/TrampEagle/articles/31661.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 3 Formulas（翻译）</title><link>http://www.blogjava.net/TrampEagle/articles/30584.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Tue, 14 Feb 2006 05:38:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/30584.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/30584.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/30584.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/30584.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/30584.html</trackback:ping><description><![CDATA[
		<p>原文引自：<a href="/rosen/archive/2005/09/19/13317.html">http://www.blogjava.net/rosen/archive/2005/09/19/13317.html</a><br /><br /> </p>
		<div class="postText">
				<p>    <a href="http://www.hibernate.org/">Hibernate</a> 和 <a href="http://www.springframework.org/">Spring</a> 这两个突出的开源框架被越来越多的应用到 J2EE 中。尽管目标有着不同的问题空间，它们却共享一个关键特性：<a href="http://martinfowler.com/articles/injection.html">依赖注入</a>。在对象返回到客户端之前 Spring 协助挑选出这些对象间依赖关系，减少客户端代码量。而 Hibernate 专门挑选出在完整的对象模型返回客户端之前由数据模型表现的依赖关系。当使用 JDBC 直接从数据模型映射到对象模型时，我们通常需要书写大量的代码来构建对象模型。Hibernate 的出现淘汰了这种繁琐的编码工作。<br />    <br />    Hibernate 2.x 提供基本的表到对象的映射，标准关联映射（包括 one-to-one, one-to-many 以及 many-to-many 关系），多态映射，等等。Hibernate 3.x 沿着路线继续前进，formula、filter、subselect 等，使映射更加灵活，提供用细粒度的解释特性。<br />    <br />    在本文中，将阐述 formula 到底有哪些特性可帮助我们进行模型转换。Hibernate 3.x 之前，formula 属性只能出现在 property 元素中。但是到了现在，你可以在许多元素中使用 Hibernate 3.x 提供的 formula 属性或元素（formula 用法方面都是一样的），包括 discriminator、element、many-to-many、map-key、map-key-many-to-many、以及 property。它增加了 OR 映射的灵活性，因此允许对复杂数据模型更加细粒的解释。<br />    <br />    下面有两个 formula 应用场景：<br />    <br />    1. formula 用于评估结果的场合。在 discriminator、element、map-key、map-key-many-to-many以及 property 元素中注入 formula。<br />    2. formula 用于连接目的的场合。在 many-to-one、one-to-one 以及 many-to-many 元素中注入 formula。<br />    <br /><font size="4"><strong>范畴 1：从 formula 获得评估结果</strong><br /></font>    <br />    <strong>Discriminator<br /></strong>    <br />    在真实的数据 schema 中，经常出现一个表被用于描述其他表的情况。formula 可协助提供灵活的多态 OR 映射。<br />    <br />    在图 1 的范例中，有两个表：Product 和 ProductRelease。每条 product 记录都有一个 ProductReleaseID 参考相应的产品出厂记录，包括 product release name、type、release date 等等。<br /><br />          <img height="338" alt="Product and Product Release Data Model" src="http://www.onjava.com/onjava/2005/08/03/graphics/image001.jpg" width="344" /><br />                       图 1. Product 和 product release<br />    <br />    ProductRelease 表中有个有趣的属性 SubProductAllowable，该属性的值为 1 或 0。值为 1 代表允许任何的次品出厂，但是 0 不允许次品出厂。<br />    <br />    图 2 展示了由数据模型解释的对象模型。Nested 接口定义了 getSubProducts 和 setSubProducts 方法。NestedProduct 类继承 Product 基类并实现 Nested 接口。无论产品数据记录是 Product 或 NestedProduct，都取决于产品出厂记录中 SubProductAllowable 的值。<br /><br />        <img height="333" alt="Product and Product Release Object Domain Model" src="http://www.onjava.com/onjava/2005/08/03/graphics/image002.jpg" width="375" /><br />                     图 2. Product 和产品出厂对象域模型<br />    <br />    为了完成模型转换，我们使用如下的 Hibernate 3.x 映射：<br />    <br /><font color="#000080"><hibernate-mapping></hibernate-mapping></font></p>
				<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #0000ff">&lt;</span>
						<span style="COLOR: #800000">hibernate-mapping</span>
						<span style="COLOR: #0000ff">&gt;</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span>
						<span style="COLOR: #0000ff">&lt;</span>
						<span style="COLOR: #800000">class </span>
						<span style="COLOR: #ff0000">name</span>
						<span style="COLOR: #0000ff">="Product"</span>
						<span style="COLOR: #ff0000"> <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        discriminator-value</span>
						<span style="COLOR: #0000ff">="0"</span>
						<span style="COLOR: #ff0000">  lazy</span>
						<span style="COLOR: #0000ff">="false"</span>
						<span style="COLOR: #0000ff">&gt;</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">&lt;</span>
						<span style="COLOR: #800000">id </span>
						<span style="COLOR: #ff0000">name</span>
						<span style="COLOR: #0000ff">="id"</span>
						<span style="COLOR: #ff0000"> type</span>
						<span style="COLOR: #0000ff">="long"</span>
						<span style="COLOR: #0000ff">/&gt;</span>
						<span style="COLOR: #000000">       <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">&lt;</span>
						<span style="COLOR: #800000">discriminator <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #ff0000">formula</span>
						<span style="COLOR: #0000ff">="(select pr.SubProductAllowable <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                from ProductRelease pr <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                where pr.productReleaseID=<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                        productReleaseID)"</span>
						<span style="COLOR: #ff0000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        type</span>
						<span style="COLOR: #0000ff">="integer"</span>
						<span style="COLOR: #ff0000"> </span>
						<span style="COLOR: #0000ff">/&gt;</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">&lt;</span>
						<span style="COLOR: #800000">subclass  </span>
						<span style="COLOR: #ff0000">name</span>
						<span style="COLOR: #0000ff">="NestedProduct"</span>
						<span style="COLOR: #ff0000">  <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        discriminator-value</span>
						<span style="COLOR: #0000ff">="1"</span>
						<span style="COLOR: #0000ff">/&gt;</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span>
						<span style="COLOR: #0000ff">&lt;/</span>
						<span style="COLOR: #800000">class</span>
						<span style="COLOR: #0000ff">&gt;</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">&lt;/</span>
						<span style="COLOR: #800000">hibernate-mapping</span>
						<span style="COLOR: #0000ff">&gt;</span>
				</div>
				<p>    如果 formula 表达式评估结果为 0 时－－也就是不允许次品出厂－－那么对象将是 Product 类。如果结果是 1，那么对象将是 NestedProduct。在表 1 和 2 中，表 Product 的第一条记录（ProductID=10000001），已初始化的类将是 NestedProduct，因为它参考一条 SubProductAllowable=1 的 ProductRelease 记录。表 Product 的第二条记录（ProductID=20000001），已初始化的类将是 Product，因为它参考一条 SubProductAllowable=0 的 ProductRelease 记录。<br />    </p>
				<table width="46%" border="1">
						<tbody>
								<tr>
										<td width="10%">S/N</td>
										<td width="35%">
												<code>
														<font color="#003366">ProductReleaseID</font>
												</code>
										</td>
										<td width="35%">
												<code>
														<font color="#003366">SubProductAllowable</font>
												</code>
										</td>
										<td width="20%">...</td>
								</tr>
								<tr>
										<td>1</td>
										<td>11</td>
										<td>1</td>
										<td>¡­</td>
								</tr>
								<tr>
										<td>2</td>
										<td>601</td>
										<td>0</td>
										<td>¡­</td>
								</tr>
						</tbody>
				</table>        表 1. ProductRelease 表中的记录<br />    
<table width="46%" border="1"><tbody><tr><td width="10%">S/N</td><td width="35%"><code><font color="#003366">ProductID</font></code></td><td width="35%"><code><font color="#003366">ProductReleaseID</font></code></td><td width="20%">...</td></tr><tr><td>1</td><td>10000001</td><td>11</td><td>¡­</td></tr><tr><td>2</td><td>20000001</td><td>601</td><td>...</td></tr></tbody></table>        表 2. Product 表中的记录<br />    <br />    <strong>Property<br /></strong>    <br />    在 property 元素中的 formula 允许对象属性包含特定引伸值，比如对结果进行 sum、average、max 等等。
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="averagePrice"</span><span style="COLOR: #ff0000"> formula</span><span style="COLOR: #0000ff">="(select avg(pc.price) from PriceCatalogue pc, SelectedItems si where si.priceRefID=pc.priceID)"</span><span style="COLOR: #0000ff">/&gt;</span></div><p>    此外，formula 也能协助从基于当前记录的特定值向其它表检索数据。例如：</p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="currencyName"</span><span style="COLOR: #ff0000"> formula</span><span style="COLOR: #0000ff">="(select cur.name from currency cur where cur.id= currencyID)"</span><span style="COLOR: #0000ff">/&gt;</span></div><p>    这将由助于从 currency 表检索 currency name。正如你所看到的，这样直接映射可消除许多转换编码。<br />    <br />    <strong>map-key</strong><br />    <br />    formula 允许 map-key 持有任何可能的值。下列范例（图 3），我们想让 Role_roleID 成为对象模型的 map-key（图 4）。<br /><br />                 <img height="417" alt="User Role Data Schema" src="http://www.onjava.com/onjava/2005/08/03/graphics/image003.jpg" width="316" />   <br />                          图 3. 用户角色数据 schema<br /><br />   <img height="180" alt="User Role Object Model" src="http://www.onjava.com/onjava/2005/08/03/graphics/image004.jpg" width="431" /><br />                            图 4. 用户角色对象模型<br />    <br />    在前面的数据 schema 中，User 和 Role 由 User_has_Role 通过 many-to-many 关系关联调用。为了获取某个 User 所有的指派角色，我们进行如下映射：<font color="#000080"><hibernate-mapping></hibernate-mapping></font></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">class </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="User"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">id </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="userID"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">map </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="roles"</span><span style="COLOR: #ff0000">  <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        table</span><span style="COLOR: #0000ff">="UserRole"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">key </span><span style="COLOR: #ff0000">column</span><span style="COLOR: #0000ff">="User_userID"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">map-key <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #ff0000">formula</span><span style="COLOR: #0000ff">="Role_RoleID"</span><span style="COLOR: #ff0000"> <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        type</span><span style="COLOR: #0000ff">="string"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">many-to-many <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #ff0000">column</span><span style="COLOR: #0000ff">="Role_RoleID"</span><span style="COLOR: #ff0000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        class</span><span style="COLOR: #0000ff">="Role"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">map</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">class </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="Role"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">id </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="roleID"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span></div><p>    Role_RoleID 通常用于连接 many-to-many 元素的栏位值。Hibernate 通常不允许 Role_RoleID 出现在 map-key 和 many-to-many 的栏位属性中。但是有了 formula，Role_RoleID 也能用于 map-key。<br />    <br />    <strong>element、map-key-many-to-many 以及其他<br /></strong>    <br />    element 和 property 类似，能从任何有效的 formula 表达式赋予评估值。<br />    <br />    map-key-many-to-many 中 formula 的用法类似于 map-key。但是，map-key-many-to-many 通常用于三重关系，map key 是一个被自己参考的对象，而不是被参考的属性。<br />    <br />    那么，到底哪些情况下 formula 不支持呢？有些数据库（例如 Oracle 7）就不支持嵌入式 select 语句（也就是说一条 select SQL 内嵌在另外一条 select SQL 语句中），这种情况 formula 就不支持评估结果。因此，你应该首先检查嵌入式 select SQL 语句是否被支持。<br />    <br />    一旦 Hibernate 的映射拿 formula 表达式作为 select SQL 选取的一部分，请确认数据库 SQL 方言是否允许使用 formula，尽管这样将降低代码轻便性。<br />    <br /><strong><font size="4">范畴 2：formula 用于连接</font></strong></p><p>    <strong>many-to-one<br /></strong>    <br />    另一个普遍的场景是真实世界的数据模型是所有者关系映射，这意味着映射是不同于 one-to-one、one-to-many 以及 many-to-many 关系的，formula 是提供所有者关系管理元素中的一个。图 5 展示了某公司可有多个联系人，但是其中只有一个为默认联系人的范例。一个公司有多个联系人是典型的 one-to-many 关系。但是，为了标识默认联系人，ContactPerson 表使用了 defaultFlag 参数（1 是 yes, 0是 no）。<br /><br />                <img height="327" src="http://www.onjava.com/onjava/2005/08/03/graphics/image005.jpg" width="272" /><br />                        图 5. 用户角色数据 schema<br /><br />              <img height="121" src="http://www.onjava.com/onjava/2005/08/03/graphics/image006.jpg" width="281" /><br />                           图 6. 用户角色对象模型<br />    <br />    为了说明对象模型（图 6）中默认联系人关系，我们使用如下映射文件：<font color="#000080"></font></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">class </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="Company"</span><span style="COLOR: #ff0000"> table</span><span style="COLOR: #0000ff">="Company"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">id </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="id"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">many-to-one <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />      </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="defaultContactPerson"</span><span style="COLOR: #ff0000"> <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />      property-ref</span><span style="COLOR: #0000ff">="defaultContactPerson"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">column </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="id"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">formula</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">formula</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">many-to-one</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">class </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="Person"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">id </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="id"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">properties </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="defaultContactPerson"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="companyID"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="defaultFlag"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">properties</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span></div><p>    如上，我们把 companyID 和 defaultFlag 组织到名为 defaultContactPerson 的 properties 元素中，做为 Person 表的 unique key。Company 类中的 many-to-one 元素连接 Person 表的 defaultContactPerson properties 元素。输出的 SQL 像这样：<br />    <br />    <font color="#000080">select c.id, p.id from Company c, Person p where p.companyID=c.id and p.defaultFlag=1<br /></font>    <br />    <strong>one-to-one</strong><br />    <br />    Hibernate 中，one-to-one 主要用于两张表共享同一主键的情况。对于外键关联，我们通常使用 many-to-one 来代替。但是，有了 formula，one-to-one 可以通过外键连接表。上面的 many-to-one 范例可以通过 one-to-one 来映射：</p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">class </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="Company"</span><span style="COLOR: #ff0000"> table</span><span style="COLOR: #0000ff">="Company"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">id </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="id"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">one-to-one </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="defaultContactPerson"</span><span style="COLOR: #ff0000"> <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        property-ref</span><span style="COLOR: #0000ff">="defaultContactPerson"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">formula</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">id</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">formula</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">formula</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">formula</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">one-to-one</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />  </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">class </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="Person"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">id </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="id"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">properties </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="defaultContactPerson"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="companyID"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="defaultFlag"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">properties</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />   </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">hibernate-mapping</span><span style="COLOR: #0000ff">&gt;</span></div><p>    <strong>many-to-many<br /></strong>    <br />    formula 用于当 many-to-many 元素为关系表和实体表连接的特殊关系，尽管通常没有必要这样用。<br />    <br /><strong><font size="4">总结</font></strong></p><p>    文章范例展示了大部分 formula 的适用情景。当需要 formula 评估值时，formula 表达式将出现在 产生的 SQL 语句的 select 部分。当 formula 用于连接时，它出现在产生的 SQL 语句的 where 部分。此外，formula 表达式可用于任何 SQL 方言，只要目标数据库支持。最后，formula 可协助完成从数据模型到对象模型的无代码细粒度映射。<br /></p></div>
<img src ="http://www.blogjava.net/TrampEagle/aggbug/30584.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-14 13:38 <a href="http://www.blogjava.net/TrampEagle/articles/30584.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>为domain object注入依赖</title><link>http://www.blogjava.net/TrampEagle/articles/30263.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Sat, 11 Feb 2006 06:30:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/30263.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/30263.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/30263.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/30263.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/30263.html</trackback:ping><description><![CDATA[<P>原文引自：<A href="http://www.javaeye.com/pages/viewpage.action?pageId=1141">http://www.javaeye.com/pages/viewpage.action?pageId=1141</A><BR><BR>作者: 冰云<BR>整理:robbin</P>
<DIV class=panel>
<DIV class=panelContent>
<P>原理：利用Hibernate3提供的PostLoadEventListener在loadHibernate实体类的时候触发PostLoadEvent事件，编写一个自定义的事件监听器，注入依赖的Spring Bean对象</P></DIV></DIV>
<DIV class=code>
<DIV class=codeHeader><B>SpringHibernateInjector</B></DIV>
<DIV class=codeContent><PRE class=code-java><SPAN class=code-keyword>import</SPAN> org.hibernate.event.PostLoadEvent;
<SPAN class=code-keyword>import</SPAN> org.hibernate.event.PostLoadEventListener;
<SPAN class=code-keyword>import</SPAN> org.springframework.beans.factory.BeanFactory;
<SPAN class=code-keyword>import</SPAN> org.springframework.beans.factory.BeanFactoryAware;
<SPAN class=code-keyword>import</SPAN> org.springframework.beans.factory.config.AutowireCapableBeanFactory;

<SPAN class=code-keyword>public</SPAN> class SpringHibernateInjector <SPAN class=code-keyword>implements</SPAN> PostLoadEventListener, BeanFactoryAware{

    AutowireCapableBeanFactory beanFactory;
   
    <SPAN class=code-keyword>public</SPAN> void onPostLoad(PostLoadEvent event) {
        <SPAN class=code-object>Object</SPAN> hibernateObject = event.getEntity();
        beanFactory.autowireBeanProperties(hibernateObject, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, <SPAN class=code-keyword>false</SPAN>);   
    }

    <SPAN class=code-keyword>public</SPAN> void setBeanFactory(BeanFactory factory)  {
        beanFactory = (AutowireCapableBeanFactory) factory;
    }

}</PRE></DIV></DIV>
<DIV class=code>
<DIV class=codeHeader><B>applicationContext.xml</B></DIV>
<DIV class=codeContent><PRE class=code-xml>&lt;bean id=<SPAN class=code-quote>"sessionFactory"</SPAN>
        class=<SPAN class=code-quote>"org.springframework.orm.hibernate3.LocalSessionFactoryBean"</SPAN>&gt;
        <SPAN class=code-tag>&lt;property name=<SPAN class=code-quote>"dataSource"</SPAN>&gt;</SPAN>
            <SPAN class=code-tag>&lt;ref bean=<SPAN class=code-quote>"dataSource"</SPAN> /&gt;</SPAN>
        <SPAN class=code-tag>&lt;/property&gt;</SPAN>
        <SPAN class=code-tag>&lt;property name=<SPAN class=code-quote>"mappingResources"</SPAN>&gt;</SPAN>
            <SPAN class=code-tag>&lt;list&gt;</SPAN>
                ...domains.hbm.xml here...
            <SPAN class=code-tag>&lt;/list&gt;</SPAN>
        <SPAN class=code-tag>&lt;/property&gt;</SPAN>
        <SPAN class=code-tag>&lt;property name=<SPAN class=code-quote>"hibernateProperties"</SPAN>&gt;</SPAN>
            <SPAN class=code-tag>&lt;props&gt;</SPAN>
                <SPAN class=code-tag>&lt;prop key=<SPAN class=code-quote>"hibernate.dialect"</SPAN>&gt;</SPAN>
                    org.hibernate.dialect.Oracle9Dialect
                <SPAN class=code-tag>&lt;/prop&gt;</SPAN>
                <SPAN class=code-tag>&lt;prop key=<SPAN class=code-quote>"hibernate.query.substitutions"</SPAN>&gt;</SPAN>
                    true 1, false 0
                <SPAN class=code-tag>&lt;/prop&gt;</SPAN>
                <SPAN class=code-tag>&lt;prop key=<SPAN class=code-quote>"hibernate.show_sql"</SPAN>&gt;</SPAN>false<SPAN class=code-tag>&lt;/prop&gt;</SPAN>
            <SPAN class=code-tag>&lt;/props&gt;</SPAN>
        <SPAN class=code-tag>&lt;/property&gt;</SPAN>
       <SPAN class=code-tag>&lt;property name=<SPAN class=code-quote>"eventListeners"</SPAN>&gt;</SPAN>
          <SPAN class=code-tag>&lt;map&gt;</SPAN>
            <SPAN class=code-tag>&lt;entry key=<SPAN class=code-quote>"post-load"</SPAN>&gt;</SPAN>
              &lt;!-- This hibernate interceptor allows us to use Spring to inject services
 into Hibernate managed domain objects --&gt;   
              <SPAN class=code-tag>&lt;bean class=<SPAN class=code-quote>"yourpackage.SpringHibernateInjector"</SPAN>/&gt;</SPAN>
            <SPAN class=code-tag>&lt;/entry&gt;</SPAN>        
           <SPAN class=code-tag>&lt;/map&gt;</SPAN>
    <SPAN class=code-tag>&lt;/property&gt;</SPAN>
    <SPAN class=code-tag>&lt;/bean&gt;</SPAN></PRE></DIV></DIV>
<P>注：此方法来自于<SPAN class=nobr><A title="Visit page outside Confluence" href="http://www.thoughtworks.com/" rel=nofollow>ThoughtWorks<SUP><IMG class=rendericon height=7 alt="" src="http://www.javaeye.com/images/icons/linkext7.gif" width=7 align=absMiddle border=0></SUP></A></SPAN>的 <SPAN class=nobr><A title="Visit page outside Confluence" href="http://www.jroller.com/page/perryn" rel=nofollow>PerrynFowler<SUP><IMG class=rendericon height=7 alt="" src="http://www.javaeye.com/images/icons/linkext7.gif" width=7 align=absMiddle border=0></SUP></A></SPAN>，俺只是摘抄一下</P><!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
         xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
    rdf:about="http://www.javaeye.com/pages/viewpage.action?pageId=1141"
    dc:identifier="http://www.javaeye.com/pages/viewpage.action?pageId=1141"
    dc:title="为domain object注入依赖"
    trackback:ping="http://www.javaeye.com/rpc/trackback/1141"/>
</rdf:RDF>
--><!--
    Root decorator: all decisions about how a page is to be decorated via the
                    inline decoration begins here.
--><!--
    Switch based upon the context. However, for now, just delegate to a decorator
    identified directly by the context.
--><img src ="http://www.blogjava.net/TrampEagle/aggbug/30263.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-11 14:30 <a href="http://www.blogjava.net/TrampEagle/articles/30263.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用UserType来持久化复合类型属性</title><link>http://www.blogjava.net/TrampEagle/articles/30247.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Sat, 11 Feb 2006 01:23:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/30247.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/30247.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/30247.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/30247.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/30247.html</trackback:ping><description><![CDATA[<P>原文引自：<A href="http://forum.javaeye.com/viewtopic.php?t=9035">http://forum.javaeye.com/viewtopic.php?t=9035</A><BR><BR><SPAN class=postbody>偶们经常会遇到一些用户需求, 需要实现一个区间类型的东东: <BR><BR>public class Entity { <BR>private Date startDate; <BR>private Date endDate; <BR>private ......; <BR>} <BR><BR>如项目的开始/结束时间, 人员的任职期间等等 <BR><BR>但是如要比较这个对象和其他对象区间的关系, 就得写一些恶心的code: <BR>if(this.startDate &gt; that.startDate &amp;&amp; this.endDate &lt; that.endDate) <BR>if(this.startDate &lt; that.startDate) ...... <BR>一堆的if else了. <BR><BR>或许你会觉得这些小东西这样写就可以了, 但是为了有一个更完美, 更好用的Domain Object, 是值得偶们在这些小细节上操劳的. <BR><BR>有一些现成的lib就是做这些东西的, 比如: <BR><A href="http://timeandmoney.sourceforge.net/" target=_blank>http://timeandmoney.sourceforge.net/</A> <BR><A href="http://joda-time.sourceforge.net/" target=_blank>http://joda-time.sourceforge.net/</A> <BR><BR>这里用timeandmoney lib为例子, 来介绍一下利用Hibernate的UserType来创建一个Domain Object <BR><BR>首先是一个业务对象: <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> com.<SPAN style="COLOR: #000000">domainlanguage</SPAN>.<SPAN style="COLOR: #000000">time</SPAN>.<SPAN style="COLOR: #000000">TimeInterval</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> RecordLog <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>extends</SPAN> <SPAN style="COLOR: #aaaadd" ?>Entity</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> description; <BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> TimeInterval interval; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #6666ff">//getters and setters......</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN> <BR></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>然后是mapping文件: <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>xml代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;class name=<SPAN style="COLOR: #ff0000">"RecordLog"</SPAN>&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;id name=<SPAN style="COLOR: #ff0000">"id"</SPAN>&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;generator class=<SPAN style="COLOR: #ff0000">"native"</SPAN>/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/id&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=<SPAN style="COLOR: #ff0000">"description"</SPAN>/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=<SPAN style="COLOR: #ff0000">"interval"</SPAN> type=<SPAN style="COLOR: #ff0000">"TimeIntervalType"</SPAN>&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;column name=<SPAN style="COLOR: #ff0000">"LOWER_LIMIT"</SPAN>/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;column name=<SPAN style="COLOR: #ff0000">"INCLUDES_LOWER_LIMIT"</SPAN>/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;column name=<SPAN style="COLOR: #ff0000">"UPPER_LIMIT"</SPAN>/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;column name=<SPAN style="COLOR: #ff0000">"INCLUDES_UPPER_LIMIT"</SPAN>/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/class&gt; <BR></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>一个操作它的Manager: <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> Manager <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>extends</SPAN> HibernateDaoSupport <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> RecordLog load<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>Long</SPAN> id<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> <SPAN style="COLOR: #000000">(</SPAN>RecordLog<SPAN style="COLOR: #000000">)</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">load</SPAN><SPAN style="COLOR: #000000">(</SPAN>RecordLog.<SPAN style="COLOR: #000000">class</SPAN>, id<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> save<SPAN style="COLOR: #000000">(</SPAN>RecordLog log<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">saveOrUpdate</SPAN><SPAN style="COLOR: #000000">(</SPAN>log<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN> <BR></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>偶们先来看看它是怎么运行的: <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> test<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; TimePoint nov01 = TimePoint.<SPAN style="COLOR: #000000">atMidnightGMT</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>2004</SPAN>, <SPAN style="COLOR: #000000" ?>11</SPAN>, <SPAN style="COLOR: #000000" ?>01</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; TimePoint nov03 = TimePoint.<SPAN style="COLOR: #000000">atMidnightGMT</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>2004</SPAN>, <SPAN style="COLOR: #000000" ?>11</SPAN>, <SPAN style="COLOR: #000000" ?>03</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; TimePoint nov02 = TimePoint.<SPAN style="COLOR: #000000">atMidnightGMT</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>2004</SPAN>, <SPAN style="COLOR: #000000" ?>11</SPAN>, <SPAN style="COLOR: #000000" ?>02</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; TimePoint nov05 = TimePoint.<SPAN style="COLOR: #000000">atMidnightGMT</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>2004</SPAN>, <SPAN style="COLOR: #000000" ?>11</SPAN>, <SPAN style="COLOR: #000000" ?>05</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; RecordLog log1 = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> RecordLog<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; log1.<SPAN style="COLOR: #000000">setDescription</SPAN><SPAN style="COLOR: #000000">(</SPAN>"Record Log <SPAN style="COLOR: #000000" ?>1</SPAN>"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; log1.<SPAN style="COLOR: #000000">setInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN>TimeInterval.<SPAN style="COLOR: #000000">closed</SPAN><SPAN style="COLOR: #000000">(</SPAN>nov01, nov03<SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; RecordLog log2 = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> RecordLog<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; log2.<SPAN style="COLOR: #000000">setDescription</SPAN><SPAN style="COLOR: #000000">(</SPAN>"Record Log <SPAN style="COLOR: #000000" ?>2</SPAN>"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; log2.<SPAN style="COLOR: #000000">setInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN>TimeInterval.<SPAN style="COLOR: #000000">closed</SPAN><SPAN style="COLOR: #000000">(</SPAN>nov02, nov05<SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #6666ff">//这里, 偶们只取交叉区间</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #6666ff">//比原来的一堆if else简洁多了吧? </SPAN><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN><SPAN style="COLOR: #000000">(</SPAN>log1.<SPAN style="COLOR: #000000">getInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">intersects</SPAN><SPAN style="COLOR: #000000">(</SPAN>log2.<SPAN style="COLOR: #000000">getInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log1.<SPAN style="COLOR: #000000">setInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN>log1.<SPAN style="COLOR: #000000">getInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">intersect</SPAN><SPAN style="COLOR: #000000">(</SPAN>log2.<SPAN style="COLOR: #000000">getInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; manager.<SPAN style="COLOR: #000000">save</SPAN><SPAN style="COLOR: #000000">(</SPAN>log1<SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; RecordLog loaded = manager.<SPAN style="COLOR: #000000">load</SPAN><SPAN style="COLOR: #000000">(</SPAN>log1.<SPAN style="COLOR: #000000">getId</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<SPAN style="COLOR: #000000">(</SPAN>"Record Log <SPAN style="COLOR: #000000" ?>1</SPAN>", loaded.<SPAN style="COLOR: #000000">getDescription</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<SPAN style="COLOR: #000000">(</SPAN>nov02, loaded.<SPAN style="COLOR: #000000">getInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">lowerLimit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<SPAN style="COLOR: #000000">(</SPAN>nov03, loaded.<SPAN style="COLOR: #000000">getInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">upperLimit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<SPAN style="COLOR: #000000">(</SPAN>loaded.<SPAN style="COLOR: #000000">getInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">includesLowerLimit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<SPAN style="COLOR: #000000">(</SPAN>loaded.<SPAN style="COLOR: #000000">getInterval</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">includesUpperLimit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>怎么样, 是不是比原来的代码简单多了? 在背后干脏活,累活的就是这个TimeIntervalType和TimeAndMoney Lib: <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>代码格式好难看阿, 只好用quote 写道:</B></SPAN></TD></TR>
<TR>
<TD class=quote><BR>public class TimeIntervalType implements UserType { <BR><BR>private static final int[] SQL_TYPES = new int[] { Hibernate.TIMESTAMP.sqlType(), Hibernate.BOOLEAN.sqlType(), Hibernate.TIMESTAMP.sqlType(), <BR>Hibernate.BOOLEAN.sqlType() }; <BR><BR>public int[] sqlTypes() { <BR>return SQL_TYPES; <BR>} <BR><BR>public Class returnedClass() { <BR>return TimeInterval.class; <BR>} <BR><BR>public boolean equals(Object x, Object y) throws HibernateException { <BR>if (x == y) <BR>return true; <BR>if (x == null || y == null) <BR>return false; <BR>return ((TimeInterval) x).compareTo((TimeInterval) y) == 0; <BR>} <BR><BR>public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { <BR>TimePoint lower = TimePoint.from((Timestamp) Hibernate.TIMESTAMP.nullSafeGet(rs, names[0])); <BR>boolean lowerIncluded = ((Boolean) Hibernate.BOOLEAN.nullSafeGet(rs, names[1])).booleanValue(); <BR>TimePoint upper = TimePoint.from((Timestamp) Hibernate.TIMESTAMP.nullSafeGet(rs, names[2])); <BR>boolean upperIncluded = ((Boolean) Hibernate.BOOLEAN.nullSafeGet(rs, names[3])).booleanValue(); <BR>return new TimeInterval(lower, lowerIncluded, upper, upperIncluded); <BR>} <BR><BR>public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { <BR>TimeInterval interval = (TimeInterval) value; <BR>Hibernate.TIMESTAMP.nullSafeSet(st, new Timestamp(((TimePoint) interval.lowerLimit()).asJavaUtilDate().getTime()), index); <BR>Hibernate.BOOLEAN.nullSafeSet(st, new Boolean(interval.includesLowerLimit()), index + 1); <BR>Hibernate.TIMESTAMP.nullSafeSet(st, new Timestamp(((TimePoint) interval.upperLimit()).asJavaUtilDate().getTime()), index + 2); <BR>Hibernate.BOOLEAN.nullSafeSet(st, new Boolean(interval.includesUpperLimit()), index + 3); <BR>} <BR><BR>public Object deepCopy(Object value) throws HibernateException { <BR>if (value == null) <BR>return null; <BR>TimeInterval interval = (TimeInterval) value; <BR>return new TimeInterval((TimePoint) interval.lowerLimit(), interval.includesLowerLimit(), (TimePoint) interval.upperLimit(), interval <BR>.includesUpperLimit()); <BR>} <BR><BR>public boolean isMutable() { <BR>return true; <BR>} <BR><BR>} <BR></TD></TR></TBODY></TABLE></P><img src ="http://www.blogjava.net/TrampEagle/aggbug/30247.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-11 09:23 <a href="http://www.blogjava.net/TrampEagle/articles/30247.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> OpenSessionInView会不会影响性能？</title><link>http://www.blogjava.net/TrampEagle/articles/30094.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Fri, 10 Feb 2006 01:52:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/30094.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/30094.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/30094.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/30094.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/30094.html</trackback:ping><description><![CDATA[原文摘自：<A href="http://forum.javaeye.com/viewtopic.php?t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0">http://forum.javaeye.com/viewtopic.php?t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0</A><BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105066#105066"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-11 12:15:26<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105066"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0&amp;p=105066"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>hongliang 写道:</B></SPAN></TD></TR>
<TR>
<TD class=quote>那这么说来，假设Hibernate在第一秒拿到叻数据库连接，这个连接不还是持续15秒么？</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>如果是网络传输速度导致load页面需要15秒，那么时间消耗在网络传输上，本身页面执行时间很短。 <BR><BR>如果不是网络传输速度导致load页面需要15秒，那说明什么？说明这个页面的程序执行了15秒！为什么一个页面程序需要执行这么久呢？说明这个页面的数据库查询非常耗时！(纯内存操作都是ms级别的，如果都10多秒了，瓶颈只可能是数据库操作) 如果数据库查询需要15秒，请问你就算不用OpenSessionInView，你不是一样需要打开数据库连接长达15秒吗？</SPAN><SPAN class=postbody></SPAN><SPAN class=gensmall></SPAN></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105070#105070"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-11 12:32:42<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105070"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0&amp;p=105070"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody>我明白robbin你的意思，我说的情况就是网络传输导致的15秒。本身页面执行的时间，比如freemarker渲染一下模板，这个速度是非常快的，没问题，可是我的疑问就在于如果网络传输狠慢，会不会影响到数据库连接。 <BR><BR>假设WebWork+Hibernate+FreeMarker架构模型是这样的 <BR><BR>Request <BR>| <BR>|---other filters... <BR>| <BR>|---OpenSessionInView Filter <BR>| <BR>|-----WebWork Controller <BR>| <BR>|---Action <BR>| <BR>|---FreeMarker Result(对response.getWriter()做process()操作) <BR>| <BR>| <BR>|---OpenSessionInView Filter <BR>| <BR>|---other filters... <BR>| <BR>Request <BR><BR><BR>这里有两种情况。 <BR><BR>一是页面缓冲区足够大，足够一次性容纳所有的页面，这样渲染页面就会一次性进入缓冲区，然后返回到OpenSessionInView Filter，关闭Session，数据库连接返回池中，一切OK。 <BR><BR>第二种情况我是重点想讨论的，也是我的疑虑所在。那就是假如这个页面比较大，超出叻页面缓冲区的大小，那么渲染页面时，就拿FreeMarker/Velocity这样的模板语言来说，它执行process/merge方法，往servlet的response writer/outputStream里面写东西，写着写着，发现写不动叻，是缓冲区满叻，而这个writer/outputStream，正是服务器同用户之间建立的socket请求，于是底层代码开始先向用户传输一部分页面，传好后，又有叻新的缓冲区，FreeMarker/Velocity的方法又能向缓冲区里写东西叻。而传输页面这个过程，又耗费叻几秒钟的时间，就导致叻数据库连接被占用叻狠长的时间。 <BR><BR>可能我描述的是错误的，希望robbin指正！：）</SPAN></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105072#105072"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-11 13:07:27<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105072"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0&amp;p=105072"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody>我觉得这个问题归根结底就是AppServer究竟会如何实现页面输出。那么必然和具体的应用服务器实现有关系。那么至于每个AppServer究竟会怎样去实现，我就不得而知了。起码大家可以研究一下Tomcat源代码看看tomcat是如何实现的。 <BR><BR>confluence采用的就是Hibernate/Spring/Webwork架构，OpenSessionInView，以confluence这么广的使用，好像也没有听过这方面的问题投诉。</SPAN><SPAN class=postbody></SPAN><SPAN class=gensmall></SPAN></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105098#105098"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-11 18:26:22<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105098"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0&amp;p=105098"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody>我写了一个简单的webapp在Tomcat5.5.12上面做了一个小测试。在JSP页面里面循环1万次输出字符串，程序在远程服务器上面运行，网络是ADSL宽带，filter确实被阻塞了20秒左右。然后我另外开了一个flashget去下载服务器上的大文件，模拟网络速度比较慢的环境，filter被阻塞了50秒左右。分别做了三次测试。另外当页面下载过程中直接点击浏览器stop按钮，则JSP执行被打断，filter立刻解除阻塞，被执行完毕。 <BR><BR>结论证明，使用OpenSessionInView的时候，如果render的页面数据量非常大，并且客户端网络速度很慢的情况下，由于页面的输出时间过程很长，确实会造成filter被长时间阻塞。对于OpenSessionInViewFilter来说，就会造成数据库连接被保持很长的时间，才能被关闭。 <BR><BR>不过，对于Spring的OpenSessionInViewFilter来说，虽然数据库连接被保持了过长的时间，但是并没有锁定数据库资源，特别是事务资源。因为Spring的事务是通过TransactionInterceptor来实现的，在MVC结构中，当最后一个业务bean被调用结束以后，Transaction就已经被提交了。此后，虽然数据库连接还保持中，但是数据库资源没有锁定问题。 <BR><BR>完整的调用示意图： <BR><BR>request -&gt; (OpenSessionInViewFilter打开Session) -&gt; ServletDispatcher -&gt; Action -&gt; (打开Connection，启动事务) -&gt; spring bean -&gt; another spring bean -&gt; (提交事务) -&gt; bean执行完毕，返回Action -&gt; render view(JSP/Template) -&gt; (OpenSessionInViewFilter关闭Session和Connection)</SPAN></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105128#105128"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-11 22:44:45<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105128"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0&amp;p=105128"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody>robbin的分析很透彻，对于最后一点，我稍有疑问。 <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote><BR>对于Spring的OpenSessionInViewFilter来说，虽然数据库连接被保持了过长的时间，但是并没有锁定数据库资源，特别是事务资源。 <BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>其实我认为数据库连接被保持过长时间有时候会有很大的问题。尤其是对于采用数据连接池的情况，如果你的数据库连接一直被保持，那么这个资源就未被释放。假设说这个数据连接池的最大连接数为15，我感觉很容易造成数据库的连接不够用。 <BR><BR>不清楚底层的实现是如何做的，或许我的疑问有些多虑。</SPAN></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105129#105129"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-11 22:51:56<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105129"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0&amp;p=105129"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>downpour 写道:</B></SPAN></TD></TR>
<TR>
<TD class=quote>robbin的分析很透彻，对于最后一点，我稍有疑问。 <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote><BR>对于Spring的OpenSessionInViewFilter来说，虽然数据库连接被保持了过长的时间，但是并没有锁定数据库资源，特别是事务资源。 <BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>其实我认为数据库连接被保持过长时间有时候会有很大的问题。尤其是对于采用数据连接池的情况，如果你的数据库连接一直被保持，那么这个资源就未被释放。假设说这个数据连接池的最大连接数为15，我感觉很容易造成数据库的连接不够用。 <BR><BR>不清楚底层的实现是如何做的，或许我的疑问有些多虑。</SPAN></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>按道理来说，数据库连接应该尽早被释放，以缓解数据库资源的压力，延迟很久才释放，确实会导致需要更多的数据库连接。这个就只能扩大连接池数量，增加数据库最大允许连接数来解决了。 <BR><BR>此外，Session被延迟很久释放，那么Session占用的一级缓存也会占用比较长时间，这意味着会无谓消耗更多的JVM内存。 <BR><BR>因此，OpenSessionInView虽然确实方便，但是大家还是慎用吧。对于那些页面渲染速度很慢，拨号连接用户数量过多的网站就最好不要使用。</SPAN></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105482#105482"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-14 10:22:24<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105482"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0&amp;p=105482"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote>因此，OpenSessionInView虽然确实方便，但是大家还是慎用吧。对于那些页面渲染速度很慢，拨号连接用户数量过多的网站就最好不要使用。</TD></TR></TBODY></TABLE><SPAN class=postbody><BR>确切的应该是大并发用户量的情况吧。这个问题一直都存在，在1年多前我和robbin争论中就提出来了过。hibernate2的代码可以看到session是和connection紧密耦合的（Hibernate3没看过）。但hibernate大部分被用于并发用户可预见的intranet应用，所以问题也不是很大。如果并发用户多，对connection pool资源, opensession in view在hibernate中使用会构成较大压力。如果jboss j2ee5 server采用hibernate作为ejb3实现，没有做修正的话，同样的问题也会存在于jboss j2ee5 server中。</SPAN><SPAN class=postbody></SPAN><SPAN class=gensmall><BR><BR>上一次由Charlesxp于2005-12-14 周三, 上午10:25修改，总共修改了2次</SPAN></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105483#105483"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-14 10:22:33<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105483"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=0&amp;p=105483"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>hongliang 写道:</B></SPAN></TD></TR>
<TR>
<TD class=quote>几天没来，居然变精华叻。。。本来我也想做一下robbin的那个测试，结果这几天忙于其它事，一直没时间。看来OpenSessionInView果然有这个问题，这也是我一直担心的，看来真是应叻那句话，“如果一件事可能出错，那它一定会出错”。。。 <BR><BR>不过，如果不用OpenSessionInView，我还真一下子就找不到北叻，从学Hibernate开始就一直在OpenSessionInView的熏陶下长大。。。-_-b <BR><BR>Robbin有什么好的办法能够在不使用OpenSessionInView的情况下比较好的处理页面吗？</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>在dao中对要render的集合强制初始化。</SPAN><SPAN class=postbody></SPAN><SPAN class=gensmall></SPAN></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><A href="http://forum.javaeye.com/viewtopic.php?p=105503#105503"><IMG title=文章 height=9 alt=文章 src="http://forum.javaeye.com/templates/subSilver/images/icon_minipost.gif" width=12 border=0></A><SPAN class=postdetails>时间: 2005-12-14 10:51:52<SPAN class=gen>&nbsp;</SPAN>&nbsp; &nbsp;标题: </SPAN></TD>
<TD vAlign=top noWrap><A href="http://forum.javaeye.com/posting.php?mode=quote&amp;p=105503"><IMG title=引用回复 alt=引用回复 src="http://forum.javaeye.com/templates/subSilver/images/lang_chinese_simplified/icon_quote.gif" border=0></A> <A href="http://forum.javaeye.com/addblog.php?type=javaeye&amp;title=OpenSessionInView会不会影响性能？&amp;t=17501&amp;postdays=0&amp;postorder=asc&amp;start=15&amp;p=105503"><IMG title=将这个帖子加入我的Blog alt=将这个帖子加入我的Blog src="http://forum.javaeye.com/templates/subSilver/images/icon_blog.gif" border=0></A></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>hongliang 写道:</B></SPAN></TD></TR>
<TR>
<TD class=quote>是不是像这样？ <BR><BR>foo.getBars()</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>Hibernate.initialize(foo.getBars);</SPAN><SPAN class=postbody></SPAN><SPAN class=gensmall></SPAN></TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/TrampEagle/aggbug/30094.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-10 09:52 <a href="http://www.blogjava.net/TrampEagle/articles/30094.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate中的延迟加载</title><link>http://www.blogjava.net/TrampEagle/articles/30093.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Fri, 10 Feb 2006 01:47:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/30093.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/30093.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/30093.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/30093.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/30093.html</trackback:ping><description><![CDATA[原文引自：<A href="http://forum.javaeye.com/viewtopic.php?t=18128">http://forum.javaeye.com/viewtopic.php?t=18128</A><BR><BR><SPAN class=postbody>众所周知,到了Hibernate3.0以后,关联关系的对象默认都是使用延迟加载,例如&lt;one-to-many&gt;时.但我在映射&lt;one-to-one&gt;,&lt;many-to-one&gt;关系时指定了lazy="true",但是在查询对象时,我只想查询一个对象,仍然会把这个对象所关联的&lt;one-to-one&gt;,&lt;many-to-one&gt;对象一起查询出来,这样造成了极大的性能浪费.在不指定lazy属性时,&lt;many-to-one&gt;所关联的对象反而会延迟加载,这让我大为困惑,还以为是Hibernate的bug. <BR>在网上查找资料,说在为了延迟加载&lt;one-to-one&gt;,&lt;many-to-one&gt;所关联的对象,需要设置被关联的对象&lt;class name="" lazy="true"&gt;,我也这样做了,但是仍然没有效果. <BR>仔细阅读了Hibernate的手册,才发现原来要延迟加载&lt;one-to-one&gt;,&lt;many-to-one&gt;所关联的对象时,除了要指定lazy="true"外,还需要运行期字节码增强,而我省去了这一步,所以延迟加载没有效果.同时还发现在默认情况下,&lt;one-to-one&gt;,&lt;many-to-one&gt;的lazy属性是"proxy"而不是"true"!因此如果直接采用lazy的默认值,是可以延迟加载的. <BR>总结一下: <BR>&lt;many-to-one&gt;默认的属性是lazy="proxy",此时默认是会延迟加载的.在指定了lazy="true"之后,必须要经过运行期字节码增加,延迟加载才有效果. <BR>而&lt;one-to-one&gt;相对要复杂一点,延迟加载还要受到constrained属性的限制.constrained="false"时表明实体和被关联到的实体的约束不是强制的,即存在一个实体时,它通过&lt;one-to-one&gt;关联的实体可能存在,也可能不存在,这时在查询实体时,Hibernate总会发起一次查询检查&lt;one-to-one&gt;所关联的实体是否存在,而这时已经可以把one-to-one关联的实体查询出来了,因此在&lt;one-to-one&gt;关系中,如果constrained="false",总是会立即加载关联到的实体. <BR>如果当constrained="true",且lazy="proxy"(默认),是可以延迟加载的. <BR>如果当constrained="true",且lazy="true"时,需要经过运行期字节码增加,延迟加载才会奏效. <BR><BR><SPAN style="FONT-WEIGHT: bold">但是这里我还是有个疑问，既然在lazy="proxy"时，已经实现了延迟加载的效果，为什么在lazy="true"时，还需要动态字节码增强才能实现延迟加载呢？<BR><BR><SPAN class=postbody><BR>Re: Hibernate中的延迟加载<BR><BR><BR>以后在one-to-one或many-to-one中不会再有lazy="true"了，你可以理解成no-proxy． <BR><BR>[1]起码还是好的方向: 默认就可以lazy了. 而且来了extra :) <BR>[2]写Hibernate的哥们也是要活命的嘛, 不改变怎么来的咨询费, 不过Hibernate的migration写的也还不错. <BR>[3]constrained如你所说, 而且我估计这个属性以后不会有太大变更. <BR><BR>hope that helps :) <BR>regards</SPAN></SPAN></SPAN><img src ="http://www.blogjava.net/TrampEagle/aggbug/30093.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-10 09:47 <a href="http://www.blogjava.net/TrampEagle/articles/30093.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用JDBC和Hibernate来写入Blob型数据到Oracle中</title><link>http://www.blogjava.net/TrampEagle/articles/30085.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Fri, 10 Feb 2006 01:05:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/30085.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/30085.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/30085.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/30085.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/30085.html</trackback:ping><description><![CDATA[<SPAN class=postbody>原文引自：<A href="http://forum.javaeye.com/viewtopic.php?t=254">http://forum.javaeye.com/viewtopic.php?t=254</A><BR><BR>Oracle的Blob字段比较特殊，他比long字段的性能要好很多，可以用来保存例如图片之类的二进制数据。 <BR><BR>写入Blob字段和写入其它类型字段的方式非常不同，因为Blob自身有一个cursor，你必须使用cursor对blob进行操作，因而你在写入Blob之前，必须获得cursor才能进行写入，那么如何获得Blob的cursor呢？ <BR><BR>这需要你先插入一个empty的blob，这将创建一个blob的cursor，然后你再把这个empty的blob的cursor用select查询出来，这样通过两步操作，你就获得了blob的cursor，可以真正的写入blob数据了。 <BR><BR>看下面的JDBC的demo，把oraclejdbc.jar这个二进制文件写入数据库表javatest的content字段(这是一个blob型字段) <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">sql</SPAN>.*; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">io</SPAN>.*; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> oracle.<SPAN style="COLOR: #000000">sql</SPAN>.*; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> WriteBlob <SPAN style="COLOR: #000000">{</SPAN> <BR><BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>static</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> main<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> args<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR><BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>try</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>DriverManager</SPAN>.<SPAN style="COLOR: #000000">registerDriver</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> oracle.<SPAN style="COLOR: #000000">jdbc</SPAN>.<SPAN style="COLOR: #000000">driver</SPAN>.<SPAN style="COLOR: #000000">OracleDriver</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>Connection</SPAN> conn = <SPAN style="COLOR: #aaaadd" ?>DriverManager</SPAN>.<SPAN style="COLOR: #000000">getConnection</SPAN><SPAN style="COLOR: #000000">(</SPAN>"jdbc:oracle:thin:@localhost:<SPAN style="COLOR: #000000" ?>1521</SPAN>:orcl","fankai","fankai"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; conn.<SPAN style="COLOR: #000000">setAutoCommit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>false</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; BLOB blob = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>null</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>PreparedStatement</SPAN> pstmt = conn.<SPAN style="COLOR: #000000">prepareStatement</SPAN><SPAN style="COLOR: #000000">(</SPAN>"insert into javatest<SPAN style="COLOR: #000000">(</SPAN>name,content<SPAN style="COLOR: #000000">)</SPAN> values<SPAN style="COLOR: #000000">(</SPAN>?,empty_blob<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; pstmt.<SPAN style="COLOR: #000000">setString</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>1</SPAN>,"fankai"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; pstmt.<SPAN style="COLOR: #000000">executeUpdate</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; pstmt.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; pstmt = conn.<SPAN style="COLOR: #000000">prepareStatement</SPAN><SPAN style="COLOR: #000000">(</SPAN>"select content from javatest where name= ? <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>for</SPAN> update"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; pstmt.<SPAN style="COLOR: #000000">setString</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>1</SPAN>,"fankai"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>ResultSet</SPAN> rset = pstmt.<SPAN style="COLOR: #000000">executeQuery</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>rset.<SPAN style="COLOR: #000000">next</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN> blob = <SPAN style="COLOR: #000000">(</SPAN>BLOB<SPAN style="COLOR: #000000">)</SPAN> rset.<SPAN style="COLOR: #000000">getBlob</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>1</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>String</SPAN> fileName = "oraclejdbc.<SPAN style="COLOR: #000000">jar</SPAN>"; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>File</SPAN> f = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="COLOR: #aaaadd" ?>File</SPAN><SPAN style="COLOR: #000000">(</SPAN>fileName<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>FileInputStream</SPAN> fin = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="COLOR: #aaaadd" ?>FileInputStream</SPAN><SPAN style="COLOR: #000000">(</SPAN>f<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>System</SPAN>.<SPAN style="COLOR: #000000">out</SPAN>.<SPAN style="COLOR: #000000">println</SPAN><SPAN style="COLOR: #000000">(</SPAN>"file size = " + fin.<SPAN style="COLOR: #000000">available</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; pstmt = conn.<SPAN style="COLOR: #000000">prepareStatement</SPAN><SPAN style="COLOR: #000000">(</SPAN>"update javatest set content=? where name=?"<SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>OutputStream</SPAN> out = blob.<SPAN style="COLOR: #000000">getBinaryOutputStream</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> count = -<SPAN style="COLOR: #000000" ?>1</SPAN>, total = <SPAN style="COLOR: #000000" ?>0</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>byte</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> data = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>byte</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN><SPAN style="COLOR: #000000">)</SPAN>fin.<SPAN style="COLOR: #000000">available</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">]</SPAN>; <BR>&nbsp; &nbsp; &nbsp; fin.<SPAN style="COLOR: #000000">read</SPAN><SPAN style="COLOR: #000000">(</SPAN>data<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; out.<SPAN style="COLOR: #000000">write</SPAN><SPAN style="COLOR: #000000">(</SPAN>data<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #6666ff">/* <BR>&nbsp; &nbsp; &nbsp; byte[] data = new byte[blob.getBufferSize()];&nbsp; 另一种实现方法,节省内存 <BR>&nbsp; &nbsp; &nbsp; while ((count = fin.read(data)) != -1) { <BR>&nbsp; &nbsp; &nbsp; &nbsp; total += count; <BR>&nbsp; &nbsp; &nbsp; &nbsp; out.write(data, 0, count); <BR>&nbsp; &nbsp; &nbsp; } <BR>&nbsp; &nbsp; &nbsp; */</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; fin.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; out.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; pstmt.<SPAN style="COLOR: #000000">setBlob</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>1</SPAN>,blob<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; pstmt.<SPAN style="COLOR: #000000">setString</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>2</SPAN>,"fankai"<SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; pstmt.<SPAN style="COLOR: #000000">executeUpdate</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; pstmt.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; conn.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; conn.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>catch</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>SQLException</SPAN> e<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>System</SPAN>.<SPAN style="COLOR: #000000">err</SPAN>.<SPAN style="COLOR: #000000">println</SPAN><SPAN style="COLOR: #000000">(</SPAN>e.<SPAN style="COLOR: #000000">getMessage</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; e.<SPAN style="COLOR: #000000">printStackTrace</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>catch</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>IOException</SPAN> e<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>System</SPAN>.<SPAN style="COLOR: #000000">err</SPAN>.<SPAN style="COLOR: #000000">println</SPAN><SPAN style="COLOR: #000000">(</SPAN>e.<SPAN style="COLOR: #000000">getMessage</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR><BR><BR>仔细看上例，分三步： <BR><BR>1、插入空blob <BR>into javatest(name,content) values(?,empty_blob()); <BR><BR>2、获得blob的cursor <BR>select content from javatest where name= ? for update; <BR><BR>注意！！！必须加for update，这将锁定该行，直至该行被修改完毕，保证不产生并发冲突。 <BR><BR>3、update javatest set content=? where name= <BR><BR>用cursor往数据库写数据 <BR><BR>这里面还有一点要提醒大家： <BR><BR>JDK1.3带的JDBC2.0规范是不完善的，只有读Blob的接口，而没有写Blob的接口，JDK1.4带的JDBC3.0加入了写Blob的接口。你可以使用JDBC3.0的接口，也可以直接使用Oracle的JDBC的API，我在上例中使用了Oracle的JDBC的API。 <BR><BR>另外要注意的是： <BR><BR>java.sql.Blob <BR>oracle.sql.BLOB <BR><BR>注意看blob的大小写，是不一样的。写程序的时候不要搞混了。 <BR><BR>下面看看用Hibernate怎么写，原理是一样的，也要分三步，但是代码简单很多 <BR><BR>这是Cat对象定义 <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>package</SPAN> com.<SPAN style="COLOR: #000000">fankai</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">sql</SPAN>.<SPAN style="COLOR: #000000">Blob</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> Cat <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> id; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> name; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> char sex; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> float weight; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>Blob</SPAN> image; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> Cat<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> getId<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> id; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setId<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN> id<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> this.<SPAN style="COLOR: #000000">id</SPAN> = id; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> getName<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> name; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setName<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN> name<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> this.<SPAN style="COLOR: #000000">name</SPAN> = name; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> char getSex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> sex; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setSex<SPAN style="COLOR: #000000">(</SPAN>char sex<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> this.<SPAN style="COLOR: #000000">sex</SPAN> = sex; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> float getWeight<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> weight; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setWeight<SPAN style="COLOR: #000000">(</SPAN>float weight<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> this.<SPAN style="COLOR: #000000">weight</SPAN> = weight; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>Blob</SPAN> getImage<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> image; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setImage<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>Blob</SPAN> image<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> this.<SPAN style="COLOR: #000000">image</SPAN> = image;<SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>这是Cat.hbm.xml <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>&lt;?xml version="<SPAN style="COLOR: #000000" ?>1</SPAN>.<SPAN style="COLOR: #000000" ?>0</SPAN>"?&gt; <BR>&lt;!DOCTYPE hibernate-mapping SYSTEM "http:<SPAN style="COLOR: #6666ff">//hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"&gt;</SPAN> <BR><BR>&lt;hibernate-mapping&gt; <BR>&nbsp; &nbsp; &lt;<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> name="com.<SPAN style="COLOR: #000000">fankai</SPAN>.<SPAN style="COLOR: #000000">Cat</SPAN>" table="cat"&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;!--jcs-cache usage="read-only"/--&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;id name="id" unsaved-value="<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>null</SPAN>"&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;generator <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN>="uuid.<SPAN style="COLOR: #000000">hex</SPAN>"/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/id&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="name" length="<SPAN style="COLOR: #000000" ?>16</SPAN>" not-<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>null</SPAN>="<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>"/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="sex" length="<SPAN style="COLOR: #000000" ?>1</SPAN>" not-<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>null</SPAN>="<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>"/&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="weight" /&gt; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="image" /&gt; <BR>&nbsp; &nbsp; &lt;/<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN>&gt; <BR>&lt;/hibernate-mapping&gt;</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR><BR>下面是完整的用Hibernate写入Blob的例子，相比JDBC，已经简单轻松多了，也不用写那些Oracle特殊的sql了： <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>package</SPAN> com.<SPAN style="COLOR: #000000">fankai</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">sql</SPAN>.<SPAN style="COLOR: #000000">Blob</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.*; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> oracle.<SPAN style="COLOR: #000000">sql</SPAN>.*; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">io</SPAN>.*; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> TestCatHibernate <SPAN style="COLOR: #000000">{</SPAN>&nbsp; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>static</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> testBlob<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; Session s = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>null</SPAN>;&nbsp; &nbsp; <BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>byte</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> buffer = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>byte</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000" ?>1</SPAN><SPAN style="COLOR: #000000">]</SPAN>; <BR>&nbsp; &nbsp; buffer<SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">]</SPAN> = <SPAN style="COLOR: #000000" ?>1</SPAN>; <BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>try</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; SessionFactory sf = HibernateSessionFactory.<SPAN style="COLOR: #000000">getSessionFactory</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; s = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;&nbsp; <BR>&nbsp; &nbsp; &nbsp; Transaction tx = s.<SPAN style="COLOR: #000000">beginTransaction</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; Cat c = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> Cat<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; c.<SPAN style="COLOR: #000000">setName</SPAN><SPAN style="COLOR: #000000">(</SPAN>"Robbin"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; c.<SPAN style="COLOR: #000000">setImage</SPAN><SPAN style="COLOR: #000000">(</SPAN>Hibernate.<SPAN style="COLOR: #000000">createBlob</SPAN><SPAN style="COLOR: #000000">(</SPAN>buffer<SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; s.<SPAN style="COLOR: #000000">save</SPAN><SPAN style="COLOR: #000000">(</SPAN>c<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; s.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; s.<SPAN style="COLOR: #000000">refresh</SPAN><SPAN style="COLOR: #000000">(</SPAN>c, LockMode.<SPAN style="COLOR: #000000">UPGRADE</SPAN><SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; BLOB blob = <SPAN style="COLOR: #000000">(</SPAN>BLOB<SPAN style="COLOR: #000000">)</SPAN> c.<SPAN style="COLOR: #000000">getImage</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>OutputStream</SPAN> out = blob.<SPAN style="COLOR: #000000">getBinaryOutputStream</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>String</SPAN> fileName = "oraclejdbc.<SPAN style="COLOR: #000000">jar</SPAN>"; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>File</SPAN> f = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="COLOR: #aaaadd" ?>File</SPAN><SPAN style="COLOR: #000000">(</SPAN>fileName<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>FileInputStream</SPAN> fin = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="COLOR: #aaaadd" ?>FileInputStream</SPAN><SPAN style="COLOR: #000000">(</SPAN>f<SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> count = -<SPAN style="COLOR: #000000" ?>1</SPAN>, total = <SPAN style="COLOR: #000000" ?>0</SPAN>; <BR>&nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>byte</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> data = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>byte</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN><SPAN style="COLOR: #000000">)</SPAN>fin.<SPAN style="COLOR: #000000">available</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">]</SPAN>; <BR>&nbsp; &nbsp; &nbsp; fin.<SPAN style="COLOR: #000000">read</SPAN><SPAN style="COLOR: #000000">(</SPAN>data<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; out.<SPAN style="COLOR: #000000">write</SPAN><SPAN style="COLOR: #000000">(</SPAN>data<SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; fin.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; out.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; s.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; tx.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>catch</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>Exception</SPAN> e<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>System</SPAN>.<SPAN style="COLOR: #000000">out</SPAN>.<SPAN style="COLOR: #000000">println</SPAN><SPAN style="COLOR: #000000">(</SPAN>e.<SPAN style="COLOR: #000000">getMessage</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>finally</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>s != <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>null</SPAN><SPAN style="COLOR: #000000">)</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>try</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>catch</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>Exception</SPAN> e<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN>&nbsp; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN></DIV></TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/TrampEagle/aggbug/30085.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-10 09:05 <a href="http://www.blogjava.net/TrampEagle/articles/30085.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用Hibernate3的DetachedCriteria实现分页查询 </title><link>http://www.blogjava.net/TrampEagle/articles/30084.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Fri, 10 Feb 2006 00:51:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/30084.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/30084.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/30084.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/30084.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/30084.html</trackback:ping><description><![CDATA[原文引自：<A href="http://forum.javaeye.com/viewtopic.php?t=14657">http://forum.javaeye.com/viewtopic.php?t=14657</A><BR><BR><SPAN class=postbody>Hibernate3提供了DetachedCriteria，使得我们可以在Web层构造detachedCriteria，然后调用业务层Bean，进行动态条件查询，根据这一功能，我设计了通用的抽象Bean基类和分页类支持，代码来自于Quake Wang的javaeye-core包的相应类，然后又做了很多修改。 <BR><BR>分页支持类： <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>package</SPAN> com.<SPAN style="COLOR: #000000">javaeye</SPAN>.<SPAN style="COLOR: #000000">common</SPAN>.<SPAN style="COLOR: #000000">util</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">util</SPAN>.<SPAN style="COLOR: #000000">List</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> PaginationSupport <SPAN style="COLOR: #000000">{</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>static</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> PAGESIZE = <SPAN style="COLOR: #000000" ?>30</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> pageSize = PAGESIZE; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> items; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> totalCount; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> indexes = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">]</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> startIndex = <SPAN style="COLOR: #000000" ?>0</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> PaginationSupport<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>List</SPAN> items, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> totalCount<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setPageSize<SPAN style="COLOR: #000000">(</SPAN>PAGESIZE<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTotalCount<SPAN style="COLOR: #000000">(</SPAN>totalCount<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setItems<SPAN style="COLOR: #000000">(</SPAN>items<SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setStartIndex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> PaginationSupport<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>List</SPAN> items, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> totalCount, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> startIndex<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setPageSize<SPAN style="COLOR: #000000">(</SPAN>PAGESIZE<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTotalCount<SPAN style="COLOR: #000000">(</SPAN>totalCount<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setItems<SPAN style="COLOR: #000000">(</SPAN>items<SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setStartIndex<SPAN style="COLOR: #000000">(</SPAN>startIndex<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> PaginationSupport<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>List</SPAN> items, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> totalCount, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> pageSize, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> startIndex<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setPageSize<SPAN style="COLOR: #000000">(</SPAN>pageSize<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTotalCount<SPAN style="COLOR: #000000">(</SPAN>totalCount<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setItems<SPAN style="COLOR: #000000">(</SPAN>items<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setStartIndex<SPAN style="COLOR: #000000">(</SPAN>startIndex<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> getItems<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> items; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setItems<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>List</SPAN> items<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">items</SPAN> = items; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> getPageSize<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> pageSize; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setPageSize<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> pageSize<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">pageSize</SPAN> = pageSize; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> getTotalCount<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> totalCount; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setTotalCount<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> totalCount<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>totalCount &gt; <SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">totalCount</SPAN> = totalCount; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> count = totalCount / pageSize; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>totalCount % pageSize &gt; <SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">)</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexes = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN><SPAN style="COLOR: #000000">[</SPAN>count<SPAN style="COLOR: #000000">]</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>for</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> i = <SPAN style="COLOR: #000000" ?>0</SPAN>; i &lt; count; i++<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexes<SPAN style="COLOR: #000000">[</SPAN>i<SPAN style="COLOR: #000000">]</SPAN> = pageSize * i; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>else</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">totalCount</SPAN> = <SPAN style="COLOR: #000000" ?>0</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> getIndexes<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> indexes; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setIndexes<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> indexes<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">indexes</SPAN> = indexes; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> getStartIndex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> startIndex; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setStartIndex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> startIndex<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>totalCount &lt;= <SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">)</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">startIndex</SPAN> = <SPAN style="COLOR: #000000" ?>0</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>else</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>startIndex &gt;= totalCount<SPAN style="COLOR: #000000">)</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">startIndex</SPAN> = indexes<SPAN style="COLOR: #000000">[</SPAN>indexes.<SPAN style="COLOR: #000000">length</SPAN> - <SPAN style="COLOR: #000000" ?>1</SPAN><SPAN style="COLOR: #000000">]</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>else</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>startIndex &lt; <SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">)</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">startIndex</SPAN> = <SPAN style="COLOR: #000000" ?>0</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>else</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">startIndex</SPAN> = indexes<SPAN style="COLOR: #000000">[</SPAN>startIndex / pageSize<SPAN style="COLOR: #000000">]</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> getNextIndex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> nextIndex = getStartIndex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> + pageSize; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>nextIndex &gt;= totalCount<SPAN style="COLOR: #000000">)</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getStartIndex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>else</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> nextIndex; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> getPreviousIndex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> previousIndex = getStartIndex<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> - pageSize; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>previousIndex &lt; <SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">)</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> <SPAN style="COLOR: #000000" ?>0</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>else</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> previousIndex; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>抽象业务类 <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR><SPAN style="COLOR: #6666ff">/** <BR>* Created on 2005-7-12 <BR>*/</SPAN> <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>package</SPAN> com.<SPAN style="COLOR: #000000">javaeye</SPAN>.<SPAN style="COLOR: #000000">common</SPAN>.<SPAN style="COLOR: #000000">business</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">io</SPAN>.<SPAN style="COLOR: #000000">Serializable</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">util</SPAN>.<SPAN style="COLOR: #000000">List</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> org.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">Criteria</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> org.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">HibernateException</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> org.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">Session</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> org.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">criterion</SPAN>.<SPAN style="COLOR: #000000">DetachedCriteria</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> org.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">criterion</SPAN>.<SPAN style="COLOR: #000000">Projections</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> org.<SPAN style="COLOR: #000000">springframework</SPAN>.<SPAN style="COLOR: #000000">orm</SPAN>.<SPAN style="COLOR: #000000">hibernate3</SPAN>.<SPAN style="COLOR: #000000">HibernateCallback</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> org.<SPAN style="COLOR: #000000">springframework</SPAN>.<SPAN style="COLOR: #000000">orm</SPAN>.<SPAN style="COLOR: #000000">hibernate3</SPAN>.<SPAN style="COLOR: #000000">support</SPAN>.<SPAN style="COLOR: #000000">HibernateDaoSupport</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> com.<SPAN style="COLOR: #000000">javaeye</SPAN>.<SPAN style="COLOR: #000000">common</SPAN>.<SPAN style="COLOR: #000000">util</SPAN>.<SPAN style="COLOR: #000000">PaginationSupport</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>abstract</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> AbstractManager <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>extends</SPAN> HibernateDaoSupport <SPAN style="COLOR: #000000">{</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>boolean</SPAN> cacheQueries = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>false</SPAN>; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> queryCacheRegion; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setCacheQueries<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>boolean</SPAN> cacheQueries<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">cacheQueries</SPAN> = cacheQueries; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setQueryCacheRegion<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN> queryCacheRegion<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<SPAN style="COLOR: #000000">queryCacheRegion</SPAN> = queryCacheRegion; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> save<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> entity<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">save</SPAN><SPAN style="COLOR: #000000">(</SPAN>entity<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> persist<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> entity<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">save</SPAN><SPAN style="COLOR: #000000">(</SPAN>entity<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> update<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> entity<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">update</SPAN><SPAN style="COLOR: #000000">(</SPAN>entity<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> delete<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> entity<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">delete</SPAN><SPAN style="COLOR: #000000">(</SPAN>entity<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> load<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>Class</SPAN> entity, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Serializable</SPAN> id<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">load</SPAN><SPAN style="COLOR: #000000">(</SPAN>entity, id<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> get<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>Class</SPAN> entity, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Serializable</SPAN> id<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">get</SPAN><SPAN style="COLOR: #000000">(</SPAN>entity, id<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> findAll<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>Class</SPAN> entity<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">find</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">"from "</SPAN> + entity.<SPAN style="COLOR: #000000">getName</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> findByNamedQuery<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> namedQuery<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">findByNamedQuery</SPAN><SPAN style="COLOR: #000000">(</SPAN>namedQuery<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> findByNamedQuery<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> query, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> parameter<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">findByNamedQuery</SPAN><SPAN style="COLOR: #000000">(</SPAN>query, parameter<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> findByNamedQuery<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> query, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> parameters<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">findByNamedQuery</SPAN><SPAN style="COLOR: #000000">(</SPAN>query, parameters<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> find<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> query<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">find</SPAN><SPAN style="COLOR: #000000">(</SPAN>query<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> find<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> query, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> parameter<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">find</SPAN><SPAN style="COLOR: #000000">(</SPAN>query, parameter<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> PaginationSupport findPageByCriteria<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> DetachedCriteria detachedCriteria<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> findPageByCriteria<SPAN style="COLOR: #000000">(</SPAN>detachedCriteria, PaginationSupport.<SPAN style="COLOR: #000000">PAGESIZE</SPAN>, <SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> PaginationSupport findPageByCriteria<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> DetachedCriteria detachedCriteria, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> startIndex<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> findPageByCriteria<SPAN style="COLOR: #000000">(</SPAN>detachedCriteria, PaginationSupport.<SPAN style="COLOR: #000000">PAGESIZE</SPAN>, startIndex<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> PaginationSupport findPageByCriteria<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> DetachedCriteria detachedCriteria, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> pageSize, <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> startIndex<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> <SPAN style="COLOR: #000000">(</SPAN>PaginationSupport<SPAN style="COLOR: #000000">)</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">execute</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> HibernateCallback<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> doInHibernate<SPAN style="COLOR: #000000">(</SPAN>Session session<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>throws</SPAN> HibernateException <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Criteria criteria = detachedCriteria.<SPAN style="COLOR: #000000">getExecutableCriteria</SPAN><SPAN style="COLOR: #000000">(</SPAN>session<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> totalCount = <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>Integer</SPAN><SPAN style="COLOR: #000000">)</SPAN> criteria.<SPAN style="COLOR: #000000">setProjection</SPAN><SPAN style="COLOR: #000000">(</SPAN>Projections.<SPAN style="COLOR: #000000">rowCount</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">uniqueResult</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">intValue</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; criteria.<SPAN style="COLOR: #000000">setProjection</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>null</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>List</SPAN> items = criteria.<SPAN style="COLOR: #000000">setFirstResult</SPAN><SPAN style="COLOR: #000000">(</SPAN>startIndex<SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">setMaxResults</SPAN><SPAN style="COLOR: #000000">(</SPAN>pageSize<SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">list</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PaginationSupport ps = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> PaginationSupport<SPAN style="COLOR: #000000">(</SPAN>items, totalCount, pageSize, startIndex<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> ps; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN>, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>List</SPAN> findAllByCriteria<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> DetachedCriteria detachedCriteria<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>List</SPAN><SPAN style="COLOR: #000000">)</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">execute</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> HibernateCallback<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> doInHibernate<SPAN style="COLOR: #000000">(</SPAN>Session session<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>throws</SPAN> HibernateException <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Criteria criteria = detachedCriteria.<SPAN style="COLOR: #000000">getExecutableCriteria</SPAN><SPAN style="COLOR: #000000">(</SPAN>session<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> criteria.<SPAN style="COLOR: #000000">list</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN>, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> getCountByCriteria<SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>final</SPAN> DetachedCriteria detachedCriteria<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #aaaadd" ?>Integer</SPAN> count = <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>Integer</SPAN><SPAN style="COLOR: #000000">)</SPAN> getHibernateTemplate<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">execute</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> HibernateCallback<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>Object</SPAN> doInHibernate<SPAN style="COLOR: #000000">(</SPAN>Session session<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>throws</SPAN> HibernateException <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Criteria criteria = detachedCriteria.<SPAN style="COLOR: #000000">getExecutableCriteria</SPAN><SPAN style="COLOR: #000000">(</SPAN>session<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> criteria.<SPAN style="COLOR: #000000">setProjection</SPAN><SPAN style="COLOR: #000000">(</SPAN>Projections.<SPAN style="COLOR: #000000">rowCount</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">uniqueResult</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN>, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> count.<SPAN style="COLOR: #000000">intValue</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN> <BR></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR><BR>用户在web层构造查询条件detachedCriteria，和可选的startIndex，调用业务bean的相应findByCriteria方法，返回一个PaginationSupport的实例ps。 <BR><BR>ps.getItems()得到已分页好的结果集 <BR>ps.getIndexes()得到分页索引的数组 <BR>ps.getTotalCount()得到总结果数 <BR>ps.getStartIndex()当前分页索引 <BR>ps.getNextIndex()下一页索引 <BR>ps.getPreviousIndex()上一页索引</SPAN><img src ="http://www.blogjava.net/TrampEagle/aggbug/30084.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-10 08:51 <a href="http://www.blogjava.net/TrampEagle/articles/30084.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate源码分析---数据库连接池</title><link>http://www.blogjava.net/TrampEagle/articles/30081.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Fri, 10 Feb 2006 00:29:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/30081.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/30081.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/30081.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/30081.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/30081.html</trackback:ping><description><![CDATA[<P>原文引自：<A href="http://www.matrix.org.cn/thread.shtml?topicId=33623&amp;forumId=23">http://www.matrix.org.cn/thread.shtml?topicId=33623&amp;forumId=23</A><BR><BR>1：net.sf.hibernate.connection<BR>此包中的类为提供数据源连接池的类，其中用到了C3P0，DBCP，DriverManager等第三方连接池插件；<BR>其中ConnectionProvider为一致对外接口；所有的功能类都实现的他的接口；<BR>/**<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * Initialize the connection provider from given properties.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * @param props &lt;tt&gt;SessionFactory&lt;/tt&gt; properties<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>public void configure(Properties props) throws HibernateException;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/**<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * Grab a connection<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * @return a JDBC connection<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * @throws SQLException<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public Connection getConnection() throws SQLException;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/**<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * Dispose of a used connection.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * @param conn a JDBC connection<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * @throws SQLException<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void closeConnection(Connection conn) throws SQLException;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/**<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * Release all resources held by this provider. JavaDoc requires a second sentence.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * @throws HibernateException<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void close() throws HibernateException;<BR><BR><BR>源码中的注释把方法功能描述的很具体，我再次就不再加介绍了，现在具体的拿出两个实现类来具体看一下他的功能；<BR>1.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C3P0ConnectionProvider implements ConnectionProvider<BR>public void configure(Properties props) throws HibernateException {<BR>//获取数据库的驱程和URL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String jdbcDriverClass = props.getProperty(Environment.DRIVER);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String jdbcUrl = props.getProperty(Environment.URL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//对第三方插件所需要的属性进行定制包装；<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(props);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info("C3P0 using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info("Connection properties: " + connectionProps);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//加载数据库驱动程序<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (jdbcDriverClass==null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class.forName(jdbcDriverClass);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (ClassNotFoundException cnfe) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String msg = "JDBC Driver class not found: " + jdbcDriverClass;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.fatal(msg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new HibernateException(msg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//加载配置文件中的properties;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int minPoolSize = PropertiesHelper.getInt(Environment.C3P0_MIN_SIZE, props, 1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int maxPoolSize = PropertiesHelper.getInt(Environment.C3P0_MAX_SIZE, props, 100);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int maxIdleTime = PropertiesHelper.getInt(Environment.C3P0_TIMEOUT, props, 0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int maxStatements = PropertiesHelper.getInt(Environment.C3P0_MAX_STATEMENTS, props, 0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int acquireIncrement = PropertiesHelper.getInt(Environment.C3P0_ACQUIRE_INCREMENT, props, 1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int idleTestPeriod = PropertiesHelper.getInt(Environment.C3P0_IDLE_TEST_PERIOD, props, 0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean validateConnection = PropertiesHelper.getBoolean(Environment.C3P0_VALIDATE_CONNECTION, props);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//有C3P0的连接池接口来设置第三方插件所需要的属性<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;此处为与第三方插件的结合点；<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PoolConfig pcfg = new PoolConfig();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcfg.setInitialPoolSize(minPoolSize);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcfg.setMinPoolSize(minPoolSize);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcfg.setMaxPoolSize(maxPoolSize);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcfg.setAcquireIncrement(acquireIncrement);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcfg.setMaxIdleTime(maxIdleTime);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcfg.setMaxStatements(maxStatements);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcfg.setTestConnectionOnCheckout(validateConnection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pcfg.setIdleConnectionTestPeriod(idleTestPeriod);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //用第三方插件来初始化连接池，并向程序返回了JDBC的DataSource接口，使得任何第三方插件的上层程序都不必去引入相应具体的第三方的类库，实现的低耦合；<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*DataSource unpooled = DataSources.unpooledDataSource(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jdbcUrl, props.getProperty(Environment.USER), props.getProperty(Environment.PASS)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataSource unpooled = DataSources.unpooledDataSource(jdbcUrl, connectionProps);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ds = DataSources.pooledDataSource(unpooled, pcfg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.fatal("could not instantiate C3P0 connection pool", e);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new HibernateException("Could not instantiate C3P0 connection pool", e);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String i = props.getProperty(Environment.ISOLATION);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (i==null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isolation=null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isolation = new Integer(i);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>public Connection getConnection() throws SQLException {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Connection c = ds.getConnection();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//设置事务的隔离级别；<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (isolation!=null) c.setTransactionIsolation( isolation.intValue() );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( c.getAutoCommit() ) c.setAutoCommit(false);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return c;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>public void closeConnection(Connection conn) throws SQLException {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>public void close() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataSources.destroy(ds);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (SQLException sqle) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.warn("could not destroy C3P0 connection pool", sqle);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>1.2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class DriverManagerConnectionProvider implements ConnectionProvider<BR><BR><BR>public void configure(Properties props) throws HibernateException {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String driverClass = props.getProperty(Environment.DRIVER);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info("Using Hibernate built-in connection pool (not for production use!)");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info("Hibernate connection pool size: " + poolSize);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (isolation!=null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (driverClass==null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// trying via forName() first to be as close to DriverManager's semantics<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class.forName(driverClass);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (ClassNotFoundException cnfe) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReflectHelper.classForName(driverClass);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (ClassNotFoundException e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String msg = "JDBC Driver class not found: " + driverClass;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.fatal(msg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new HibernateException(msg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url = props.getProperty(Environment.URL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (url==null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String msg = "JDBC URL was not specified by property " + Environment.URL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.fatal(msg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new HibernateException(msg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connectionProps = ConnectionProviderFactory.getConnectionProperties(props);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info( "using driver: " + driverClass + " at URL: " + url );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info("connection properties: " + connectionProps);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public Connection getConnection() throws SQLException {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( log.isTraceEnabled() ) log.trace( "total checked-out connections: " + checkedOut );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//锁死连接池，只需一个线程访问，池中有连接就提供，没有在新建一个；<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;synchronized (pool) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( !pool.isEmpty() ) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int last = pool.size() - 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( log.isTraceEnabled() ) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.trace("using pooled JDBC connection, pool size: " + last);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;checkedOut++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Connection pooled = (Connection) pool.remove(last);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (isolation!=null) pooled.setTransactionIsolation( isolation.intValue() );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( pooled.getAutoCommit() ) pooled.setAutoCommit(false);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return pooled;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.debug("opening new JDBC connection");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Connection conn = DriverManager.getConnection(url, connectionProps);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (isolation!=null) conn.setTransactionIsolation( isolation.intValue() );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( conn.getAutoCommit() ) conn.setAutoCommit(false);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( log.isDebugEnabled() ) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.debug( "created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation() );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( log.isTraceEnabled() ) checkedOut++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return conn;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>public void closeConnection(Connection conn) throws SQLException {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( log.isDebugEnabled() ) checkedOut--;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//如果池中连接已满就释放连接，否则就加到连接池中；<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;synchronized (pool) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int currentSize = pool.size();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( currentSize &lt; poolSize ) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( log.isTraceEnabled() ) log.trace("returning connection to pool, pool size: " + (currentSize + 1) );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool.add(conn);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.debug("closing JDBC connection");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (SQLException sqle) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JDBCExceptionReporter.logExceptions(sqle);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw sqle;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>public void close() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//清空连接池；<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info("cleaning up connection pool: " + url);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Iterator iter = pool.iterator();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ( iter.hasNext() ) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( (Connection) iter.next() ).close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (SQLException sqle) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.warn("problem closing pooled connection", sqle);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool.clear();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><!-- the post be hidden --></P><img src ="http://www.blogjava.net/TrampEagle/aggbug/30081.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-10 08:29 <a href="http://www.blogjava.net/TrampEagle/articles/30081.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate也可以自动建表</title><link>http://www.blogjava.net/TrampEagle/articles/29972.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Wed, 08 Feb 2006 14:10:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/29972.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/29972.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/29972.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/29972.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/29972.html</trackback:ping><description><![CDATA[<SPAN class=postbody>摘自：<A href="http://forum.javaeye.com/viewtopic.php?t=176">http://forum.javaeye.com/viewtopic.php?t=176</A><BR><BR>test.java <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>package</SPAN> hibernatedemo; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> hibernatedemo.<SPAN style="COLOR: #000000">person</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> java.<SPAN style="COLOR: #000000">util</SPAN>.*; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">Query</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">Session</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">SessionFactory</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN>; <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>import</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">tool</SPAN>.<SPAN style="COLOR: #000000">hbm2ddl</SPAN>.<SPAN style="COLOR: #000000">SchemaExport</SPAN>; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> Test <SPAN style="COLOR: #000000">{</SPAN> <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>static</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> main<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">]</SPAN> args<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>throws</SPAN> <SPAN style="COLOR: #aaaadd" ?>Exception</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp;Configuration cfg = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> Configuration<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">addClass</SPAN><SPAN style="COLOR: #000000">(</SPAN>person.<SPAN style="COLOR: #000000">class</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp;SessionFactory sessions = cfg.<SPAN style="COLOR: #000000">buildSessionFactory</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp;<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> SchemaExport<SPAN style="COLOR: #000000">(</SPAN>cfg<SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">create</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp;Session s = sessions.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp;Query q = s.<SPAN style="COLOR: #000000">createQuery</SPAN><SPAN style="COLOR: #000000">(</SPAN>"from person"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp;<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>for</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>Iterator</SPAN> it = q.<SPAN style="COLOR: #000000">iterate</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;it.<SPAN style="COLOR: #000000">hasNext</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;<SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp;person b = <SPAN style="COLOR: #000000">(</SPAN>person<SPAN style="COLOR: #000000">)</SPAN>it.<SPAN style="COLOR: #000000">next</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp;<SPAN style="COLOR: #aaaadd" ?>System</SPAN>.<SPAN style="COLOR: #000000">out</SPAN>.<SPAN style="COLOR: #000000">println</SPAN><SPAN style="COLOR: #000000">(</SPAN>"##name:"+b.<SPAN style="COLOR: #000000">getName</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp;<SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN> <BR></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>person.java <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>package</SPAN> hibernatedemo; <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> person <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> name; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> address; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>private</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> id; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> person<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">{</SPAN> <BR><BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> getId<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> id; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setId<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN> id<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; this.<SPAN style="COLOR: #000000">id</SPAN> = id; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> getName<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> name; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setName<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN> name<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; this.<SPAN style="COLOR: #000000">name</SPAN> = name; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> getAddress<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> address; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> setAddress<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN> address<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; this.<SPAN style="COLOR: #000000">address</SPAN> = address; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>person.hbm.xml <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>&lt;?xml version="<SPAN style="COLOR: #000000" ?>1</SPAN>.<SPAN style="COLOR: #000000" ?>0</SPAN>"?&gt; <BR><BR>&lt;!DOCTYPE hibernate-mapping <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>PUBLIC</SPAN> <BR>&nbsp; &nbsp; "-<SPAN style="COLOR: #6666ff">//Hibernate/Hibernate Mapping DTD 2.0//EN" </SPAN><BR>&nbsp; &nbsp; "http:<SPAN style="COLOR: #6666ff">//hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"&gt;</SPAN> <BR><BR>&lt;hibernate-mapping&gt; <BR>&nbsp; &lt;<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN> name="hibernatedemo.<SPAN style="COLOR: #000000">person</SPAN>" &gt; <BR>&nbsp; &nbsp; &lt;id&nbsp; name="id" <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;column="id" <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type="java.<SPAN style="COLOR: #000000">lang</SPAN>.<SPAN style="COLOR: #000000">String</SPAN>"&gt; <BR>&nbsp; &nbsp; &nbsp; &lt;generator <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN>="assigned"/&gt;&nbsp; &nbsp; <BR>&nbsp; &nbsp; &lt;/id&gt; <BR>&nbsp; &nbsp; &lt;property <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name="name" <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type="java.<SPAN style="COLOR: #000000">lang</SPAN>.<SPAN style="COLOR: #000000">String</SPAN>" <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;column="name"/&gt; <BR>&nbsp; &nbsp; &lt;property <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name="address" <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type="java.<SPAN style="COLOR: #000000">lang</SPAN>.<SPAN style="COLOR: #000000">String</SPAN>" <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;column="pass"/&gt; <BR>&nbsp; &lt;/<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN>&gt;&nbsp; &nbsp; &nbsp; &nbsp; <BR>&lt;/hibernate-mapping&gt; <BR></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>数据库是mysql，服务器tomcat <BR>执行的时候控制台信息 <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>12</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Environment</SPAN> &lt;clinit&gt; <BR><BR>信息: Hibernate <SPAN style="COLOR: #000000" ?>2</SPAN>.<SPAN style="COLOR: #000000" ?>1</SPAN> beta <SPAN style="COLOR: #000000" ?>3</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>12</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Environment</SPAN> &lt;clinit&gt; <BR><BR>信息: loaded properties from resource hibernate.<SPAN style="COLOR: #000000">properties</SPAN>: <SPAN style="COLOR: #000000">{</SPAN>hibernate.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">driver_class</SPAN>=com.<SPAN style="COLOR: #000000">mysql</SPAN>.<SPAN style="COLOR: #000000">jdbc</SPAN>.<SPAN style="COLOR: #000000">Driver</SPAN>, hibernate.<SPAN style="COLOR: #000000">cglib</SPAN>.<SPAN style="COLOR: #000000">use_reflection_optimizer</SPAN>=<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>, hibernate.<SPAN style="COLOR: #000000">dialect</SPAN>=net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">dialect</SPAN>.<SPAN style="COLOR: #000000">MySQLDialect</SPAN>, hibernate.<SPAN style="COLOR: #000000">jdbc</SPAN>.<SPAN style="COLOR: #000000">use_streams_for_binary</SPAN>=<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>, hibernate.<SPAN style="COLOR: #000000">jdbc</SPAN>.<SPAN style="COLOR: #000000">batch_size</SPAN>=<SPAN style="COLOR: #000000" ?>0</SPAN>, hibernate.<SPAN style="COLOR: #000000">query</SPAN>.<SPAN style="COLOR: #000000">substitutions</SPAN>=<SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN> <SPAN style="COLOR: #000000" ?>1</SPAN>, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>false</SPAN> <SPAN style="COLOR: #000000" ?>0</SPAN>, yes <SPAN style="COLOR: #0000ff">'Y'</SPAN>, no <SPAN style="COLOR: #0000ff">'N'</SPAN>, hibernate.<SPAN style="COLOR: #000000">query</SPAN>.<SPAN style="COLOR: #000000">imports</SPAN>=net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">test</SPAN>, net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">eg</SPAN>, hibernate.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">username</SPAN>=root, hibernate.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">url</SPAN>=jdbc:mysql:<SPAN style="COLOR: #6666ff">//localhost/hibernatedb, hibernate.connection.password=, hibernate.statement_cache.size=25, hibernate.connection.pool_size=1}</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>12</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Environment</SPAN> &lt;clinit&gt; <BR><BR>信息: using java.<SPAN style="COLOR: #000000">io</SPAN> streams to persist binary types <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>12</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Environment</SPAN> &lt;clinit&gt; <BR><BR>信息: using CGLIB reflection optimizer <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>12</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> addClass <BR><BR>信息: Mapping resource: hibernatedemo/person.<SPAN style="COLOR: #000000">hbm</SPAN>.<SPAN style="COLOR: #000000">xml</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Binder</SPAN> bindRootClass <BR><BR>信息: Mapping <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>class</SPAN>: hibernatedemo.<SPAN style="COLOR: #000000">person</SPAN> -&gt; person <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing one-to-many association mappings <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing one-to-one association property references <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing foreign key constraints <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">dialect</SPAN>.<SPAN style="COLOR: #000000">Dialect</SPAN> &lt;init&gt; <BR><BR>信息: Using dialect: net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">dialect</SPAN>.<SPAN style="COLOR: #000000">MySQLDialect</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">SettingsFactory</SPAN> buildSettings <BR><BR>信息: Use outer join fetching: <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> configure <BR><BR>信息: Using Hibernate built-in connection pool <SPAN style="COLOR: #000000">(</SPAN>not <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>for</SPAN> production use!<SPAN style="COLOR: #000000">)</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> configure <BR><BR>信息: Hibernate connection pool size: <SPAN style="COLOR: #000000" ?>1</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> configure <BR><BR>信息: using driver: com.<SPAN style="COLOR: #000000">mysql</SPAN>.<SPAN style="COLOR: #000000">jdbc</SPAN>.<SPAN style="COLOR: #000000">Driver</SPAN> at <SPAN style="COLOR: #aaaadd" ?>URL</SPAN>: jdbc:mysql:<SPAN style="COLOR: #6666ff">//localhost/hibernatedb</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> configure <BR><BR>信息: connection properties: <SPAN style="COLOR: #000000">{</SPAN>user=root, password=<SPAN style="COLOR: #000000">}</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>15</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">transaction</SPAN>.<SPAN style="COLOR: #000000">TransactionManagerLookupFactory</SPAN> getTransactionManagerLookup <BR><BR>信息: No TransactionManagerLookup configured <SPAN style="COLOR: #000000">(</SPAN>use of process level read-write cache is not recommended<SPAN style="COLOR: #000000">)</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>16</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">SettingsFactory</SPAN> buildSettings <BR><BR>信息: Use scrollable result sets: <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>16</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">SettingsFactory</SPAN> buildSettings <BR><BR>信息: Query language substitutions: <SPAN style="COLOR: #000000">{</SPAN>no=<SPAN style="COLOR: #0000ff">'N'</SPAN>, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>=<SPAN style="COLOR: #000000" ?>1</SPAN>, yes=<SPAN style="COLOR: #0000ff">'Y'</SPAN>, <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>false</SPAN>=<SPAN style="COLOR: #000000" ?>0</SPAN><SPAN style="COLOR: #000000">}</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>16</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">SettingsFactory</SPAN> buildSettings <BR><BR>信息: cache provider: net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cache</SPAN>.<SPAN style="COLOR: #000000">JCSCacheProvider</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>16</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> configureCaches <BR><BR>信息: instantiating and configuring caches <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>16</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">impl</SPAN>.<SPAN style="COLOR: #000000">SessionFactoryImpl</SPAN> &lt;init&gt; <BR><BR>信息: building session factory <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">impl</SPAN>.<SPAN style="COLOR: #000000">SessionFactoryObjectFactory</SPAN> addInstance <BR><BR>信息: no JNDI name configured <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">dialect</SPAN>.<SPAN style="COLOR: #000000">Dialect</SPAN> &lt;init&gt; <BR><BR>信息: Using dialect: net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">dialect</SPAN>.<SPAN style="COLOR: #000000">MySQLDialect</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing one-to-many association mappings <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing one-to-one association property references <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing foreign key constraints <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing one-to-many association mappings <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing one-to-one association property references <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">cfg</SPAN>.<SPAN style="COLOR: #000000">Configuration</SPAN> secondPassCompile <BR><BR>信息: processing foreign key constraints <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">tool</SPAN>.<SPAN style="COLOR: #000000">hbm2ddl</SPAN>.<SPAN style="COLOR: #000000">SchemaExport</SPAN> execute <BR><BR>信息: Running hbm2ddl schema export <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">tool</SPAN>.<SPAN style="COLOR: #000000">hbm2ddl</SPAN>.<SPAN style="COLOR: #000000">SchemaExport</SPAN> execute <BR><BR>信息: exporting generated schema to database <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> configure <BR><BR>信息: Using Hibernate built-in connection pool <SPAN style="COLOR: #000000">(</SPAN>not <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>for</SPAN> production use!<SPAN style="COLOR: #000000">)</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> configure <BR><BR>信息: Hibernate connection pool size: <SPAN style="COLOR: #000000" ?>1</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> configure <BR><BR>信息: using driver: com.<SPAN style="COLOR: #000000">mysql</SPAN>.<SPAN style="COLOR: #000000">jdbc</SPAN>.<SPAN style="COLOR: #000000">Driver</SPAN> at <SPAN style="COLOR: #aaaadd" ?>URL</SPAN>: jdbc:mysql:<SPAN style="COLOR: #6666ff">//localhost/hibernatedb</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> configure <BR><BR>信息: connection properties: <SPAN style="COLOR: #000000">{</SPAN>user=root, password=<SPAN style="COLOR: #000000">}</SPAN> <BR><BR>drop table person <BR><BR>create table person <SPAN style="COLOR: #000000">(</SPAN> <BR>&nbsp; &nbsp;id VARCHAR<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>255</SPAN><SPAN style="COLOR: #000000">)</SPAN> not <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>null</SPAN>, <BR>&nbsp; &nbsp;name VARCHAR<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>255</SPAN><SPAN style="COLOR: #000000">)</SPAN>, <BR>&nbsp; &nbsp;pass VARCHAR<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>255</SPAN><SPAN style="COLOR: #000000">)</SPAN>, <BR>&nbsp; &nbsp;primary key <SPAN style="COLOR: #000000">(</SPAN>id<SPAN style="COLOR: #000000">)</SPAN> <BR><SPAN style="COLOR: #000000">)</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">tool</SPAN>.<SPAN style="COLOR: #000000">hbm2ddl</SPAN>.<SPAN style="COLOR: #000000">SchemaExport</SPAN> execute <BR><BR>信息: schema export complete <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> close <BR><BR>信息: cleaning up connection pool: jdbc:mysql:<SPAN style="COLOR: #6666ff">//localhost/hibernatedb</SPAN> <BR><BR><SPAN style="COLOR: #000000" ?>2003</SPAN>-<SPAN style="COLOR: #000000" ?>9</SPAN>-<SPAN style="COLOR: #000000" ?>16</SPAN> <SPAN style="COLOR: #000000" ?>10</SPAN>:<SPAN style="COLOR: #000000" ?>36</SPAN>:<SPAN style="COLOR: #000000" ?>18</SPAN> net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">connection</SPAN>.<SPAN style="COLOR: #000000">DriverManagerConnectionProvider</SPAN> close <BR><BR>信息: cleaning up connection pool: jdbc:mysql:<SPAN style="COLOR: #6666ff">//localhost/hibernatedb</SPAN> <BR></DIV></TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/TrampEagle/aggbug/29972.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-08 22:10 <a href="http://www.blogjava.net/TrampEagle/articles/29972.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate入门 - Transaction </title><link>http://www.blogjava.net/TrampEagle/articles/29971.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Wed, 08 Feb 2006 14:08:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/29971.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/29971.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/29971.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/29971.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/29971.html</trackback:ping><description><![CDATA[<SPAN class=postbody>引自：<A href="http://forum.javaeye.com/viewtopic.php?t=264">http://forum.javaeye.com/viewtopic.php?t=264</A><BR><BR>hibernate入门 - Transaction <BR><BR>Hibernate是对JDBC的轻量级对象封装，Hibernate本身是不具备Transaction处理功能的，Hibernate的Transaction实际上是底层的JDBC Transaction的封装，或者是JTA Transaction的封装，下面我们详细的分析： <BR><BR>Hibernate可以配置为JDBCTransaction或者是JTATransaction，这取决于你在hibernate.properties中的配置: <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote>#hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory <BR>#hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>如果你什么都不配置，默认情况下使用JDBCTransaction，如果你配置为： <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote>hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>将使用JTATransaction <BR><BR>不管你准备让Hibernate使用JDBCTransaction，还是JTATransaction，我的忠告就是什么都不配，将让它保持默认状态，如下： <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote>#hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory <BR>#hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>在下面的分析中我会给出原因。 <BR><BR>一、JDBC Transaction <BR><BR>看看使用JDBC Transaction的时候我们的代码例子： <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>Session session = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>Transaction tx = session.<SPAN style="COLOR: #000000">beginTransactioin</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>... <BR><SPAN style="COLOR: #000000">session</SPAN>.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>tx.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>session.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>这是默认的情况，当你在代码中使用Hibernate的Transaction的时候实际上就是JDBCTransaction。那么JDBCTransaction究竟是什么东西呢？来看看源代码就清楚了： <BR><BR>Hibernate2.0.3源代码中的类</SPAN> 
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>net.<SPAN style="COLOR: #000000">sf</SPAN>.<SPAN style="COLOR: #000000">hibernate</SPAN>.<SPAN style="COLOR: #000000">transaction</SPAN>.<SPAN style="COLOR: #000000">JDBCTransaction</SPAN>: <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> begin<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>throws</SPAN> HibernateException <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; log.<SPAN style="COLOR: #000000">debug</SPAN><SPAN style="COLOR: #000000">(</SPAN>"begin"<SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>try</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toggleAutoCommit = session.<SPAN style="COLOR: #000000">connection</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">getAutoCommit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>toggleAutoCommit<SPAN style="COLOR: #000000">)</SPAN> session.<SPAN style="COLOR: #000000">connection</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">setAutoCommit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>false</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>catch</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>SQLException</SPAN> e<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.<SPAN style="COLOR: #000000">error</SPAN><SPAN style="COLOR: #000000">(</SPAN>"Begin failed", e<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> TransactionException<SPAN style="COLOR: #000000">(</SPAN>"Begin failed with SQL exception: ", e<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN>&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; begun = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>这是启动Transaction的方法，看到 connection().setAutoCommit(false) 了吗？是不是很熟悉？ <BR><BR>再来看 <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> commit<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>throws</SPAN> HibernateException <SPAN style="COLOR: #000000">{</SPAN>&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>!begun<SPAN style="COLOR: #000000">)</SPAN> throw <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> TransactionException<SPAN style="COLOR: #000000">(</SPAN>"Transaction not successfully started"<SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; log.<SPAN style="COLOR: #000000">debug</SPAN><SPAN style="COLOR: #000000">(</SPAN>"commit"<SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>try</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN> session.<SPAN style="COLOR: #000000">getFlushMode</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>!=FlushMode.<SPAN style="COLOR: #000000">NEVER</SPAN> <SPAN style="COLOR: #000000">)</SPAN> session.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>try</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.<SPAN style="COLOR: #000000">connection</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; committed = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>catch</SPAN> <SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>SQLException</SPAN> e<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.<SPAN style="COLOR: #000000">error</SPAN><SPAN style="COLOR: #000000">(</SPAN>"Commit failed", e<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> TransactionException<SPAN style="COLOR: #000000">(</SPAN>"Commit failed with SQL exception: ", e<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>finally</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.<SPAN style="COLOR: #000000">afterTransactionCompletion</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; &nbsp; &nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; toggleAutoCommit<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>这是提交方法，看到connection().commit() 了吗？下面就不用我多说了，这个类代码非常简单易懂，通过阅读使我们明白Hibernate的Transaction都在干了些什么？我现在把用Hibernate写的例子翻译成JDBC，大家就一目了然了： <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="COLOR: #aaaadd" ?>Connection</SPAN> conn = ...;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;---&nbsp; &nbsp;session = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>conn.<SPAN style="COLOR: #000000">setAutoCommit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>false</SPAN><SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp;&lt;---&nbsp; &nbsp;tx = session.<SPAN style="COLOR: #000000">beginTransactioin</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;---&nbsp; &nbsp;... <BR><BR><SPAN style="COLOR: #000000">conn</SPAN>.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;---&nbsp; &nbsp;tx.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <SPAN style="COLOR: #000000">(</SPAN>对应左边的两句<SPAN style="COLOR: #000000">)</SPAN> <BR>conn.<SPAN style="COLOR: #000000">setAutoCommit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>conn.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;---&nbsp; &nbsp;session.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>看明白了吧，Hibernate的JDBCTransaction根本就是conn.commit而已，根本毫无神秘可言，只不过在Hibernate中，Session打开的时候，就会自动conn.setAutoCommit(false)，不像一般的JDBC，默认都是true，所以你最后不写commit也没有关系，由于Hibernate已经把AutoCommit给关掉了，所以用Hibernate的时候，你在程序中不写Transaction的话，数据库根本就没有反应。 <BR><BR><BR>二、JTATransaction <BR><BR>如果你在EJB中使用Hibernate，或者准备用JTA来管理跨Session的长事务，那么就需要使用JTATransaction，先看一个例子： <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>javax.<SPAN style="COLOR: #000000">transaction</SPAN>.<SPAN style="COLOR: #000000">UserTransaction</SPAN> tx = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="COLOR: #aaaadd" ?>InitialContext</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">lookup</SPAN><SPAN style="COLOR: #000000">(</SPAN>"javax.<SPAN style="COLOR: #000000">transaction</SPAN>.<SPAN style="COLOR: #000000">UserTransaction</SPAN>"<SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>Session s1 = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>... <BR><SPAN style="COLOR: #000000">s1</SPAN>.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>s1.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>... <BR><BR><SPAN style="COLOR: #000000">Session</SPAN> s2 = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>... <BR><SPAN style="COLOR: #000000">s2</SPAN>.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>s2.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>tx.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>这是标准的使用JTA的代码片断，Transaction是跨Session的，它的生命周期比Session要长。如果你在EJB中使用Hibernate，那么是最简单不过的了，你什么Transaction代码统统都不要写了，直接在EJB的部署描述符上配置某某方法是否使用事务就可以了。 <BR><BR>现在我们来分析一下JTATransaction的源代码， net.sf.hibernate.transaction.JTATransaction: <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> begin<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>InitialContext</SPAN> context, ... <BR>&nbsp; ... <BR>&nbsp; <SPAN style="COLOR: #000000">ut</SPAN> = <SPAN style="COLOR: #000000">(</SPAN>UserTransaction<SPAN style="COLOR: #000000">)</SPAN> context.<SPAN style="COLOR: #000000">lookup</SPAN><SPAN style="COLOR: #000000">(</SPAN>utName<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; ...</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>看清楚了吗？ 和我上面写的代码 tx = new InitialContext().lookup("javax.transaction.UserTransaction"); 是不是完全一样？ <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>void</SPAN> commit<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> ... <BR>&nbsp; ... <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>newTransaction<SPAN style="COLOR: #000000">)</SPAN> ut.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; ...</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR>JTATransaction的控制稍微复杂，不过仍然可以很清楚的看出来Hibernate是如何封装JTA的Transaction代码的。 <BR><BR>但是你现在是否看到了什么问题？ 仔细想一下，Hibernate Transaction是从Session中获得的，tx = session.beginTransaction()，最后要先提交tx，然后再session.close，这完全符合JDBC的Transaction的操作顺序，但是这个顺序是和JTA的Transactioin操作顺序彻底矛盾的！！！ JTA是先启动Transaction，然后启动Session，关闭Session，最后提交Transaction，因此当你使用JTA的Transaction的时候，那么就千万不要使用Hibernate的Transaction，而是应该像我上面的JTA的代码片断那样使用才行。 <BR><BR>总结： <BR><BR>1、在JDBC上使用Hibernate <BR><BR>必须写上Hibernate Transaction代码，否则数据库没有反应。此时Hibernate的Transaction就是Connection.commit而已 <BR><BR>2、在JTA上使用Hibernate <BR><BR>写JTA的Transaction代码，不要写Hibernate的Transaction代码，否则程序会报错 <BR><BR>3、在EJB上使用Hibernate <BR><BR>什么Transactioin代码都不要写，在EJB的部署描述符里面配置 <BR><BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><BR>|---CMT<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>Container</SPAN> Managed Transaction<SPAN style="COLOR: #000000">)</SPAN> <BR>| <BR>|---BMT<SPAN style="COLOR: #000000">(</SPAN>Bean Managed Transaction<SPAN style="COLOR: #000000">)</SPAN> <BR>&nbsp; &nbsp; &nbsp; &nbsp; | <BR>&nbsp; &nbsp; &nbsp; &nbsp; |----JDBC Transaction <BR>&nbsp; &nbsp; &nbsp; &nbsp; | <BR>&nbsp; &nbsp; &nbsp; &nbsp; |----JTA Transaction <BR></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR><BR>robbin: <BR>你说“Hibernate的JDBCTransaction根本就是conn.commit而已，根本毫无神秘可言，只不过在Hibernate中，Session打开的时候，就会自动conn.setAutoCommit(false)，不像一般的JDBC，默认都是true，所以你最后不写commit也没有关系，由于Hibernate已经把AutoCommit给关掉了，所以用Hibernate的时候，你在程序中不写Transaction的话，数据库根本就没有反应” <BR>但sf.opengSession()时，并没有setAutoCommit(false)，我想问的是，如果不编写任何事务代码，如： <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>Session s = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>...... <BR><SPAN style="COLOR: #000000">s</SPAN>.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody>数据库会不会有反应（此时应该是默认AutoCommit为true）。 <BR><BR>另外，我想问一下： <BR>1. s.flush()是不是必须的 <BR>2. s.close()是不是一定要关闭 <BR>比如你上面提到的： <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>javax.<SPAN style="COLOR: #000000">transaction</SPAN>.<SPAN style="COLOR: #000000">UserTransaction</SPAN> tx = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="COLOR: #aaaadd" ?>InitialContext</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">lookup</SPAN><SPAN style="COLOR: #000000">(</SPAN>"javax.<SPAN style="COLOR: #000000">transaction</SPAN>.<SPAN style="COLOR: #000000">UserTransaction</SPAN>"<SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>Session s1 = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>... <BR><SPAN style="COLOR: #000000">s1</SPAN>.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>s1.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>... <BR><BR><SPAN style="COLOR: #000000">Session</SPAN> s2 = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>... <BR><SPAN style="COLOR: #000000">s2</SPAN>.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>s2.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><BR>tx.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>s1不关闭，使用s2进行操作的代码中使用s1可不可以（我觉得这样更加节约资源，不需要反复的连接、关闭） <BR><BR><BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote>但sf.opengSession()时，并没有setAutoCommit(false)，我想问的是，如果不编写任何事务代码，如： <BR>Session s = sf.openSession(); <BR>...... <BR>s.close(); <BR>数据库会不会有反应（此时应该是默认AutoCommit为true）。</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>不会有反应。在sf.openSession() 创建Session实例的时候，就已经调用了conn.setAutoCommit(false)了。 <BR><BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote>另外，我想问一下： <BR>1. s.flush()是不是必须的 <BR>2. s.close()是不是一定要关闭 </TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>s.flush不是必须的，s.close()会调用一次s.flush() <BR><BR>s.close()正常情况下应该关闭，除非你是用ThreadLocal管理Session。 <BR><BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>引用:</B></SPAN></TD></TR>
<TR>
<TD class=quote>s1不关闭，使用s2进行操作的代码中使用s1可不可以（我觉得这样更加节约资源，不需要反复的连接、关闭）</TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>在这个例子中看不出来JTA的作用。 <BR>假设 <BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>Class</SPAN> A&nbsp; <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; find<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; Session s1 = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; ... <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">s1</SPAN>.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; s1.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>Class</SPAN> B&nbsp; <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; find<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; &nbsp; Session s2 = sf.<SPAN style="COLOR: #000000">openSession</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; ... <BR>&nbsp; &nbsp; <SPAN style="COLOR: #000000">s2</SPAN>.<SPAN style="COLOR: #000000">flush</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; &nbsp; s2.<SPAN style="COLOR: #000000">close</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; <SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>Main <SPAN style="COLOR: #000000">{</SPAN> <BR><BR>&nbsp; tx = ...; <BR>&nbsp; A.<SPAN style="COLOR: #000000">find</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; B.<SPAN style="COLOR: #000000">find</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; tx.<SPAN style="COLOR: #000000">commit</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>看明白了吗？JTA的Transaction管理是跨类调用的。</SPAN><img src ="http://www.blogjava.net/TrampEagle/aggbug/29971.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-08 22:08 <a href="http://www.blogjava.net/TrampEagle/articles/29971.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 可以实现分页查询</title><link>http://www.blogjava.net/TrampEagle/articles/29969.html</link><dc:creator>TrampEagle</dc:creator><author>TrampEagle</author><pubDate>Wed, 08 Feb 2006 14:04:00 GMT</pubDate><guid>http://www.blogjava.net/TrampEagle/articles/29969.html</guid><wfw:comment>http://www.blogjava.net/TrampEagle/comments/29969.html</wfw:comment><comments>http://www.blogjava.net/TrampEagle/articles/29969.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/TrampEagle/comments/commentRss/29969.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/TrampEagle/services/trackbacks/29969.html</trackback:ping><description><![CDATA[<SPAN class=postbody>本文引自：<BR><A href="http://forum.javaeye.com/viewtopic.php?t=261">http://forum.javaeye.com/viewtopic.php?t=261</A><BR>Hibernate 可以实现分页查询，例如： <BR>从第2万条开始取出100条记录 <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR>Query q = session.<SPAN style="COLOR: #000000">createQuery</SPAN><SPAN style="COLOR: #000000">(</SPAN>"from Cat as c"<SPAN style="COLOR: #000000">)</SPAN>; <BR>q.<SPAN style="COLOR: #000000">setFirstResult</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>20000</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>q.<SPAN style="COLOR: #000000">setMaxResults</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>100</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><SPAN style="COLOR: #aaaadd" ?>List</SPAN> l = q.<SPAN style="COLOR: #000000">list</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>;</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>那么Hibernate底层如何实现分页的呢？实际上Hibernate的查询定义在net.sf.hibernate.loader.Loader这个类里面，仔细阅读该类代码，就可以把问题彻底搞清楚。 <BR><BR>Hibernate2.0.3的Loader源代码第480行以下： <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN>useLimit<SPAN style="COLOR: #000000">)</SPAN> sql = dialect.<SPAN style="COLOR: #000000">getLimitString</SPAN><SPAN style="COLOR: #000000">(</SPAN>sql<SPAN style="COLOR: #000000">)</SPAN>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <BR><SPAN style="COLOR: #aaaadd" ?>PreparedStatement</SPAN> st = session.<SPAN style="COLOR: #000000">getBatcher</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">prepareQueryStatement</SPAN><SPAN style="COLOR: #000000">(</SPAN>sql, scrollable<SPAN style="COLOR: #000000">)</SPAN>;</DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR><BR>如果相应的数据库定义了限定查询记录的sql语句，那么直接使用特定数据库的sql语句。 <BR><BR>然后来看net.sf.hibernate.dialect.MySQLDialect: <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>boolean</SPAN> supportsLimit<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> getLimitString<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN> sql<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; <SPAN style="COLOR: #aaaadd" ?>StringBuffer</SPAN> pagingSelect = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="COLOR: #aaaadd" ?>StringBuffer</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>100</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; pagingSelect.<SPAN style="COLOR: #000000">append</SPAN><SPAN style="COLOR: #000000">(</SPAN>sql<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; pagingSelect.<SPAN style="COLOR: #000000">append</SPAN><SPAN style="COLOR: #000000">(</SPAN>" limit ?, ?"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> pagingSelect.<SPAN style="COLOR: #000000">toString</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>这是MySQL的专用分页语句，再来看net.sf.hibernate.dialect.Oracle9Dialect: <BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>boolean</SPAN> supportsLimit<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>true</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN> <BR><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>public</SPAN> <SPAN style="COLOR: #aaaadd" ?>String</SPAN> getLimitString<SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #aaaadd" ?>String</SPAN> sql<SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; <SPAN style="COLOR: #aaaadd" ?>StringBuffer</SPAN> pagingSelect = <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>new</SPAN> <SPAN style="COLOR: #aaaadd" ?>StringBuffer</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000" ?>100</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; pagingSelect.<SPAN style="COLOR: #000000">append</SPAN><SPAN style="COLOR: #000000">(</SPAN>"select * from <SPAN style="COLOR: #000000">(</SPAN> select row_.*, rownum rownum_ from <SPAN style="COLOR: #000000">(</SPAN> "<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; pagingSelect.<SPAN style="COLOR: #000000">append</SPAN><SPAN style="COLOR: #000000">(</SPAN>sql<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; pagingSelect.<SPAN style="COLOR: #000000">append</SPAN><SPAN style="COLOR: #000000">(</SPAN>" <SPAN style="COLOR: #000000">)</SPAN> row_ where rownum &lt;= ?<SPAN style="COLOR: #000000">)</SPAN> where rownum_ &gt; ?"<SPAN style="COLOR: #000000">)</SPAN>; <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>return</SPAN> pagingSelect.<SPAN style="COLOR: #000000">toString</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR>Oracle采用嵌套3层的查询语句结合rownum来实现分页，这在Oracle上是最快的方式，如果只是一层或者两层的查询语句的rownum不能支持order by。 <BR><BR>除此之外，Interbase，PostgreSQL，HSQL也支持分页的sql语句，在相应的Dialect里面，大家自行参考。 <BR><BR>如果数据库不支持分页的SQL语句，那么根据在配置文件里面 <BR>#hibernate.jdbc.use_scrollable_resultset true <BR>默认是true，如果你不指定为false，那么Hibernate会使用JDBC2.0的scrollable result来实现分页，看Loader第430行以下： <BR><BR><BR></SPAN>
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN class=genmed><B>java代码:&nbsp;</B></SPAN></TD></TR>
<TR>
<TD class=code>
<DIV style="FONT-FAMILY: 'Courier New', Courier, monospace"><BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>if</SPAN> <SPAN style="COLOR: #000000">(</SPAN> session.<SPAN style="COLOR: #000000">getFactory</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>.<SPAN style="COLOR: #000000">useScrollableResultSets</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">)</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; <SPAN style="COLOR: #6666ff">// we can go straight to the first required row</SPAN> <BR>&nbsp; rs.<SPAN style="COLOR: #000000">absolute</SPAN><SPAN style="COLOR: #000000">(</SPAN>firstRow<SPAN style="COLOR: #000000">)</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN> <BR><SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>else</SPAN> <SPAN style="COLOR: #000000">{</SPAN> <BR>&nbsp; <SPAN style="COLOR: #6666ff">// we need to step through the rows one row at a time (slow)</SPAN> <BR>&nbsp; <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>for</SPAN> <SPAN style="COLOR: #000000">(</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #990066" ?>int</SPAN> m=<SPAN style="COLOR: #000000" ?>0</SPAN>; m&lt;firstRow; m++ <SPAN style="COLOR: #000000">)</SPAN> rs.<SPAN style="COLOR: #000000">next</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">)</SPAN>; <BR><SPAN style="COLOR: #000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN class=postbody><BR><BR><BR>如果支持scrollable result，使用ResultSet的absolute方法直接移到查询起点，如果不支持的话，使用循环语句，rs.next一点点的移过去。 <BR><BR>可见使用Hibernate，在进行查询分页的操作上，是具有非常大的灵活性，Hibernate会首先尝试用特定数据库的分页sql，如果没用，再尝试Scrollable，如果不行，最后采用rset.next()移动的办法。 <BR><BR>在查询分页代码中使用Hibernate的一大好处是，既兼顾了查询分页的性能，同时又保证了代码在不同的数据库之间的可移植性。</SPAN><SPAN class=postbody></SPAN><SPAN class=gensmall></SPAN><img src ="http://www.blogjava.net/TrampEagle/aggbug/29969.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/TrampEagle/" target="_blank">TrampEagle</a> 2006-02-08 22:04 <a href="http://www.blogjava.net/TrampEagle/articles/29969.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>