<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title><![CDATA[friend365 - Hibernate]]></title>
<link>http://www.friend365.cn/</link>
<description><![CDATA[敬业是对渴望成功的人对待工作的基本要求，思想决定行为，行为形成习惯，习惯决定性格，性格决定命运。永久域名www.friend365.cn]]></description>
<language>zh-cn</language>
<copyright><![CDATA[Copyright 2005-2009 friend365.cn]]></copyright>
<webMaster><![CDATA[friend365@163.com(美丽人生)]]></webMaster>
<generator>friend365.cn</generator> 
<image>
	<title>friend365</title> 
	<url>http://www.friend365.cn/images/logomy.gif</url> 
	<link>http://www.friend365.cn/</link> 
	<description>friend365</description> 
</image>

			<item>
			<link>http://www.friend365.cn/default.asp?id=720</link>
			<title><![CDATA[Java应用中Hibernate对多表关联查询的总结]]></title>
			<author>friend365@163.com(51itcn)</author>
			<category><![CDATA[Hibernate]]></category>
			<pubDate>Fri,11 Apr 2008 09:20:20 +0800</pubDate>
			<guid>http://www.friend365.cn/default.asp?id=720</guid>	
		<description><![CDATA[由于公司项目的需要，我对Hibernate对多表关联查询研究了一下，现总结如下，供朋友参考。 <br/><br/>一、 Hibernate简介Hibernate是一个JDO工具。它的工作原理是通过文件（一般有两种：xml文件和properties文件）把值对象和数据库表之间建立起一个映射关系。这样，我们只需要通过操作这些值对象和Hibernate提供的一些基本类，就可以达到使用数据库的目的。例如，使用Hibernate的查询，可以直接返回包含某个值对象的列表（List），而不必向传统的JDBC访问方式一样把结果集的数据逐个装载到一个值对象中，为编码工作节约了大量的时间。Hibernate提供的HQL是一种类SQL语言，它和EJBQL一样都是提供对象化的数据库查询方式，但HQL在功能和使用方式上都非常接近于标准的SQL. <br/><br/><br/>二、 Hibernate与JDBC的区别Hibernate与JDBC的主要区别如下： <br/><br/><br/>1、 Hibernate是JDBC的轻量级的对象封装，它是一个独立的对象持久层框架，和App Server，和EJB没有什么必然的联系。Hibernate可以用在任何JDBC可以使用的场合，从某种意义上来说，Hibernate在任何场合下取代JDBC. <br/><br/><br/>2、 Hibernate是一个和JDBC密切关联的框架，所以Hibernate的兼容性和JDBC驱动，和数据库都有一定的关系，但是和使用它的Java程序，和App Server没有任何关系，也不存在兼容性问题。 <br/><br/><br/>3、 Hibernate是做为JDBC的替代者出现的，不能用来直接和Entity Bean做对比。 <br/><br/><br/>三、 Hibernate 进行多表关联查询Hibernate对多个表进行查询时，查询结果是多个表的笛卡尔积，或者称为“交叉”连接。 例如：from Student， Book from Student as stu， Book as boo from Student stu， Book boo注重：让查询中的Student和Book均是表student和book对应的类名，它的名字一定要和类的名字相同，包括字母的大小写。别名应该服从首字母小写的规则是一个好习惯，这和Java对局部变量的命名规范是一致的。 <br/><br/><br/>下面列举一个完整的例子来说明Hibernate对多个表进行关联查询（其中粗体是我们要非凡注重的地方，相应表tBookInfo和BookSel&#101;ction的结构和其对应的hbm.xml、class文件就不一一列举了:<br/><br/>String sTest = &#34;from tBookInfo book, BookSel&#101;ction sel wh&#101;re book.id = sel.bookId&#34;; <br/><br/>Collection result = new ArrayList(); <br/><br/>Transaction tx = null; <br/><br/>try { <br/><div class="UBBPanel"><div class="UBBTitle"><img src="http://www.friend365.cn/images/quote.gif" style="margin:0px 2px -3px 0px" alt="隐藏内容"/> 隐藏内容</div><div class="UBBContent">该内容已经被作者隐藏,只有会员才允许查阅 <a href="http://www.friend365.cn/login.asp">登录</a> | <a href="http://www.friend365.cn/register.asp">注册</a></div></div>]]></description>
		</item>
		
			<item>
			<link>http://www.friend365.cn/default.asp?id=660</link>
			<title><![CDATA[struts+hibernate项目debug总结]]></title>
			<author>friend365@163.com(51itcn)</author>
			<category><![CDATA[Hibernate]]></category>
			<pubDate>Mon,31 Mar 2008 14:54:10 +0800</pubDate>
			<guid>http://www.friend365.cn/default.asp?id=660</guid>	
		<description><![CDATA[javax.servlet.ServletException: Cannot retrieve mapping for action /companyNews<br/><br/>struts-config.xml中没有写相关companyNews的action. <br/><br/>============================================<br/>在myeclipse增加一个jar包时候，先打开项目properties,选择java build path --&gt; libraries --&gt;add external JARs 选择到需要的jar包，加入后，发现，他这个包加载的是绝对路径，而我们的项目需要cvs共享，无法<br/><br/>commit到cvs服务器。myeclipse有以下提示信息：<br/>2 build path entries are missing.<br/><br/>解决办法是，关闭myeclipse,用notepad打开项目目录下的 .classpath, 手动修改成相对路径，并且检查，指定的相对路径中是否真正添加了jar包，<br/>启动myeclipse就可以上传新添加的jar包了。<br/><br/><br/>=============================================<br/>在使用junit/StrutsTest时候，报错：<br/>java.lang.UnsupportedClassVersionError: junit/framework/TestListener (Unsupported major.minor version 49.0)<br/><br/>原因是如果是jdk1.4的话，只能使用junit3.8以下版本，如果是使用jdk1.5的话，必须使用junit4.0以上版本。<br/><br/> <br/><br/> <br/><br/><br/>==========================================================<br/><br/>Cannot find ActionMappings o&#114; ActionformBeans collection<br/>原因是：web.xml文件中没有配置struts-config.xml的相关信息。<br/><br/><br/>============================================================<br/><br/>o&#114;g.apache.jasper.JasperException: The absolute uri: <a href="http://java.sun.com/jstl/core" target="_blank">http://java.sun.com/jstl/core</a> cannot be resolved in either web.xml o&#114; the jar files deployed with this application<br/><br/><br/>缺少jstl的相关jar和web.xml配置<br/>jstl.jar<br/>&lt;taglib&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-uri&gt;<a href="http://java.sun.com/jstl/fmt" target="_blank">http://java.sun.com/jstl/fmt</a>&lt;/taglib-uri&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-location&gt;/WEB-INF/fmt.tld&lt;/taglib-location&gt;<br/>&lt;/taglib&gt;<br/><br/>&lt;taglib&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-uri&gt;<a href="http://java.sun.com/jstl/fmt" target="_blank">http://java.sun.com/jstl/fmt</a>-rt&lt;/taglib-uri&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-location&gt;/WEB-INF/fmt-rt.tld&lt;/taglib-location&gt;<br/>&lt;/taglib&gt;<br/><br/>&lt;taglib&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-uri&gt;<a href="http://java.sun.com/jstl/core" target="_blank">http://java.sun.com/jstl/core</a>&lt;/taglib-uri&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-location&gt;/WEB-INF/c.tld&lt;/taglib-location&gt;<br/>&lt;/taglib&gt;<br/><br/>&lt;taglib&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-uri&gt;<a href="http://java.sun.com/jstl/core" target="_blank">http://java.sun.com/jstl/core</a>-rt&lt;/taglib-uri&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-location&gt;/WEB-INF/c-rt.tld&lt;/taglib-location&gt;<br/>&lt;/taglib&gt;<br/><br/>&lt;taglib&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-uri&gt;<a href="http://java.sun.com/jstl/sql" target="_blank">http://java.sun.com/jstl/sql</a>&lt;/taglib-uri&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-location&gt;/WEB-INF/sql.tld&lt;/taglib-location&gt;<br/>&lt;/taglib&gt;<br/><br/>&lt;taglib&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-uri&gt;<a href="http://java.sun.com/jstl/sql" target="_blank">http://java.sun.com/jstl/sql</a>-rt&lt;/taglib-uri&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-location&gt;/WEB-INF/sql-rt.tld&lt;/taglib-location&gt;<br/>&lt;/taglib&gt;<br/><br/>&lt;taglib&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-uri&gt;<a href="http://java.sun.com/jstl/x" target="_blank">http://java.sun.com/jstl/x</a>&lt;/taglib-uri&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-location&gt;/WEB-INF/x.tld&lt;/taglib-location&gt;<br/>&lt;/taglib&gt;<br/><br/>&lt;taglib&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-uri&gt;<a href="http://java.sun.com/jstl/x" target="_blank">http://java.sun.com/jstl/x</a>-rt&lt;/taglib-uri&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;taglib-location&gt;/WEB-INF/x-rt.tld&lt;/taglib-location&gt;<br/>&lt;/taglib&gt;<br/><br/><br/>==========================================================<br/><br/>Failed to load o&#114; instantiate TagLibraryValidator class: o&#114;g.apache.taglibs.standard.tlv.JstlCoreTLV<br/><br/><br/>standard.jar没有放在lib里面<br/><br/><br/>===========================================================<br/><br/>创建oracle表的时候，使用了，role和comment关键字，建议不要将这两个关键字作为表名和字段名。<br/>建议用toad或plsql developer创建表，这样软件会提示关键字。<br/>=============================================================<br/><br/>[ERROR] XMLHelper - Error parsing XML: XML InputStream(18) Attribute name &#34;column&#34; associated with an element type &#34;key&#34; must be followed by the &#39; = &#39; character.<br/>[ERROR] Configuration - Could not configure datastore from input stream &lt;org.dom4j.DocumentException: Error on line 18 of document&nbsp;&nbsp;: Attribute name &#34;column&#34; associated with an element type &#34;key&#34; <br/><br/>must be followed by the &#39; = &#39; character. Nested exception: Attribute name &#34;column&#34; associated with an element type &#34;key&#34; must be followed by the &#39; = &#39; character.&gt;org.dom4j.DocumentException: Error <br/><br/>on line 18 of document&nbsp;&nbsp;: Attribute name &#34;column&#34; associated with an element type &#34;key&#34; must be followed by the &#39; = &#39; character. Nested exception: Attribute name &#34;column&#34; associated with an element <br/><br/>type &#34;key&#34; must be followed by the &#39; = &#39; character.<br/><br/> <br/><br/>xml 语法错误，key语法中应该类似这样的写法 &lt;key column=&#34;id&#34;&gt;<br/>=============================================================<br/>hibernate3,对象中一对多的one方，必须写private Set pays=new HashSet();，否则包错java.lang.NullPointerException ，<br/>而hibernate2中private Set pays;却不报错；<br/><br/>=============================================================<br/>[DEBUG] AbstractSaveEventListener - generated identifier: 1, using strategy: o&#114;g.hibernate.id.IncrementGenerator<br/>o&#114;g.hibernate.PropertyValueException: not-null property references a null o&#114; transient value: com.xxx.yyy.company<br/> at o&#114;g.hibernate.engine.Nullability.checkNullability(Nullability.java:72)<br/><br/>&lt;many to one &gt;中的设置应该设置为not-null=&#34;false&#34; ，设置为not-null=&#34;true&#34;则报以上错误<br/>================================================================<br/><br/><br/>o&#114;g.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xxx.yyy.Company<br/><br/>在save的同时也需要save其他的表，然后再flush()<br/>=========================================================================<br/><br/>Parse Fatal Error at line 12 column 1<br/><br/>struts-config.xml文件被修改，语法错误。检查语法。<br/><br/>========================================<br/><br/>o&#114;g.hibernate.QueryException: could not resolve property: userid of: com.xxx.yyy.Pay<br/><br/>使用到外键userid的时候，必须使用userinfo.userid方法才能得到。<br/><br/>=========================================<br/>javax.naming.NameNotFoundException: Name hibernate_connection_factory is not bound in this Context<br/>原因：hibernate的数据库映射.xml文件有配置错误，导致hibernate_connection_factory无法绑定数据库。<br/>例如many-to-one设置了以后，仍然在其中设置相冲突的&lt;property&gt;属性。<br/><br/>==========================================<br/><br/>[WARN] RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException<br/>必须将使用到的对象new起来。<br/><br/>============================================<br/>GROUP BY 表达式的查询必须满足如下：<br/>sel&#101;ct 子句后的每一项必需出现在group by 子句中，除非该项使用了聚集函数。<br/><br/>===========================================<br/>o&#114;g.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xxx.yyy.Company<br/><br/>要级联保存，多次session.save()<br/>==============================================<br/>java.lang.IllegalArgumentException: id to load is required for loading<br/>原因：<br/><br/>session.load(Company.class,payForm.getCompanyId());<br/>load()方法第二个参数必须是searlizable,并且必须是和数据库映射类的属性值类型一致，即使强制转换都不行。<br/><br/><br/>=============================================<br/>[INFO] DefaultLoadEventListener - Error performing load command &lt;org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.xxx.yyy.Company#0]<br/><br/>&gt;org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.xxx.yyy.Company#0]<br/><br/><br/>表示你现在查询的对象所关联的对象有问题，一般是因为数据的问题（该对象所关联的对象找不到）,数据的错误，影响了程序正常执行。<br/><br/>========================================================<br/>eclipse 3.1,myeclipse 4错误<br/>Deployment is out of date due to changes in the underlying project contents . You&#39;ll need to mannally &#39;Redeploy&#39; the project to up&#100;ate the deployed archive.<br/>原因<br/>tomcat 中部署的某个文件的拒绝访问影响了部署。<br/>重启后，去除tomcat中部署的文件。重新在eclipse中设置部署。<br/>为什么需要重启？<br/>因为：google desktop软件正在对我部署的一个300MB大文件进行索引，锁定了这个大文件，我估计google desktop需要对这个文件索引半个小时以上。因此，eclipse无法对过去部署的文件，做先删除后重新部署的工作。<br/><br/>===================================================<br/>ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed<br/> <br/>o&#114;g.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed<br/>解决办法。<br/> cmpy=(CompanyEdit)ss.load(CompanyEdit.class,companyId1);<br/>&nbsp;&nbsp; Hibernate.initialize(cmpy);//强制初始化cmpy,否则ss.close()后,cmpy将消失.<br/><br/>====================================================<br/>javascript错误<br/>行: 56<br/>字符: 45<br/>错误: 未结束的字符串常量<br/>代码: 0<br/><br/>是编码的问题！用ANSI编码另存后就好了。<br/><br/>====================================================<br/>[WARN] JDBCExceptionReporter - SQL Error: 904, SQLState: 42000<br/>[ERROR] JDBCExceptionReporter - o&#114;A-00904: 无效列名<br/><br/>[INFO] DefaultLoadEventListener - Error performing load command &lt;org.hibernate.exception.SQLGrammarException: could not load an entity: [com.xxx.yyy.Sellinfo#1]<br/><br/>&gt;org.hibernate.exception.SQLGrammarException: could not load an entity: [com.xxx.yyy.Sellinfo#1]<br/><br/>***.hbm.xml文件中的某个列名和数据库中的不同。<br/><br/><br/>=======================================================<br/>ConnectionManager - unclosed connection, forgot to call close() on your session?<br/><br/><br/>原因：没有关闭hibernate的session的transaction。或者没有关闭session<br/><br/>=======================================================<br/>[WARN] SellCommentDAO - o&#114;g.hibernate.ObjectDel&#101;tedException: del&#101;ted object would be re-saved by cascade (remove del&#101;ted object from associations): [com.xxx.yyy.SellComment#7]<br/><br/>原因：父亲对象（one方）设置cascade=&#34;save-up&#100;ate&#34; 时，直接删除子对象时，会报错，<br/>处理方法：save信息需要级联操作，del&#101;te时候也要用相同的原理。<br/>SellComment sellComment=new SellComment();<br/>sellComment = (SellComment)session.load(SellComment.class,sellCommentId);//获取儿子对象<br/>Long sellInfoId=sellComment.getSellInfo().getSellId();//获取父亲id<br/>SellInfo sellinfo=(SellInfo)session.load(SellInfo.class,sellInfoId);//获取父亲对象<br/>sellInfo.getSellComments().remove(sellComment);//断绝父子关系<br/>sellComment.setSellInfo(null);//断绝子父关系<br/>session.del&#101;te(sellComment);//删除儿子<br/>session.flush();]]></description>
		</item>
		
			<item>
			<link>http://www.friend365.cn/default.asp?id=638</link>
			<title><![CDATA[Hibernate 实现分页_无忧IT]]></title>
			<author>friend365@163.com(51itcn)</author>
			<category><![CDATA[Hibernate]]></category>
			<pubDate>Thu,27 Mar 2008 12:50:47 +0800</pubDate>
			<guid>http://www.friend365.cn/default.asp?id=638</guid>	
		<description><![CDATA[action:<br/><div class="UBBPanel"><div class="UBBTitle"><img src="http://www.friend365.cn/images/code.gif" style="margin:0px 2px -3px 0px" alt="程序代码"/> 程序代码</div><div class="UBBContent"><br/>public ActionForward listresume(&nbsp;&nbsp; ActionMapping mapping,<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActionForm form,<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletRequest request,<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpServletResponse response)...{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StuffInfoDao stuffDao=(StuffInfoImpl)getBean(&#34;stuffInfoDao&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String hql=&#34;from StuffClientInfo wh&#101;re flag = 1 &#34;;<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 offset = 0 ;<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 length= 8 ;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List list = stuffDao.getListForPage(hql, offset, length);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.setAttribute(&#34;listresume&#34;,list);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return mapping.findForward(&#34;***&#34;);<br/>&nbsp;&nbsp; }<br/></div></div><br/>impl:<br/><div class="UBBPanel"><div class="UBBTitle"><img src="http://www.friend365.cn/images/code.gif" style="margin:0px 2px -3px 0px" alt="程序代码"/> 程序代码</div><div class="UBBContent"><br/>/**//*<br/>&nbsp;&nbsp;&nbsp;&nbsp;* method getListForPage方法实现分页查询<br/>&nbsp;&nbsp;&nbsp;&nbsp;* @param hql查询语句<br/>&nbsp;&nbsp;&nbsp;&nbsp;* @param offset开始查询的位置<br/>&nbsp;&nbsp;&nbsp;&nbsp;* @param length查询数据的长度<br/>&nbsp;&nbsp;&nbsp;&nbsp;* return 返回查询数据列表即结果；<br/>&nbsp;&nbsp;&nbsp;&nbsp;*/<br/>&nbsp;&nbsp; public List getListForPage( final String hql, final int offset, final int length )...{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 实现分页<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List list=getHibernateTemplate().executeFind(new&nbsp;&nbsp;HibernateCallback()...{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Object doInHibernate(Session session)throws HibernateException,SQLException...{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Query query=session.cr&#101;ateQuery(hql);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; query.setFirstResult(offset);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; query.setMaxResults(length);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List list=query.list();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return list;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return list;<br/>&nbsp;&nbsp; }<br/></div></div>]]></description>
		</item>
		
			<item>
			<link>http://www.friend365.cn/default.asp?id=637</link>
			<title><![CDATA[Hibernate的lazy问题]]></title>
			<author>friend365@163.com(51itcn)</author>
			<category><![CDATA[Hibernate]]></category>
			<pubDate>Thu,27 Mar 2008 12:48:18 +0800</pubDate>
			<guid>http://www.friend365.cn/default.asp?id=637</guid>	
		<description><![CDATA[在运行孙卫琴那本书第七章第一个例子的时候，<br/>BusinessServices.java<br/>package mypack;<br/><br/>import java.util.HashSet;<br/>import java.util.Iterator;<br/>import java.util.List;<br/>import java.util.Set;<br/><br/>import o&#114;g.hibernate.Session;<br/>import o&#114;g.hibernate.SessionFactory;<br/>import o&#114;g.hibernate.Transaction;<br/>import o&#114;g.hibernate.cfg.Configuration;<br/>public class BusinessService{<br/>&nbsp;&nbsp;public static SessionFactory sessionFactory;<br/>&nbsp;&nbsp;static{<br/>&nbsp;&nbsp;&nbsp;&nbsp; try{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Cr&#101;ate a configuration based on the properties file we&#39;ve put<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Configuration config = new Configuration();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; config.addClass(Category.class);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Get the session factory we can use for persistence<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sessionFactory = config.configure().buildSessionFactory();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}catch(Exception e){e.printStackTrace();}<br/>&nbsp;&nbsp;}<br/><br/>&nbsp;&nbsp;public void saveFoodCategory() throws Exception{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category&nbsp;&nbsp;foodCategory=new Category(&#34;food&#34;,null,new HashSet());<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category&nbsp;&nbsp;fruitCategory=new Category(&#34;fruit&#34;,null,new HashSet());<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category&nbsp;&nbsp;appleCategory=new Category(&#34;apple&#34;,null,new HashSet());<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//建立食品类别和水果类别之间的关联关系<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foodCategory.addChildCategory(fruitCategory);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //建立水果类别和苹果类别之间的关联关系<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fruitCategory.addChildCategory(appleCategory);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; saveOrUp&#100;ate(foodCategory);<br/>&nbsp;&nbsp;}<br/><br/>&nbsp;&nbsp;public void navigateCategories() throws Exception{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category fruitCategory=findCategoryByName(&#34;fruit&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HashSet categories=new HashSet();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;navigateCategories(fruitCategory,categories);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (Iterator it = categories.iterator(); it.hasNext();) {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(((Category)it.next()).getName());<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;private void navigateCategories(Category category,Set categories){<br/>&nbsp;&nbsp; if(categories.contains(category)|| category==null)return;<br/>&nbsp;&nbsp; categories.add(category);<br/><br/>&nbsp;&nbsp; //遍历父类Category<br/>&nbsp;&nbsp; navigateCategories(category.getParentCategory(),categories);<br/>//遍历所有子类Category<br/>&nbsp;&nbsp; Set childCategories=category.getChildCategories();<br/>&nbsp;&nbsp; if(childCategories==null)return;<br/><br/>&nbsp;&nbsp; for (Iterator it = childCategories.iterator(); it.hasNext();) {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; navigateCategories((Category)it.next(),categories);<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public void saveVegetableCategory() throws Exception{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category foodCategory=findCategoryByName(&#34;food&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category vegetableCategory=new Category(&#34;vegetable&#34;,null,new HashSet());<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foodCategory.addChildCategory(vegetableCategory);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;saveOrUp&#100;ate(vegetableCategory);<br/>&nbsp;&nbsp;}<br/><br/>&nbsp;&nbsp;public void up&#100;ateVegetableCategory() throws Exception{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category vegetableCategory=findCategoryByName(&#34;vegetable&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vegetableCategory.setName(&#34;green vegetable&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category tomatoCategory=new Category(&#34;tomato&#34;,null,new HashSet());<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vegetableCategory.addChildCategory(tomatoCategory);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;saveOrUp&#100;ate(vegetableCategory);<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public void saveOrangeCategory() throws Exception{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Session session = sessionFactory.openSession();<br/>&nbsp;&nbsp;&nbsp;&nbsp;Transaction tx = null;<br/>&nbsp;&nbsp;&nbsp;&nbsp;try {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx = session.beginTransaction();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category fruitCategory=findCategoryByName(session,&#34;fruit&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category o&#114;angeCategory=new Category(&#34;orange&#34;,null,new HashSet());<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fruitCategory.addChildCategory(orangeCategory);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.commit();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}catch (Exception e) {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (tx != null) {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Something went wrong; discard all partial changes<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.rollback();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw e;<br/>&nbsp;&nbsp;&nbsp;&nbsp;} finally {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// No matter what, close the session<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.close();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public void saveOrUp&#100;ate(Object object) throws Exception{<br/>Session session = sessionFactory.openSession();<br/>&nbsp;&nbsp;&nbsp;&nbsp;Transaction tx = null;<br/>&nbsp;&nbsp;&nbsp;&nbsp;try {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx = session.beginTransaction();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.saveOrUp&#100;ate(object);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.commit();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}catch (Exception e) {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (tx != null) {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Something went wrong; discard all partial changes<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.rollback();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw e;<br/>&nbsp;&nbsp;&nbsp;&nbsp;} finally {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// No matter what, close the session<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.close();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/><br/>&nbsp;&nbsp; private Category findCategoryByName(String name)throws Exception{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Session session = sessionFactory.openSession();<br/>&nbsp;&nbsp;&nbsp;&nbsp;Transaction tx = null;<br/>&nbsp;&nbsp;&nbsp;&nbsp;Category category=null;<br/>&nbsp;&nbsp;&nbsp;&nbsp;try {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx = session.beginTransaction();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List results=session.cr&#101;ateQuery(&#34;from Category as c wh&#101;re c.name=&#39;&#34;+name+&#34;&#39;&#34;).list();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;category=(Category)results.iterator().next();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.commit();<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;}catch (Exception e) {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (tx != null) {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Something went wrong; discard all partial changes<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.rollback();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw e;<br/>&nbsp;&nbsp;&nbsp;&nbsp;} finally {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// No matter what, close the session<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.close();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;return category;<br/>&nbsp;&nbsp;}<br/><br/>&nbsp;&nbsp; private Category findCategoryByName(Session session, String name)throws Exception{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List results=session.cr&#101;ateQuery(&#34;from Category as c wh&#101;re c.name=&#39;&#34;+name+&#34;&#39;&#34;).list();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (Category)results.iterator().next();<br/>&nbsp;&nbsp;}<br/><br/>&nbsp;&nbsp; public void test() throws Exception{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//saveFoodCategory();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//saveOrangeCategory();<br/> saveVegetableCategory();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//up&#100;ateVegetableCategory();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//navigateCategories();<br/>&nbsp;&nbsp; }<br/><br/>&nbsp;&nbsp;public static void main(String args[]) throws Exception {<br/>&nbsp;&nbsp;&nbsp;&nbsp;new BusinessService().test();<br/>&nbsp;&nbsp;&nbsp;&nbsp;sessionFactory.close();<br/>&nbsp;&nbsp;}<br/>}<br/><br/>saveVegetableCategory();方法抛出错误：<br/>o&#114;g.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: mypack.Category.childCategories, no session o&#114; session was closed<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at o&#114;g.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at o&#114;g.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at o&#114;g.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at o&#114;g.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:183)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at o&#114;g.hibernate.collection.PersistentSet.add(PersistentSet.java:165)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at mypack.Category.addChildCategory(Category.java:80)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at mypack.BusinessService.saveVegetableCategory(BusinessService.java:66)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at mypack.BusinessService.test(BusinessService.java:152)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at mypack.BusinessService.main(BusinessService.java:158)<br/>Exception in thread &#34;main&#34; <br/>但是我的配置文件里面在-many-to-one中已经加入了lazy＝&#34;false&#34;了。<br/>Category.hbm.xml<br/>&lt;?xml version=&#34;1.0&#34;?&gt;<br/>&lt;!DOCTYPE hibernate-mapping<br/>PUBLIC &#34;-//Hibernate/Hibernate Mapping DTD 3.0//EN&#34;<br/>&#34;<a href="http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" target="_blank">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</a>&#34;&gt;<br/>&lt;hibernate-mapping &gt;<br/>&nbsp;&nbsp;&lt;class name=&#34;mypack.Category&#34; table=&#34;CATEGORIES&#34;&nbsp;&nbsp;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;id name=&#34;id&#34; type=&#34;long&#34; column=&#34;ID&#34;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=&#34;increment&#34;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/id&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name=&#34;name&#34; type=&#34;string&#34; &gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;column name=&#34;NAME&#34; length=&#34;15&#34; /&gt;<br/>&lt;/property&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;set&nbsp;&nbsp;name=&#34;childCategories&#34;&nbsp;&nbsp;cascade=&#34;save-up&#100;ate&#34; inverse=&#34;true&#34;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;key column=&#34;CATEGORY_ID&#34; /&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;one-to-many class=&#34;mypack.Category&#34;&nbsp;&nbsp;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/set&gt;&nbsp;&nbsp; <br/><br/>&nbsp;&nbsp; &lt;many-to-one&nbsp;&nbsp;name=&#34;parentCategory&#34;&nbsp;&nbsp;column=&#34;CATEGORY_ID&#34; class=&#34;mypack.Category&#34;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cascade=&#34;none&#34;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lazy=&#34;false&#34;&nbsp;&nbsp;&nbsp;&nbsp; /&gt;<br/>&nbsp;&nbsp;&lt;/class&gt;<br/><br/>&lt;/hibernate-mapping&gt;<br/>为什么还是出现延迟初始化的错误呢！<br/>因为lazy应该放到set里，而不是many-to-one里。该了就好了！！ 发现网上很多人问这个问题。]]></description>
		</item>
		
			<item>
			<link>http://www.friend365.cn/default.asp?id=73</link>
			<title><![CDATA[Hibernate参考文档]]></title>
			<author>friend365@163.com(admin)</author>
			<category><![CDATA[Hibernate]]></category>
			<pubDate>Wed,18 Oct 2006 12:45:06 +0800</pubDate>
			<guid>http://www.friend365.cn/default.asp?id=73</guid>	
		<description><![CDATA[<span style="font-size:12pt">HIBERNATE - 符合Java习惯的关系数据库持久化</span><br/>在今日的企业环境中，把面向对象的软件和关系数据库一起使用可能是相当麻烦、浪费时间的。Hibernate是一个面向Java环境的对象/关系数据库映射工具。对象/关系数据库映射(object/relational mapping (ORM))这个术语表示一种技术，用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。 <br/><br/>Hibernate不仅仅管理Java类到数据库表的映射（包括Java数据类型到SQL数据类型的映射），还提供数据查询和获取数据的方法，可以大幅度减少开发时人工使用SQL和JDBC处理数据的时间。 <br/><br/>Hibernate的目标是对于开发者通常的数据持久化相关的编程任务，解放其中的95%。对于以数据为中心的程序来说,它们往往只在数据库中使用存储过程来实现商业逻辑,Hibernate可能不是最好的解决方案;对于那些在基于Java的中间层应用中，它们实现面向对象的业务模型和商业逻辑的应用，Hibernate是最有用的。不管怎样，Hibernate一定可以帮助你消除或者包装那些针对特定厂商的SQL代码，并且帮你把结果集从表格式的表示形式转换到一系列的对象去。 <br/><br/><br/><a href="http://www.huihoo.com/framework/hibernate/reference-v3_zh-cn/" target="_blank">http://www.huihoo.com/framework/hibernate/reference-v3_zh-cn/</a>]]></description>
		</item>
		
</channel>
</rss>