﻿<?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-Junky's IT Notebook-随笔分类-hibernate</title><link>http://www.blogjava.net/junky/category/11394.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 08 Nov 2007 17:00:26 GMT</lastBuildDate><pubDate>Thu, 08 Nov 2007 17:00:26 GMT</pubDate><ttl>60</ttl><item><title>ERROR AbstractBatcher:61 - Exception executing batch</title><link>http://www.blogjava.net/junky/archive/2007/11/07/158777.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Wed, 07 Nov 2007 03:49:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2007/11/07/158777.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/158777.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2007/11/07/158777.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/158777.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/158777.html</trackback:ping><description><![CDATA[<p>Exception happens when deleting an object.<br />
</p>
<h1>Solution </h1>
<p>
<p><span style="font-family: monospace">You tried to delete an object that was allready deleted from the database.</span></p>
<p><span style="font-family: monospace">This can happen when cascading deletes where performed somewhere and you did not reattach your object. For deleting you do not nee to reattach your object but this could prevent this problem.&nbsp;</span></p>
<br />
<p><br />
</p>
<p>
<h1>Exception Message </h1>
<p>
<pre>19:47:56,078 ERROR AbstractBatcher:61 - Exception executing batch: <br />
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1<br />
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)<br />
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)<br />
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)<br />
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)<br />
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)<br />
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)<br />
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)<br />
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)<br />
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)<br />
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)<br />
</pre>
<img src ="http://www.blogjava.net/junky/aggbug/158777.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2007-11-07 11:49 <a href="http://www.blogjava.net/junky/archive/2007/11/07/158777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HIBERNATE学习笔记(转)</title><link>http://www.blogjava.net/junky/archive/2007/04/19/111963.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Thu, 19 Apr 2007 07:58:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2007/04/19/111963.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/111963.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2007/04/19/111963.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/111963.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/111963.html</trackback:ping><description><![CDATA[<p>Hibernate 学习笔记</p>
<p>一 hibernate配置<br>1.下载所需包<br>下载hibernate2.13, hibernate-extension2.13,Middlegen(已装了jdk和ant)<br>2.创建数据库(例子:student;course,stu_cou)<br>Student(id,name)<br>Course(id,name)<br>Stu_cou(stu_id,cou_id)(分别是student表和course表的外健)<br>3.使用MiddleGen(见附录)</p>
<p>二 hibernate映射关系(many-to-many)<br>1. 分析inverse参数(student.hbm.xml course.hbm.xml)<br>inverse该参数用来负责映射之间的关系,设置为false的一方可以对他们的关系进行维护<br>设置inverse = false的实体,表示在映射中是它是主动关系,由它来维户关系中的数据<br>设置inverse = true 的实体,表示在映射中是它是被动关系,它不维护关系中的数据<br>1) inverse=false的情况<br>a) 添加记录的情况<br>student.hbm.xml是主动方,由它来决定stu_cou表中的记录,故inverse=false<br>当向stu_cou中写入记录时候,<br>/*<br>* 测试目的:在stu_cou中添加一组对应关系(student,course)<br>* 测试结果:当执行完该段代码后<br>* 如stu_cou中没有(student,course)时,自动加入(student,course)<br>* 如stu_cou中存在(student,course)时,什么都不作,不会存在主健冲突<br>Student student=(Student)session.load(Student.class,student2.getId())<br>Course course=(Course)session.load(Course.class,course2.getId());<br>student.getCourses().add(course);<br>session.save(student);<br>执行完后,hibenrate成功显示<br>Hibernate: select course0_.id as id0_, course0_.name as name0_ from course course0_ where course0_.id=?<br>Hibernate: select student0_.id as id0_, student0_.name as name0_ from student student0_ where student0_.id=?<br>Hibernate: select courses0_.cid as cid__, courses0_.sid as sid__ from stu_cou courses0_ where courses0_.sid=?<br>Hibernate: insert into stu_cou (sid, cid) values (?, ?)<br>b) 删除记录的情况<br>Student student=(Student)session.load(Student.class,student2.getId())<br>Course course=(Course)session.load(Course.class,course2.getId());<br>student.getCourses().让remove(course);<br>session.save(student);<br>代码执行完后,可以成功执行,hibernate显示:<br>Hibernate: select course0_.id as id0_, course0_.name as name0_ from course course0_ where course0_.id=?<br>Hibernate: select student0_.id as id0_, student0_.name as name0_ from student student0_ where student0_.id=?<br>Hibernate: select courses0_.cid as cid__, courses0_.sid as sid__ from stu_cou courses0_ where courses0_.sid=?<br>Hibernate: delete from stu_cou where sid=?</p>
<p>2) inverse=true的情况<br>student.hbm.xml是被动方,设置它的inverse=true<br>a) 添加记录情况<br>* 测试目的:在stu_cou中添加一组对应关系(student,course)<br>* 测试结果:当执行完该段代码后<br>* 如stu_cou中没有(student,course)时,自动加入(student,course)<br>* 如stu_cou中存在(student,course)时,什么都不作,不会存在主健冲突<br>Student tudent=(Student)session.load(Student.class,student2.getId());<br>Course course=(Course)session.load(Course.class,course2.getId());<br>student.getCourses().add(course);<br>session.save(student);<br>代码执行完后,hibernate显示,没有插入记录<br>Hibernate: select course0_.id as id0_, course0_.name as name0_ from course course0_ where course0_.id=?<br>Hibernate: select student0_.id as id0_, student0_.name as name0_ from student student0_ where student0_.id=?<br>Hibernate: select courses0_.cid as cid__, courses0_.sid as sid__ from stu_cou courses0_ where courses0_.sid=?</p>
<p>b) 删除记录情况<br>当向stu_cou中删除记录时候,<br>Student student=(Student)session.load(Student.class,student2.getId())<br>Course course= (Course)session.load(Course.class,course2.getId());<br>student.getCourses().remove(course);<br>session.save(student);<br>执行完,hibernate显示如下,证明没有删除记录<br>Hibernate: select course0_.id as id0_, course0_.name as name0_ from course course0_ where course0_.id=?<br>Hibernate: select student0_.id as id0_, student0_.name as name0_ from student student0_ where student0_.id=?<br>Hibernate: select courses0_.cid as cid__, courses0_.sid as sid__ from stu_cou courses0_ where courses0_.sid=?<br>Hibernate: select course0_.id as id0_, course0_.name as name0_ from course course0_ where course0_.id=?<br>Hibernate: select course0_.id as id0_, course0_.name as name0_ from course course0_ where course0_.id=?</p>
<p>2. 分析cascade参数(student.hbm.xml course.hbm.xml)<br>只有设置了inverse=false才可以操纵stu_cou的数据<br>当inverse=true的时候,下列情况1中b)提示错误<br>原因: stu_cou中存在外健约束,你无法直接删除student表中的数据<br>1) cascade=save-update的情况<br>a)添加记录情况<br>Set set=new HashSet();<br>set.add(course1);<br>set.add(course2);<br>set.add(course3);<br>student3.setCourses(set);<br>session.save(student3);<br>执行后hibernate提示:<br>Hibernate: insert into student (name) values (?)<br>Hibernate: insert into course (name) values (?)<br>Hibernate: insert into course (name) values (?)<br>Hibernate: insert into course (name) values (?)<br>Hibernate: insert into stu_cou (sid, cid) values (?, ?)<br>可见,插入了student,course,和stu_cou三个表</p>
<p>b)删除记录情况<br>Student student=(Student)session.load(Student.class,new Integer(22));<br>session.delete(student);<br>成功执行后,hibernate提示:<br>Hibernate: select student0_.id as id0_, student0_.name as name0_ from student student0_ where student0_.id=?<br>Hibernate: delete from stu_cou where sid=?<br>Hibernate: delete from student where </p>
<p>2) cascade=all的情况<br>a) 添加记录情况<br>Set set=new HashSet();<br>set.add(course1);<br>set.add(course2);<br>set.add(course3);<br>student3.setCourses(set);<br>session.save(student3);<br>执行后hibernate提示:成功添加级联关系<br>Hibernate: insert into student (name) values (?)<br>Hibernate: insert into course (name) values (?)<br>Hibernate: insert into course (name) values (?)<br>Hibernate: insert into course (name) values (?)<br>Hibernate: insert into stu_cou (sid, cid) values (?, ?)<br>b) 删除记录情况<br>Student student=(Student)session.load(Student.class,new Integer(22));<br>session.delete(student);<br>测试结果(所有和student(22)相关的记录均被删除)<br>删除student表中的student(22)记录<br>删除stu_cou表中的student(22)的记录<br>删除course表中的student(22)对应的course的记录</p>
<p>2) cascade=none情况<br>a) 添加记录情况<br>Set set=new HashSet();<br>set.add(course1);<br>set.add(course2);<br>set.add(course3);<br>student3.setCourses(set);<br>session.save(student3);<br>失败,因为没有级联关系,所以course记录添加不进去,造成外健失败</p>
<p>b) 删除记录情况<br>Student student=(Student)session.load(Student.class,new Integer(22));<br>session.delete(student);<br>正常,删除了student表中的记录student(22),同时删除了stu_cou中对应的student(22)的记录</p>
<p>&nbsp;</p>
<p>附录<br>1．MiddleGen的使用<br>1环境变量设置<br>%Hibernate_Home%/lib/*.jar到%MiddleGen%/lib下<br>%Hibernate_Home%/hibernate2.jar到%MiddleGen%/lib下<br>%Hibernate-Extension_Home%/tools/lib/*.jar到%MiddleGen%/lib下<br>%Hibernate-Extension_Home%/tools/hibernate-tools.jar到%MiddleGen%/lib下</p>
<p>2 MiddleGen的配置<br>配置目标数据库参数<br>进入MiddleGen 目录下的\config\database 子目录，根据我们实际采用的数据库打开对应的配置文件。如这里我用的是mysql数据库，对应的就是mysql.xml文件。<br>&lt;property<br>name="database.script.file" value="${src.dir}/sql/${name}-mysql.sql"/&gt;<br>&lt;property <br>name="database.driver.file" value="${lib.dir}/mysql.jar"/&gt;<br>&lt;property<br>name="database.driver.classpath" value="${database.driver.file}"/&gt;<br>&lt;property<br>name="database.driver" value="org.gjt.mm.mysql.Driver"/&gt;<br>&lt;property<br>name="database.url" value="jdbc:mysql://localhost/sample"/&gt;<br>&lt;property<br>name="database.userid" value="user"/&gt;<br>&lt;property<br>name="database.password" value="mypass"/&gt;<br>&lt;property<br>name="database.schema" value=""/&gt;<br>&lt;property<br>name="database.catalog" value=""/&gt;<br>&lt;property<br>name="jboss.datasource.mapping" value="mySQL"/&gt;<br>其中下划线标准的部分是我们进行配置的内容，分别是数据url以及数据库用<br>户名和密码。</p>
<p>1) 修改Build.xml<br>修改MiddleGen 根目录下的build.xml 文件，此文件是Middlegen-Hibernate 的Ant构建配置。Middlegen-Hibernate将根据build.xml文件中的具体参数生成数据库表映射文件。可配置的项目包括：</p>
<p>a) 目标数据库配置文件地址<br>查找关键字 &#8221;!ENTITY&#8221;，得到：<br>&lt;!DOCTYPE project [<br>&lt;!ENTITY database SYSTEM<br>"file:./config/database/hsqldb.xml"&gt;<br>]&gt;<br>默认情况下，MiddleGen 采用的是hsqldb.xml，<br>将其修改为我们所用的数据库配置文件（mysql.xml）：<br>&lt;!DOCTYPE project [<br>&lt;!ENTITY database SYSTEM<br>"file:./config/database/mysql.xml"&gt;<br>]&gt;</p>
<p>b) Application name<br>查找：<br>&lt;property value="airline"/&gt;<br>&#8220;aireline&#8221;是MiddleGen原始配置中默认的 Application Name，将其修改为我们<br>所希望的名称，如&#8220;HibernateSample&#8221;：<br>&lt;property value="HibernateSample"/&gt;</p>
<p>c) 输出目录<br>查找关键字&#8220;name="build.gen-src.dir"&#8221;，得到：<br>&lt;property <br>value="${build.dir}/gen-src"/&gt;<br>修改value="${build.dir}/gen-src"使其指向我们所期望的输出目录，<br>这里我们修改为：&lt;property <br>value="C:\sample"/&gt;<br>d) 对应代码的Package name<br>查找关键字&#8220;destination&#8221;，得到：<br>&lt;hibernate<br>destination="${build.gen-src.dir}"<br>package="${name}.hibernate"<br>genXDocletTags="false"<br>genIntergratedCompositeKeys="false"<br>javaTypeMapper=<br>"middlegen.plugins.hibernate.HibernateJavaTypeMapper"<br>/&gt;<br>可以看到，hibernate 节点package 属性的默认设置实际上是由前面的<br>Application Name （${name}）和&#8220;.hibernate&#8221;组合而成，根据我们的需要，<br>这里还有一个属性genXDocletTags，如果设置为true，则生成的代码将包含<br>xdoclet tag，这为以后在开发过程中借助xdoclet进行映射调整提供了帮助。注意，如果使用的数据库为SQLServer，需要将build.xml部分删除(参见夏?^的文档)，否则Middlegen会报出找不到表的错误。<br>至此为止，MiddleGen 已经配置完毕，在MiddleGen 根目录下运行ant，就将出现<br>MiddleGen的界面：<br>2 *.xml到.java<br>在得到.xml后,使用ant hbm2java 即可得到相应的java文件<br>总结:<br>我第一次运行成功,但是后来则出现如下类似错误,检查发现在MiddleGen的lib目录下存在多个类似的jar包<br>(velocity.jar,velocity1-0.8.jar),删除旧的jar包后,问题解决<br>[middlegen] java.lang.IncompatibleClassChangeError<br>[middlegen] at [middlegen] at org.apache.velocity.Template.process(Template.java:136)</p>
<p><br>2．(评论)关于Many-to-Many需要注意的问题<br>比如group/user,user/role,group/role,都是多对多的关系。<br>所以有必要搞搞清楚。以group/user为例<br>(1)group.getUsers() &amp; user.getGroups()双向映射。在这里，考虑到user.setGroups()是不会放出来的。所以在user端加入inverse=true，就是说，让Group端来维护两者的关系表。<br>(2)于是，添加的时候，要把user加入到group的user set中，保存，才有意义！<br>(3)如果要删除group_user的关系，而不是group/user本身，那么，需要在group端设定casade=save-update。否则，group.getUsers().remove(user)不会起作用！<br>(4)由于有了FK保护，你是无法单独删除User的。同时，由于你指定了inverse=true,所以无法光光的设置user.getGroups().clear就能够搞定一切。你必须首先在每个Group.getUsers中删除这个user，才能最后删除。所以，UserManagerImpl代码可能看起来像这样：<br>public void delUser(User user) {<br>if (user == null) {<br>logger.info("Got a null user object!");<br>return;<br>}<br>Session session = null;<br>Transaction trans = null;<br>boolean bSuccess = false;<br>try {<br>String username = new String(user.getName());<br>session = PLUtils.getSession();<br>trans = session.beginTransaction();<br>session.update(user);</p>
<p>Set groups = ((UserEntity)user).getGroups(); <br>for(Iterator i = groups.iterator();i.hasNext();){<br>GroupEntity g = (GroupEntity)i.next();<br>session.update(g);<br>g.getUsers().remove(user);<br>session.update(g);<br>}</p>
<p>// ((UserEntity)user).getGroups().clear();<br>session.delete(user);<br>bSuccess = true;<br>logger.debug("User " + username + " deleted ok!");<br>}<br>catch (ObjectNotFoundException onfe) {<br>logger.info("user not found!", onfe);<br>}<br>catch (Exception e) {<br>logger.error("failed!", e);<br>}<br>finally {<br>try {<br>if (bSuccess)<br>trans.commit();<br>else<br>trans.rollback();<br>session.close();<br>}<br>catch (Exception ex) {<br>}<br>}<br>}</p>
<img src ="http://www.blogjava.net/junky/aggbug/111963.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2007-04-19 15:58 <a href="http://www.blogjava.net/junky/archive/2007/04/19/111963.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 深入研究之 Criteria (转)</title><link>http://www.blogjava.net/junky/archive/2007/03/16/104228.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Fri, 16 Mar 2007 05:34:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2007/03/16/104228.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/104228.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2007/03/16/104228.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/104228.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/104228.html</trackback:ping><description><![CDATA[
		<div class="con_sample">
				<p>最近在项目中使用 Spring 和 Hibernate 进行开发，有感于 Criteria 比较好用，在查询方法设计上可以灵活的根据 Criteria 的特点来方便地进行查询条件的组装。所以现在对 Hibernate 的 Criteria 深入研究一下。《 Hibernate Reference 》及网上其它一些资料对 Criteria 已经做了很多介绍。</p>
		</div>
		<div class="con_all">
				<p>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">最近在项目中使用</span>
						<span lang="EN-US">Spring</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">Hibernate</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行开发，有感于</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">比较好用，在查询方法设计上可以灵活的根据</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的特点来方便地进行查询条件的组装。所以现在对</span>
						<span lang="EN-US">Hibernate</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">深入研究一下。《</span>
						<span lang="EN-US">Hibernate Reference</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">》及网上其它一些资料对</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">已经做了很多介绍。本文主要是从</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的结构入手来进行分析。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<span style="mso-tab-count: 1">       </span>
						</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如图</span>
						<span lang="EN-US">1</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span>
						<span lang="EN-US">Hibernate</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设计了</span>
						<span lang="EN-US">CriteriaSpecification</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作为</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的顶级接口，其下面提供了</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">DetachedCriteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">DetachedCriteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的主要区别在于创建的形式不一样，</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是在线的，所以它是由</span>
						<span lang="EN-US">Hibernate Session</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行创建的；而</span>
						<span lang="EN-US">DetachedCriteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是离线的，创建时无需</span>
						<span lang="EN-US">Session</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</span>
						<span lang="EN-US">DetachedCriteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供了</span>
						<span lang="EN-US">4</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个静态方法</span>
						<span lang="EN-US">forClass(Class)</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">或</span>
						<span lang="EN-US">forEntityName(Name)</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行</span>
						<span lang="EN-US">DetachedCriteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实例的创建。</span>
						<span lang="EN-US">Spring</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的框架提供了</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">getHibernateTemplate().findByCriteria(detachedCriteria)</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法可以很方便地根据</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">DetachedCriteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来返回查询结果。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如图</span>
						<span lang="EN-US">1</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">DetachedCriteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">均可使用</span>
						<span lang="EN-US">Criterion</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">Projection</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设置查询条件。可以设置</span>
						<span lang="EN-US">FetchMode(</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">联合查询抓取的模式</span>
						<span lang="EN-US">)</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，设置排序方式。对于</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">还可以设置</span>
						<span lang="EN-US">FlushModel</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（冲刷</span>
						<span lang="EN-US">Session</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的方式）和</span>
						<span lang="EN-US">LockMode</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（数据库锁模式）。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面就对</span>
						<span lang="EN-US">Criterion</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">Projection</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行详细说明。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<span style="mso-tab-count: 1">       <img alt="" src="http://www.blogjava.net/images/blogjava_net/faith/13332/o_Criteria.JPG" /></span>
								<?XML:NAMESPACE PREFIX = V /?>
								<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
										<v:stroke joinstyle="miter">
										</v:stroke>
										<v:formulas>
												<v:f eqn="if lineDrawn pixelLineWidth 0">
												</v:f>
												<v:f eqn="sum @0 1 0">
												</v:f>
												<v:f eqn="sum 0 0 @1">
												</v:f>
												<v:f eqn="prod @2 1 2">
												</v:f>
												<v:f eqn="prod @3 21600 pixelWidth">
												</v:f>
												<v:f eqn="prod @3 21600 pixelHeight">
												</v:f>
												<v:f eqn="sum @0 0 1">
												</v:f>
												<v:f eqn="prod @6 1 2">
												</v:f>
												<v:f eqn="prod @7 21600 pixelWidth">
												</v:f>
												<v:f eqn="sum @8 21600 0">
												</v:f>
												<v:f eqn="prod @7 21600 pixelHeight">
												</v:f>
												<v:f eqn="sum @10 21600 0">
												</v:f>
										</v:formulas>
										<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect">
										</v:path>
										<?XML:NAMESPACE PREFIX = O /?>
										<o:lock v:ext="edit" aspectratio="t">
										</o:lock>
								</v:shapetype>
						</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center">
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</span>
						<span lang="EN-US">1</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<o:p> </o:p>
						</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<span style="mso-tab-count: 1">       </span>Criterion</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的查询条件。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供了</span>
						<span lang="EN-US">add(Criterion criterion)</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法来添加查询条件。图</span>
						<span lang="EN-US">2</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是</span>
						<span lang="EN-US">Criterion</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的结构图。</span>
						<span lang="EN-US">Criterion</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">接口的主要实现包括：</span>
						<span lang="EN-US">Example</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</span>
						<span lang="EN-US">Junction</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">SimpleExpression</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。而</span>
						<span lang="EN-US">Junction</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的实际使用是它的两个子类</span>
						<span lang="EN-US">conjunction</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">disjunction</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，分别是使用</span>
						<span lang="EN-US">AND</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">OR</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">操作符进行来联结查询条件集合。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
						<span lang="EN-US">Criterion</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的实例可以通过</span>
						<span lang="EN-US">Restrictions</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">工具类来创建，</span>
						<span lang="EN-US">Restrictions</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供了大量的静态方法，如</span>
						<span lang="EN-US">eq</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（等于）、</span>
						<span lang="EN-US">ge</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（大于等于）、</span>
						<span lang="EN-US">between</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等来方法的创建</span>
						<span lang="EN-US">Criterion</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">查询条件</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</span>
						<span lang="EN-US">SimpleExpression</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实例）。除此之外，</span>
						<span lang="EN-US">Restrictions</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">还提供了方法来创建</span>
						<span lang="EN-US">conjunction</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">disjunction</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实例，通过往该实例的</span>
						<span lang="EN-US">add(Criteria)</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法来增加查询条件形成一个查询条件集合。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">至于</span>
						<span lang="EN-US">Example</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的创建有所不同，</span>
						<span lang="EN-US">Example</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">本身提供了一个静态方法</span>
						<span lang="EN-US">create(Object entity)</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，即根据一个对象（实际使用中一般是映射到数据库的对象）来创建。然后可以设置一些过滤条件：</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
						<span lang="EN-US">Example exampleUser =Example.create(u)</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 126pt">
						<span lang="EN-US">.ignoreCase() //</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">忽略大小写</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 105pt; TEXT-INDENT: 21pt">
						<span lang="EN-US">.enableLike(MatchMode.ANYWHERE);</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 105pt">
						<span lang="EN-US">//</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对</span>
						<span lang="EN-US">String</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类型的属性，无论在那里值在那里都匹配。相当于</span>
						<span lang="EN-US">%value%</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
						</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center">
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">
								<img alt="" src="http://www.blogjava.net/images/blogjava_net/faith/13332/o_Criterion.JPG" />
								<br />图</span>
						<span lang="EN-US">2</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<span style="mso-tab-count: 1">       </span>
						</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
						<span lang="EN-US">Project </span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">主要是让</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">能够进行报表查询，并可以实现分组。</span>
						<span lang="EN-US">Project</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">主要有</span>
						<span lang="EN-US">SimpleProjection</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</span>
						<span lang="EN-US">ProjectionList</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">Property</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">三个实现。其中</span>
						<span lang="EN-US">SimpleProjection</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
						<span lang="EN-US">ProjectionList</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的实例化是由内建的</span>
						<span lang="EN-US">Projections</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来完成，如提供的</span>
						<span lang="EN-US">avg</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</span>
						<span lang="EN-US">count</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</span>
						<span lang="EN-US">max</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</span>
						<span lang="EN-US">min</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</span>
						<span lang="EN-US">sum</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可以让开发者很容易对某个字段进行统计查询。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<span style="mso-tab-count: 1">       </span>Property</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是对某个字段进行查询条件的设置，如通过</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<span style="mso-tab-count: 1">      </span>Porperty.forName(“color”).in(new String[]{“black”,”red”,”write”});</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">则可以创建一个</span>
						<span lang="EN-US">Project</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实例。通过</span>
						<span lang="EN-US">criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span>
						<span lang="EN-US">add(Project)</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法加入到查询条件中去。</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
						</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center">
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">
								<img alt="" src="http://www.blogjava.net/images/blogjava_net/faith/13332/o_Projection.JPG" />
								<br />图</span>
						<span lang="EN-US">3</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<span style="mso-tab-count: 1">       </span>
						</span>
				</p>
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
						<span lang="EN-US">
								<span style="mso-tab-count: 1">       </span>
						</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使用</span>
						<span lang="EN-US">Criteria</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行查询，主要要清晰的是</span>
						<span lang="EN-US">Hibernate</span>
						<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供了那些类和方法来满足开发中查询条件的创建和组装，其结构层次如何。这样使用起来便可得心应手。</span>
				</p> </div>
<img src ="http://www.blogjava.net/junky/aggbug/104228.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2007-03-16 13:34 <a href="http://www.blogjava.net/junky/archive/2007/03/16/104228.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>struts+hibernate项目debug总结</title><link>http://www.blogjava.net/junky/archive/2007/03/09/102897.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Fri, 09 Mar 2007 10:12:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2007/03/09/102897.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/102897.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2007/03/09/102897.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/102897.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/102897.html</trackback:ping><description><![CDATA[
		<div class="entryBody">
				<span class="tpc_content">
						<font size="2">转：<br /><br />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 or ActionformBeans collection<br />原因是：web.xml文件中没有配置struts-config.xml的相关信息。<br /><br /><br />============================================================<br /><br />org.apache.jasper.JasperException: The absolute uri: </font>
						<a href="http://java.sun.com/jstl/core" target="_blank">
								<font color="#0e4d8b" size="2">http://java.sun.com/jstl/core</font>
						</a>
						<font size="2"> cannot be resolved in either web.xml or the jar files deployed with this application<br /><br /><br />缺少jstl的相关jar和web.xml配置<br />jstl.jar<br /><taglib><br />  <taglib-uri></taglib-uri></taglib></font>
						<a href="http://java.sun.com/jstl/fmt&lt;/taglib-uri&gt;" target="_blank">
								<font size="2">
										<font color="#0e4d8b">http://java.sun.com/jstl/fmt</font>
								</font>
						</a>
						<br />
						<font size="2">  <taglib-location>/WEB-INF/fmt.tld</taglib-location><br /><br /><br /><taglib><br />  <taglib-uri></taglib-uri></taglib></font>
						<a href="http://java.sun.com/jstl/fmt-rt&lt;/taglib-uri&gt;" target="_blank">
								<font size="2">
										<font color="#0e4d8b">http://java.sun.com/jstl/fmt-rt</font>
								</font>
						</a>
						<br />
						<font size="2">  <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location><br /><br /><br /><taglib><br />  <taglib-uri></taglib-uri></taglib></font>
						<a href="http://java.sun.com/jstl/core&lt;/taglib-uri&gt;" target="_blank">
								<font size="2">
										<font color="#0e4d8b">http://java.sun.com/jstl/core</font>
								</font>
						</a>
						<br />
						<font size="2">  <taglib-location>/WEB-INF/c.tld</taglib-location><br /><br /><br /><taglib><br />  <taglib-uri></taglib-uri></taglib></font>
						<a href="http://java.sun.com/jstl/core-rt&lt;/taglib-uri&gt;" target="_blank">
								<font size="2">
										<font color="#0e4d8b">http://java.sun.com/jstl/core-rt</font>
								</font>
						</a>
						<br />
						<font size="2">  <taglib-location>/WEB-INF/c-rt.tld</taglib-location><br /><br /><br /><taglib><br />  <taglib-uri></taglib-uri></taglib></font>
						<a href="http://java.sun.com/jstl/sql&lt;/taglib-uri&gt;" target="_blank">
								<font size="2">
										<font color="#0e4d8b">http://java.sun.com/jstl/sql</font>
								</font>
						</a>
						<br />
						<font size="2">  <taglib-location>/WEB-INF/sql.tld</taglib-location><br /><br /><br /><taglib><br />  <taglib-uri></taglib-uri></taglib></font>
						<a href="http://java.sun.com/jstl/sql-rt&lt;/taglib-uri&gt;" target="_blank">
								<font size="2">
										<font color="#0e4d8b">http://java.sun.com/jstl/sql-rt</font>
								</font>
						</a>
						<br />
						<font size="2">  <taglib-location>/WEB-INF/sql-rt.tld</taglib-location><br /><br /><br /><taglib><br />  <taglib-uri></taglib-uri></taglib></font>
						<a href="http://java.sun.com/jstl/x&lt;/taglib-uri&gt;" target="_blank">
								<font size="2">
										<font color="#0e4d8b">http://java.sun.com/jstl/x</font>
								</font>
						</a>
						<br />
						<font size="2">  <taglib-location>/WEB-INF/x.tld</taglib-location><br /><br /><br /><taglib><br />  <taglib-uri></taglib-uri></taglib></font>
						<a href="http://java.sun.com/jstl/x-rt&lt;/taglib-uri&gt;" target="_blank">
								<font size="2">
										<font color="#0e4d8b">http://java.sun.com/jstl/x-rt</font>
								</font>
						</a>
						<br />
						<font size="2">  <taglib-location>/WEB-INF/x-rt.tld</taglib-location><br /><br /><br /><br />==========================================================<br /><br />Failed to load or instantiate TagLibraryValidator class: org.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 "column" associated with an element type "key" must be followed by the ' = ' character.<br />[ERROR] Configuration - Could not configure datastore from input stream <org.dom4j.documentexception: name=""><br ?key?="" type="" element="" an="" with="" associated="" ?column?="" attribute="" :="" document="" of="" 18="" line="" on="" error="" /><br />must be followed by the ' = ' character. Nested exception: Attribute name "column" associated with an element type "key" must be followed by the ' = ' character.&gt;org.dom4j.DocumentException: Error <br /><br />on line 18 of document : Attribute name "column" associated with an element type "key" must be followed by the ' = ' character. Nested exception: Attribute name "column" associated with an element <br /><br />type "key" must be followed by the ' = ' character.<br /><br /><br /><br />xml 语法错误，key语法中应该类似这样的写法 <key column="id"><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: org.hibernate.id.IncrementGenerator<br />org.hibernate.PropertyValueException: not-null property references a null or transient value: com.xxx.yyy.company<br />at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)<br /><br /><many one="" to="">中的设置应该设置为not-null="false" ，设置为not-null="true"则报以上错误<br />================================================================<br /><br /><br />org.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 />org.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设置了以后，仍然在其中设置相冲突的<property>属性。<br /><br />==========================================<br /><br />[WARN] RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException<br />必须将使用到的对象new起来。<br /><br />============================================<br />GROUP BY 表达式的查询必须满足如下：<br />select 子句后的每一项必需出现在group by 子句中，除非该项使用了聚集函数。<br /><br />===========================================<br />org.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 <org.hibernate.objectnotfoundexception: with="" [com.xxx.yyy.company#0]<br="" exists:="" identifier="" given="" the="" row="" no="" /><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'll need to mannally 'Redeploy' the project to update 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 />org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed<br />解决办法。<br />cmpy=(CompanyEdit)ss.load(CompanyEdit.class,companyId1);<br />  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 - ORA-00904: 无效列名<br /><br />[INFO] DefaultLoadEventListener - Error performing load command <org.hibernate.exception.sqlgrammarexception: an="" [com.xxx.yyy.sellinfo#1]<br="" entity:="" load="" not="" could="" /><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 - org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.xxx.yyy.SellComment#7]<br /><br />原因：父亲对象（one方）设置cascade="save-update" 时，直接删除子对象时，会报错，<br />处理方法：save信息需要级联操作，delete时候也要用相同的原理。<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.delete(sellComment);//删除儿子<br />session.flush();</property></many></key></org.dom4j.documentexception:></font>
				</span>
				<br />
		</div>
<img src ="http://www.blogjava.net/junky/aggbug/102897.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2007-03-09 18:12 <a href="http://www.blogjava.net/junky/archive/2007/03/09/102897.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>延迟初始化错误（ERROR LazyInitializer）是如何产生的?(转)</title><link>http://www.blogjava.net/junky/archive/2007/03/08/102649.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Thu, 08 Mar 2007 10:06:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2007/03/08/102649.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/102649.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2007/03/08/102649.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/102649.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/102649.html</trackback:ping><description><![CDATA[
		<font color="#800080">延迟初始化错误（ERROR LazyInitializer）是如何产生的?<br /><br />选自&lt;&lt;精通Hibernate：Java对象持久化技术详解&gt;&gt; 作者：孙卫琴 来源:www.javathinker.org<br />如果转载，请标明出处，谢谢<br /><br /><br />延迟初始化错误是运用Hibernate开发项目时最常见的错误。如果对一个类或者集合配置了延迟检索策略，那么必须当代理类实例或代理集合处于持久化状态（即处于Session范围内）时，才能初始化它。如果在游离状态时才初始化它，就会产生延迟初始化错误。<br /><br />下面把Customer.hbm.xml文件的&lt;class&gt;元素的lazy属性设为true，表示使用延迟检索策略：<br /><br />&lt;class name="mypack.Customer" table="CUSTOMERS" lazy="true"&gt;<br /><br />当执行Session的load()方法时，Hibernate不会立即执行查询CUSTOMERS表的select语句，仅仅返回Customer类的代理类的实例，这个代理类具由以下特征：<br /><br />（1） 由Hibernate在运行时动态生成，它扩展了Customer类，因此它继承了Customer类的所有属性和方法，但它的实现对于应用程序是透明的。<br />（2） 当Hibernate创建Customer代理类实例时，仅仅初始化了它的OID属性，其他属性都为null，因此这个代理类实例占用的内存很少。<br />（3）当应用程序第一次访问Customer代理类实例时（例如调用customer.getXXX()或customer.setXXX()方法）， Hibernate会初始化代理类实例，在初始化过程中执行select语句，真正从数据库中加载Customer对象的所有数据。但有个例外，那就是当应用程序访问Customer代理类实例的getId()方法时，Hibernate不会初始化代理类实例，因为在创建代理类实例时OID就存在了，不必到数据库中去查询。<br /><br />提示：Hibernate采用CGLIB工具来生成持久化类的代理类。CGLIB是一个功能强大的Java字节码生成工具，它能够在程序运行时动态生成扩展 Java类或者实现Java接口的代理类。关于CGLIB的更多知识，请参考：http://cglib.sourceforge.net/。<br /><br />以下代码先通过Session的load()方法加载Customer对象，然后访问它的name属性： <br /><br />tx = session.beginTransaction();<br />Customer customer=(Customer)session.load(Customer.class,new Long(1));<br />customer.getName();<br />tx.commit();<br /><br />在运行session.load()方法时Hibernate不执行任何select语句，仅仅返回Customer类的代理类的实例，它的OID为1，这是由load()方法的第二个参数指定的。当应用程序调用customer.getName()方法时，Hibernate会初始化Customer代理类实例，从数据库中加载Customer对象的数据，执行以下select语句：<br /><br />select * from CUSTOMERS where ID=1;<br />select * from ORDERS where CUSTOMER_ID=1;<br /><br />当&lt;class&gt;元素的lazy属性为true，会影响Session的load()方法的各种运行时行为，下面举例说明。<br /><br />1．如果加载的Customer对象在数据库中不存在，Session的load()方法不会抛出异常，只有当运行customer.getName()方法时才会抛出以下异常：<br /><br />ERROR LazyInitializer:63 - Exception initializing proxy<br />net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 1, of class: <br />mypack.Customer<br /><br />2．如果在整个Session范围内，应用程序没有访问过Customer对象，那么Customer代理类的实例一直不会被初始化，Hibernate不会执行任何select语句。以下代码试图在关闭Session后访问Customer游离对象：<br /><br />tx = session.beginTransaction();<br />Customer customer=(Customer)session.load(Customer.class,new Long(1));<br />tx.commit();<br />session.close();<br />customer.getName();<br /><br />由于引用变量customer引用的Customer代理类的实例在Session范围内始终没有被初始化，因此在执行customer.getName()方法时，Hibernate会抛出以下异常：<br /><br />ERROR LazyInitializer:63 - Exception initializing proxy<br />net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed<br /><br />由此可见，Customer代理类的实例只有在当前Session范围内才能被初始化。<br /><br />3．net.sf.hibernate.Hibernate类的initialize()静态方法用于在Session范围内显式初始化代理类实例，isInitialized()方法用于判断代理类实例是否已经被初始化。例如：<br /><br />tx = session.beginTransaction();<br />Customer customer=(Customer)session.load(Customer.class,new Long(1));<br />if(!Hibernate.isInitialized(customer)) <br />Hibernate.initialize(customer);<br />tx.commit();<br />session.close();<br />customer.getName();<br /><br />以上代码在Session范围内通过Hibernate类的initialize()方法显式初始化了Customer代理类实例，因此当Session关闭后，可以正常访问Customer游离对象。<br /><br />4．当应用程序访问代理类实例的getId()方法时，不会触发Hibernate初始化代理类实例的行为，例如：<br /><br />tx = session.beginTransaction();<br />Customer customer=(Customer)session.load(Customer.class,new Long(1));<br />customer.getId();<br />tx.commit();<br />session.close();<br />customer.getName();<br /><br />当应用程序访问customer.getId()方法时，该方法直接返回Customer代理类实例的OID值，无需查询数据库。由于引用变量 customer始终引用的是没有被初始化的Customer代理类实例，因此当Session关闭后再执行customer.getName()方法， Hibernate会抛出以下异常：<br /><br />ERROR LazyInitializer:63 - Exception initializing proxy<br />net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed</font>
<img src ="http://www.blogjava.net/junky/aggbug/102649.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2007-03-08 18:06 <a href="http://www.blogjava.net/junky/archive/2007/03/08/102649.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate Tag 参考手册（中文翻译版1.0） (转)</title><link>http://www.blogjava.net/junky/archive/2007/02/27/100900.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Tue, 27 Feb 2007 03:39:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2007/02/27/100900.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/100900.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2007/02/27/100900.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/100900.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/100900.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: @hibernate Tag参考用来声明Hibernate描述文件的Tags：Class层次Tags @hibernate.class @hibernate.subclass @hibernate.discriminator @hibernate.joined-subclass @hibernate.joined-subclass-key @hibernate.cache ...&nbsp;&nbsp;<a href='http://www.blogjava.net/junky/archive/2007/02/27/100900.html'>阅读全文</a><img src ="http://www.blogjava.net/junky/aggbug/100900.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2007-02-27 11:39 <a href="http://www.blogjava.net/junky/archive/2007/02/27/100900.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Could not initialize proxy - the owning Session was closed </title><link>http://www.blogjava.net/junky/archive/2006/12/25/89917.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Mon, 25 Dec 2006 07:54:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2006/12/25/89917.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/89917.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2006/12/25/89917.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/89917.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/89917.html</trackback:ping><description><![CDATA[Hibernate类的initialize()静态方法用于在Session范围内显式初始化代理类实例，isInitialized()方法用于判断代理类实例是否已经被初始化。例如： 
<p><font color="#000000"><span style="COLOR: #003366">tx = session.beginTransaction(); <br />Customer customer=(Customer)session.load(Customer.class,new Long(1)); <br />if(!Hibernate.isInitialized(customer)) <br />Hibernate.initialize(customer); <br />tx.commit(); <br />session.close(); <br />customer.getName();</span></font></p><p><span style="COLOR: #003366"><font color="#000000">以上代码在Session范围内通过Hibernate类的initialize()方法显式初始化了Customer代理类实例，因此当Session关闭后，可以正常访问Customer游离对象。</font></span></p><img src ="http://www.blogjava.net/junky/aggbug/89917.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2006-12-25 15:54 <a href="http://www.blogjava.net/junky/archive/2006/12/25/89917.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Distributed Objects With Hibernate</title><link>http://www.blogjava.net/junky/archive/2006/10/17/75604.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Tue, 17 Oct 2006 06:09:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2006/10/17/75604.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/75604.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2006/10/17/75604.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/75604.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/75604.html</trackback:ping><description><![CDATA[
		<div class="diaryContent" id="diary313185" style="DISPLAY: block">
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 14pt; MARGIN: 0in; FONT-FAMILY: tahoma">Distributed Objects With Hibernate</p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; MARGIN: 0in; FONT-FAMILY: tahoma">Problem:</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">You are building a three tier distributed system and you are passing persistent objects between tiers. When you send the objects back to the server, you want to save the only the changes.</p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; MARGIN: 0in; FONT-FAMILY: tahoma">Solutions:</p>
				<br />
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">1)Hibernate added the select-before-update option in 2.1. Setting this option on your class will cause Hibernate to load the data from the database and compare that to the data in your object to determine what has changed. It makes use of a version or timestamp field to enforce optimistic concurrency. This works well but introduces additional database access that could effect performance.</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">2)Save the original data for each persistent object and implement a custom EntityPersister to return the original state to Hibernate instead of accessing the state in the database.</p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; MARGIN: 0in; FONT-FAMILY: tahoma">Implementing Solution 2:</p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; MARGIN: 0in; FONT-FAMILY: tahoma">Step 1: Save the original state for each persistent object</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">The easiest way to implement this is to have each persistent object remember the original values of any properties that changed. If you make all your persistent objects proper JavaBeans, you can capture this in the base class when the object fires a propertyChanged event:</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">public abstract class AbstractDomainObject implements Serializable {<br />private PropertyChangeSupport support = new PropertyChangeSupport(this);<br />private Map originalValues = new HashMap();<br />public AbstractDomainObject() {<br />super();<br />}<br />public Map getOriginalState() {<br />return originalValues;<br />}<br />public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {<br />support.addPropertyChangeListener(propertyChangeListener);<br />}<br />protected void firePropertyChange(String property, Object oldValue, Object newValue) {<br />if (!originalValues.containsKey(property)) {<br />originalValues.put(property, oldValue);<br />}<br />support.firePropertyChange(property, oldValue, newValue);<br />}<br />}<br /></p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; MARGIN: 0in; FONT-FAMILY: tahoma">Step 2: Create your domain objects</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">A domain object might look like follows. Because it fires propertyChange events, the base class will remember original values of properties that changed. Note that you will want to map the properties as access=field since the setter fires a propertyChange event when called.</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">public class Customer extends AbstractDomainObject {<br />private String name;<br />public String getName() {<br />return name;<br />}<br />public void setName(String name) {<br />String oldValue = this.name;<br />this.name = name;<br />firePropertyChange("name", oldValue, name);<br />}<br />}<br /></p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; MARGIN: 0in; FONT-FAMILY: tahoma">Step 3: Create a custome entity persister</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">Create a custom entity persister and override getCurrentPersistentState. Set the persister for each class to this custom persister.</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">public Object[] getCurrentPersistentState(Serializable id, Object version, SessionImplementor session) throws HibernateException {<br />AbstractDomainObject entity = (AbstractDomainObject) session.getEntity(new Key(id, this));<br />Map originalValues = entity.getOriginalState();<br />Type[] types = getPropertyTypes();<br />String[] propertyNames = getPropertyNames();<br />Object[] values = new Object[propertyNames.length];<br />boolean[] includeProperty = getPropertyUpdateability();<br />for (int i = 0; i &lt; propertyNames.length; i++) {<br />if (includeProperty[i]) {<br />if (originalValues.containsKey(propertyNames[i])) {<br />values[i] = types[i].disassemble(originalValues.get(propertyNames[i]), session);<br />} else {<br />values[i] = types[i].disassemble(getPropertyValue(entity, propertyNames[i]), session);<br />}<br />}<br />}<br />return values;<br />}<br /></p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">What about collections you ask? Well, Hibernate keeps track of changes to collections in the collection class wrappers for you.</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma">Please note this solution has worked great in my example programs, but I have not yet implemented it in a production program.</p>
		</div>
<img src ="http://www.blogjava.net/junky/aggbug/75604.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2006-10-17 14:09 <a href="http://www.blogjava.net/junky/archive/2006/10/17/75604.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate基础配置</title><link>http://www.blogjava.net/junky/archive/2006/10/17/75605.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Tue, 17 Oct 2006 06:09:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2006/10/17/75605.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/75605.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2006/10/17/75605.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/75605.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/75605.html</trackback:ping><description><![CDATA[
		<div class="diaryContent" id="diary313179" style="DISPLAY: block">
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">Hibernate配置文件可以有两种格式，一种是hibernate.properties，另一种是hibernate.cfg.xml。后者稍微方便一些，当增加hbm映射文件的时候，可以直接在hibernate.cfg.xml里面增加，不必像hibernate.properties必须在初始化代码中加入。但不管怎么说，两种的配置项都是一样的，下面详细介绍： </p>
				<br />
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">在Hibernate的src目录下有一个hibernate.properties模板，我们不必自己从头写，修改模板就可以了</p>
				<img height="19" src="file:///D:%5Ctemp%5Cmsohtml1%5C01%5Cclip_image001.gif" width="19" border="0" />
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">这个配置意思是当你在 Hibernate里面输入true的时候，Hibernate会转化为0插入数据库，当你在Hibernate里面输入false的时候， Hibernate会转化为1插入数据库，后面的Y，N同理。对于某些数据库，例如Oracle来说，没有boolean数据类型，就是采用1代表 true，0代表false，因此使用这个配置在Hibernate里面直接用true/false会非常直观。 </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.dialect net.sf.hibernate.dialect.MySQLDialect </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.driver_class org.gjt.mm.mysql.Driver </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.driver_class com.mysql.jdbc.Driver </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.url jdbc:mysql:///test </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.username root </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.password </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">这是一个连接MySQL数据库的例子，很直观，不必解释，不同的数据库的连接参数模板中全部给出了。 </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.pool_size 1 </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.statement_cache.size 25</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">这是Hibernate自带的连接池的配置参数，在默认情况下将采用。意义很直观，不多解释。只是提醒一点，Hibernate这个连接池是非常原始非常简单的连接池，如果你在项目中用Hibernate的话，建议你<span style="COLOR: red">首选App Server的连接池</span>，次选Hibernate带的DBCP连接池。自带的连接池应该做为末选。 </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">如果你采用DBCP连接池，除了要配置DBCP连接池以外，还需要取消掉下行的注释： </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.provider_class net.sf.hibernate.connection.DBCPConnectionProvider</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">其它的连接池同理。 </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">如果采用App Server的连接池，假设App Server连接池的DataSource的JNDI名称为"mypool"的话，配置应该如下： </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.dialect net.sf.hibernate.dialect.MySQLDialect </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.datasource mypool </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.provider_class net.sf.hibernate.connection.DatasourceConnectionProvider</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">其它参数就不必写了，因为已经在App Server配置连接池的时候指定好了。 </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">如果你不是在App Server环境中使用Hibernate，例如远程客户端程序，但是你又想用App Server的数据库连接池，那么你还需要配置JNDI的参数，例如Hibernate连接远程Weblogic上的数据库连接池： </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.dialect net.sf.hibernate.dialect.MySQLDialect </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.datasource mypool </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.connection.provider_class net.sf.hibernate.connection.DatasourceConnectionProvider </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.jndi.class weblogic.jndi.WLInitialContextFactory </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.jndi.url t3://servername:7001/</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">最后，如果你需要在EJB或者JTA中使用Hibernate，需要取消下行的注释： </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">杂项配置： </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.show_sql false</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">是否将Hibernate发送给数据库的sql显示出来，这是一个非常非常有用处的功能。当你在调试Hibernate的时候，让Hibernate打印sql语句，可以帮助你迅速解决问题。 </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">#hibernate.connection.isolation 4</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">指定数据库的隔离级别，往往不同的数据库有自己定义的隔离级别，未必是Hibernate的设置所能更改的，所以也不必去管它了。 </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.jdbc.fetch_size 50 </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">hibernate.jdbc.batch_size 25</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: rgb(255,102,0); FONT-FAMILY: ˎ̥">这两个选项非常非常非常重要！！！将严重影响Hibernate的CRUD性能! </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in">
						<span style="FONT-FAMILY: 宋体">注</span>
						<span style="FONT-FAMILY: tahoma">: </span>
						<span style="FONT-FAMILY: ˎ̥">C = create, R = read, U = update, D = delete</span>
				</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">Fetch Size 是设定JDBC的Statement读取数据的时候每次从数据库中取出的记录条数。例如一次查询1万条记录，对于Oracle的JDBC驱动来说，是不会1次性把1万条取出来的，而只会取出Fetch Size条数，当纪录集遍历完了这些记录以后，再去数据库取Fetch Size条数据。因此大大节省了无谓的内存消耗。当然Fetch Size设的越大，读数据库的次数越少，速度越快；Fetch Size越小，读数据库的次数越多，速度越慢。这有点像平时我们写程序写硬盘文件一样，设立一个Buffer，每次写入Buffer，等Buffer满了以后，一次写入硬盘，道理相同。 </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">Oracle数据库的JDBC驱动默认的Fetch Size=10，是一个非常保守的设定，根据我的测试，当Fetch Size=50的时候，性能会提升1倍之多，当Fetch Size=100，性能还能继续提升20%，Fetch Size继续增大，性能提升的就不显著了。因此我建议<span style="COLOR: red">使用Oracle的一定要将</span><span style="FONT-WEIGHT: bold; COLOR: red">Fetch </span><span style="COLOR: red">Size设到50</span>。 </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in">
						<span style="FONT-FAMILY: ˎ̥">不过并不是所有的数据库都支持Fetch Size特性，例如MySQL就不支持。MySQL就像我上面说的那种最坏的情况，他总是一下就把1万条记录完全取出来，内存消耗会非常非常惊人！这个情况就没有什么好办法了</span>
						<span style="FONT-FAMILY: 宋体">。</span>
				</p>
				<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: tahoma"> </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">Batch Size是设定对数据库进行批量删除，批量更新和批量插入的时候的批次大小，有点相当于设置Buffer缓冲区大小的意思。Batch Size越大，批量操作的向数据库发送sql的次数越少，速度就越快。我做的一个测试结果是当Batch Size=0的时候，使用Hibernate对Oracle数据库删除1万条记录需要25秒，Batch Size = 50的时候，删除仅仅需要5秒！！！可见有多么大的性能提升！很多人做Hibernate和JDBC的插入性能测试会奇怪的发现Hibernate速度至少是JDBC的两倍，就是因为Hibernate使用了Batch Insert，而他们写的JDBC没有使用Batch的缘故。以我的经验来看，<span style="COLOR: red">Oracle数据库 </span><span style="FONT-WEIGHT: bold; COLOR: red">Batch </span><span style="COLOR: red">Size = 30 的时候比较合适，50也不错，性能会继续提升</span>，50以上，性能提升的非常微弱，反而消耗内存更加多，就没有必要了。 </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">#hibernate.jdbc.use_scrollable_resultset true</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">设定是否可以使用JDBC2.0规范的可滚动结果集，这对Hibernate的分页显示有一定的作用，默认就好了。 </p>
				<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">代码:</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: blue; FONT-FAMILY: ˎ̥">#hibernate.cglib.use_reflection_optimizer false</p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">默认打开，启用cglib反射优化。cglib是用来在Hibernate中动态生成PO字节码的，打开优化可以加快字节码构造的速度。 </p>
				<p style="FONT-SIZE: 9pt; MARGIN: 0in; FONT-FAMILY: ˎ̥">不过，当你在调试程序过程中，特别是和proxy，lazy loading相关的应用中，代码出错，但是出错提示信息有语焉不详，那么你可以把cglib优化关掉，这样Hibernate会输出比较详细的调试信息，帮助你debug。</p>
		</div>
<img src ="http://www.blogjava.net/junky/aggbug/75605.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2006-10-17 14:09 <a href="http://www.blogjava.net/junky/archive/2006/10/17/75605.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate2.1升级到Hibernate3.0 (转)</title><link>http://www.blogjava.net/junky/archive/2006/08/28/66126.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Mon, 28 Aug 2006 02:56:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2006/08/28/66126.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/66126.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2006/08/28/66126.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/66126.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/66126.html</trackback:ping><description><![CDATA[
		<p>
				<font face="verdana, arial, helvetica" size="2">
						<span class="javascript" id="text4391596">尽管Hibernate 3.0 与Hibernate2.1的源代码是不兼容的，但是当Hibernate开发小组在设计Hibernate3.0时，为简化升级Hibernate版本作了周到的考虑。对于现有的基于Hibernate2.1的Java项目，可以很方便的把它升级到Hibernate3.0。 Hibernate3.0版本的新变化，Hibernate3.0版本的变化包括三个方面： <br />(1)API的变化，它将影响到Java程序代码。 <br />(2)元数据，它将影响到对象-关系映射文件。 <br />(3)HQL查询语句。 <br /><br />值得注意的是， Hibernate3.0并不会完全取代Hibernate2.1。在同一个应用程序中，允许Hibernate3.0和Hibernate2.1并存。 <br /><br />1.1 Hibernate API 变化 <br /><br />1.1.1 包名 <br /><br />Hibernate3.0的包的根路径为: “org.hibernate” ，而在Hibernate2.1中“net.sf.hibernate”。这一命名变化使得Hibernate2.1和Hibernate3.0能够同时在同一个应用程序中运行。 <br /><br />如果希望把已有的应用升级到Hibernate3.0，那么升级的第一步是把Java源程序中的所有“net.sf.hibernate”替换为“org.hibernate”。 <br /><br />Hibernate2.1中的“net.sf.hibernate.expression”包被改名为“org.hibernate.criterion”。假如应用程序使用了Criteria API，那么在升级的过程中，必须把Java源程序中的所有“net.sf.hibernate.expression”替换为“org.hibernate.criterion”。 <br /><br />如果应用使用了除Hibernate以外的其他外部软件，而这个外部软件又引用了Hibernate的接口，那么在升级时必须十分小心。例如EHCache拥有自己的CacheProvider： net.sf.ehcache.hibernate.Provider，在这个类中引用了Hibernate2.1中的接口，在升级应用时，可以采用以下办法之一来升级EHCache: <br /><br />(1)手工修改net.sf.ehcache.hibernate.Provider类，使它引用Hibernate3.0中的接口。 <br />(2)等到EHCache软件本身升级为使用Hibernate3.0后，使用新的EHCache软件。 <br />(3)使用Hibernate3.0中内置的CacheProvider：org.hibernate.cache.EhCacheProvider。 <br /><br />1.1.2 org.hibernate.classic包 <br /><br />Hibernate3.0把一些被废弃的接口都转移到org.hibernate.classic中。 <br /><br />1.1.3 Hibernate所依赖的第三方软件包 <br /><br />在Hibernate3.0的软件包的lib目录下的README.txt文件中，描述了Hibernate3.0所依赖的第三方软件包的变化。 <br /><br />1.1.4 异常模型 <br /><br />在Hibernate3.0中，HibernateException异常以及它的所有子类都继承了java.lang.RuntimeException。因此在编译时，编译器不会再检查HibernateException。 <br /><br />1.1.5 Session接口 <br /><br />在Hibernate3.0中，原来Hibernate2.1的Session接口中的有些基本方法也被废弃，但为了简化升级，这些方法依然是可用的，可以通过org.hibernate.classic.Session子接口来访问它们，例如： <br />org.hibernate.classic.Session session=sessionFactory.openSession(); <br />session.delete("delete from Customer "); <br />在Hibernate3.0中，org.hibernate.classic.Session接口继承了org.hibernate.Session接口，在org.hibernate.classic.Session接口中包含了一系列被废弃的方法，如find()、interate()等。SessionFactory接口的openSession()方法返回org.hibernate.classic.Session类型的实例。如果希望在程序中完全使用Hibernate3.0，可以采用以下方式创建Session实例： <br /><br />org.hibernate.Session session=sessionFactory.openSession(); <br /><br />如果是对已有的程序进行简单的升级，并且希望仍然调用Hibernate2.1中Session的一些接口，可以采用以下方式创建Session实例： <br /><br />org.hibernate.classic.Session session=sessionFactory.openSession(); <br /><br />在Hibernate3.0中，Session接口中被废弃的方法包括： <br />* 执行查询的方法：find()、iterate()、filter()和delete(String hqlSelectQuery) <br />* saveOrUpdateCopy() <br /><br />Hibernate3.0一律采用createQuery()方法来执行所有的查询语句，采用DELETE 查询语句来执行批量删除，采用merge()方法来替代 saveOrUpdateCopy()方法。 <br /><br />提示：在Hibernate2.1中，Session的delete()方法有几种重载形式，其中参数为HQL查询语句的delete()方法在Hibernate3.0中被废弃，而参数为Ojbect类型的的delete()方法依然被支持。delete(Object o)方法用于删除参数指定的对象，该方法支持级联删除。 <br />Hibernate2.1没有对批量更新和批量删除提供很好的支持，参见&lt;&lt;精通Hibernate&gt;&gt;一书的第13章的13.1.1节（批量更新和批量删除），而Hibernate3.0对批量更新和批量删除提供了支持，能够直接执行批量更新或批量删除语句，无需把被更新或删除的对象先加载到内存中。以下是通过Hibernate3.0执行批量更新的程序代码： <br />Session session = sessionFactory.openSession(); <br />Transaction tx = session.beginTransaction(); <br />String hqlUpdate = "update Customer set name = :newName where name = <img alt="困惑" src="http://www.itpub.net/images/smilies/33.gif" border="0" />ldName"; <br />int updatedEntities = s.createQuery( hqlUpdate ) <br />.setString( "newName", newName ) <br />.setString( "oldName", oldName ) <br />.executeUpdate(); <br />tx.commit(); <br />session.close(); <br />以下是通过Hibernate3.0执行批量删除的程序代码： <br />Session session = sessionFactory.openSession(); <br />Transaction tx = session.beginTransaction(); <br />String hqlDelete = "delete Customer where name = <img alt="困惑" src="http://www.itpub.net/images/smilies/33.gif" border="0" />ldName"; <br />int deletedEntities = s.createQuery( hqlDelete ) <br />.setString( "oldName", oldName ) <br />.executeUpdate(); <br />tx.commit(); <br />session.close(); <br /><br />1.1.6 createSQLQuery() <br /><br />在Hibernate3.0中，Session接口的createSQLQuery()方法被废弃，被移到org.hibernate.classic.Session接口中。Hibernate3.0采用新的SQLQuery接口来完成相同的功能。 <br /><br />1.1.7 Lifecycle 和 Validatable 接口 <br /><br />Lifecycle和Validatable 接口被废弃，并且被移到org.hibernate.classic包中。 <br /><br />1.1.8 Interceptor接口 <br /><br />在Interceptor 接口中加入了两个新的方法。 用户创建的Interceptor实现类在升级的过程中，需要为这两个新方法提供方法体为空的实现。此外，instantiate()方法的参数作了修改，isUnsaved()方法被改名为isTransient()。 <br /><br />1.1.9 UserType和CompositeUserType接口 <br /><br />在UserType和CompositeUserType接口中都加入了一些新的方法，这两个接口被移到org.hibernate.usertype包中，用户定义的UserType和CompositeUserType实现类必须实现这些新方法。 <br />Hibernate3.0提供了ParameterizedType接口，用于更好的重用用户自定义的类型。 <br />1.1.10 FetchMode类 <br /><br />FetchMode.LAZY 和 FetchMode.EAGER被废弃。取而代之的分别为FetchMode.SELECT 和FetchMode.JOIN。 <br /><br />1.1.11 PersistentEnum类 <br /><br />PersistentEnum被废弃并删除。已经存在的应用应该采用UserType来处理枚举类型。 <br /><br />1.1.12 对Blob 和Clob的支持 <br /><br />Hibernate对Blob和Clob实例进行了包装，使得那些拥有Blob或Clob类型的属性的类的实例可以被游离、序列化或反序列化，以及传递到merge()方法中。 <br /><br />1.1.13 Hibernate中供扩展的API的变化 <br /><br />org.hibernate.criterion、 org.hibernate.mapping、 org.hibernate.persister和org.hibernate.collection 包的结构和实现发生了重大的变化。多数基于Hibernate <br />2.1 的应用不依赖于这些包，因此不会被影响。如果你的应用扩展了这些包中的类，那么必须非常小心的对受影响的程序代码进行升级。 <br /><br />1.2 元数据的变化 <br /><br />1.2.1 检索策略 <br /><br />在Hibernate2.1中，lazy属性的默认值为“false”，而在Hibernate3.0中，lazy属性的默认值为“true”。在升级映射文件时，如果原来的映射文件中的有关元素，如&lt;set&gt;、&lt;class&gt;等没有显式设置lazy属性，那么必须把它们都显式的设置为lazy=“true”。如果觉得这种升级方式很麻烦，可以采取另一简单的升级方式：在&lt;hibernate-mapping&gt;元素中设置: default-lazy=“false”。 <br /><br />1.2.2 对象标识符的映射 <br /><br />unsaved-value属性是可选的，在多数情况下，Hibernate3.0将把unsaved-value="0" 作为默认值。 <br /><br />在Hibernate3.0中，当使用自然主键和游离对象时，不再强迫实现Interceptor.isUnsaved()方法。 如果没有设置这个方法，当Hibernate3.0无法区分对象的状态时，会查询数据库，来判断这个对象到底是临时对象，还是游离对象。不过，显式的使用Interceptor.isUnsaved()方法会获得更好的性能，因为这可以减少Hibernate直接访问数据库的次数。 <br /><br />1.2.3 集合映射 <br /><br />&lt;index&gt;元素在某些情况下被&lt;list-index&gt;和&lt;map-key&gt;元素替代。此外，Hibernate3.0用&lt;map-key-many-to-many&gt; 元素来替代原来的&lt;key-many-to-many&gt;.元素，用&lt;composite-map-key&gt;元素来替代原来的&lt;composite-index&gt;元素。 <br /><br />1.2.4 DTD <br /><br />对象-关系映射文件中的DTD文档，由原来的： <br /><a href="http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" target="_blank">http://hibernate.sourceforge.net/hi...mapping-2.0.dtd</a><br />改为： <br /><a href="http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" target="_blank">http://hibernate.sourceforge.net/hi...mapping-3.0.dtd</a><br /><br />1.3 查询语句的变化 <br /><br />Hibernate3.0 采用新的基于ANTLR的HQL/SQL查询翻译器，不过，Hibernate2.1的查询翻译器也依然存在。在Hibernate的配置文件中，hibernate.query.factory_class属性用来选择查询翻译器。例如： <br />（1）选择Hibernate3.0的查询翻译器： <br />hibernate.query.factory_class= org.hibernate.hql.ast.ASTQueryTranslatorFactory <br />（2）选择Hibernate2.1的查询翻译器 <br />hibernate.query.factory_class= org.hibernate.hql.classic.ClassicQueryTranslatorFactory <br /><br />提示：ANTLR是用纯Java语言编写出来的一个编译工具，它可生成Java语言或者是C++的词法和语法分析器，并可产生语法分析树并对该树进行遍历。ANTLR由于是纯Java的，因此可以安装在任意平台上，但是需要JDK的支持。 <br />Hibernate开发小组尽力保证Hibernate3.0的查询翻译器能够支持Hibernate2.1的所有查询语句。不过，对于许多已经存在的应用，在升级过程中，也不妨仍然使用Hibernate2.1的查询翻译器。 <br />值得注意的是， Hibernate3.0的查询翻译器存在一个Bug：不支持某些theta-style连结查询方言：如Oracle8i的OracleDialect方言、Sybase11Dialect。解决这一问题的办法有两种：（1）改为使用支持ANSI-style连结查询的方言，如 Oracle9Dialect,（2）如果升级的时候遇到这一问题，那么还是改为使用Hibernate2.1的查询翻译器。 <br /><br />1.3.1 indices()和elements()函数 <br /><br />在HQL的select子句中废弃了indices()和elements()函数，因为这两个函数的语法很让用户费解，可以用显式的连接查询语句来替代 select elements(...) 。而在HQL的where子句中，仍然可以使用elements()函数<br /><br /><br /><br /><br /><br /></span>
				</font>
		</p>
		<p>
				<font face="verdana, arial, helvetica" size="2">
						<span class="javascript" id="text4391600">hibernate2 升级为hibernate3的需要注意的事<br /><br />1.首先将hibernate2.jar替换为hibernate3.jar(hibernate-3.0.5)<br />hibernate-tools.jar也替换成新的(从hibernate-tools-3.0.0.alpha4a找出来的)<br />2.将所有程序中的net.sf.hibernate替换为org.hibernate.<br /><br />3.但是有例外<br />net.sf.hibernate.expression.Expression换为org.hibernate.criterion.Expression<br />如果用eclipse,用ctrl+shift+o快捷键可以加快速度<img alt="憨笑" src="http://www.itpub.net/images/smilies/14.gif" border="0" /><br /><br />4.在使用hql查询时将<br />createSQLQuery(hql,"c",EZCampaignDTO.class);改为createSQLQuery(hql).addEntity("c",EZCampaignDTO.class);<br /><br />5.在批量插入时<br />将原来的int size = ((SessionFactoryImpl)(session.getSessionFactory())).getJdbcBatchSize()<br />改为int size = ((SessionFactoryImpl)(session.getSessionFactory())).getSettings().getJdbcBatchSize();<br /><br />6.在计算count时<br />将原来的int size = ((Integer) session.iterate(hql).next()).intValue();<br />改为int size = ((Integer) session.createQuery(hql).iterate().next()).intValue();<br />其中hql="select count(*) from " + DAOVar.contactClass;<br /><br />7.还有就是把.hbm中的hibernate-mapping-2.0.dtd替换为hibernate-mapping-3.0.dtd<br />Hibernate Mapping DTD 2.0替换为Hibernate Mapping DTD 3.0<br /><br />8.hibernate.cfg.xml中<br />Hibernate Mapping DTD 2.0替换为Hibernate Mapping DTD 3.0<br />&lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.SQLServerDialect&lt;/property&gt;<br /><br />9.hibernate.properties中类似<br /><br />10.cache-config.xml中<br />&lt;provider className="net.sf.hibernate.cache.OSCacheProvider"/&gt;替换为<br />&lt;provider className="org.hibernate.cache.OSCacheProvider"/&gt;<br /><br />11.classeshibernate.properties中<br />hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider<br />hibernate.dialect=org.hibernate.dialect.SQLServerDialect<br />晕s了,怎么这里还有<br />还是用编辑器暴力替换一下吧干脆<br /><br />然后部署,集成测试,希望一切ok<br />结果咣铛,还是报错<br /><br />12.在自动外部模块部分有一个功能是根据模版自动生成.hbm文件在load,结果出来的.hbm中有问题:<br />生成的 &lt;composite-id unsaved-value="any" mapped="false"&gt;其中mapped="false" 出错.<br />找了半天才发现在网上的hibernate-mapping-3.0.dtd文件有支持mapped="false"这个属性.而本地的hebernate3.0.5中的<br />hibernate-mapping-3.0.dtd文件没有这个属性.晕,hibernate也太不负责了吧. 解决办法把hibernate-mapping-3.0.dtd<br />copy到jboss\bin目录下然后,在template文件中<br />&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "hibernate-mapping-3.0.dtd"&gt;<br />然后他会在jboss\bin目录下读取该文件<br /><br />13.重新测试,还是咣铛<br />发现子类读父类数据时抛出异常<br />"org.hibernate.LazyInitializationException: could not initialize proxy"<br />延迟抓取出的错,hb3对many-to-one的默认处理是lazy = "proxy"<br />没有搞懂到底怎么回事,把所有many-to-one,one-to-one都加上lazy="false"<br />再测试终于大功告成</span>
				</font>
		</p>
<img src ="http://www.blogjava.net/junky/aggbug/66126.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2006-08-28 10:56 <a href="http://www.blogjava.net/junky/archive/2006/08/28/66126.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用hibernate经常碰到的一些异常介绍</title><link>http://www.blogjava.net/junky/archive/2006/08/25/65698.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Fri, 25 Aug 2006 02:19:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2006/08/25/65698.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/65698.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2006/08/25/65698.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/65698.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/65698.html</trackback:ping><description><![CDATA[
		<div class="postText">
				<p>在使用hibernate过程中经常碰到一些异常，有些很常见，经过个人经验和网上的搜集，也总结一下：</p>
				<p>一.CGLIB异常</p>
				<p>org.springframework.orm.hibernate.HibernateSystemException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.anyi.fa.model.FaCard.setCardND; nested exception is net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.anyi.fa.model.FaCard.setCardND<br />net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.anyi.fa.model.FaCard.setCardND<br /> at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:220)<br /> at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2224)<br /> at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:319)<br /> at net.sf.hibernate.loader.Loader.doQuery(Loader.java:309)<br /> at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)<br /> at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:941)<br /> at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:961)<br /> at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)<br /> at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)<br /> at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:413)<br /> at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2131)</p>
				<p> 此中异常比较常见，一般是因为null造成的。例如：一个int映射到数据库中，但从数据库读取时是null值这样就会出现此中异常。</p>
				<p>解决方式：如果允许为空的字段做一包装（int--&gt;Integer,long--&gt;Long，double---&gt;Double等），其实这在系统设计时就应该根据实际情况考虑到的。</p>
				<p>二 。延迟加载问题</p>
				<p>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; COLOR: red; FONT-FAMILY: Georgia; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体">
								<font color="#000000" size="2">net.sf.hibernate.LazyInitializationException:</font>
						</span>
				</p>
				<p>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; COLOR: red; FONT-FAMILY: Georgia; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体">
								<font color="#000000" size="2"> Failed to lazily initialize a collection - no session or session was closed</font>
						</span>
				</p>
				<p>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; COLOR: red; FONT-FAMILY: Georgia; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体">
								<font color="#000000" size="2">此类问题也是比较常见的，一般是由于采用了延迟加载机制(lazy=true)，在session关闭之后又调用延迟加载的数据或方法造成的。</font>
						</span>
				</p>
				<p>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; COLOR: red; FONT-FAMILY: Georgia; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体">
								<font color="#000000" size="2">解决方式:</font>
						</span>
				</p>
				<p>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; COLOR: red; FONT-FAMILY: Georgia; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体">
								<font color="#000000" size="2">   在session关闭之前读取，或调用HIbernate.initalize()方法。</font>
						</span>
				</p>
				<p>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; COLOR: red; FONT-FAMILY: Georgia; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体">
								<font color="#000000" size="2">三.BigDecimal属性映射时要指定小数位数。</font>
						</span>
				</p>
				<p>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; COLOR: red; FONT-FAMILY: Georgia; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体">
								<font color="#000000" size="2">四.其他就是一些级联更新删除，主外建约束错误，操作主，子表顺序的问题了，一般比较容易解决.</font>
						</span> </p>
				<br />
				<br />
				<p id="TBPingURL">Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1108781</p>
		</div>
<img src ="http://www.blogjava.net/junky/aggbug/65698.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2006-08-25 10:19 <a href="http://www.blogjava.net/junky/archive/2006/08/25/65698.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate对视图的操作 </title><link>http://www.blogjava.net/junky/archive/2006/06/02/49821.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Thu, 01 Jun 2006 16:17:00 GMT</pubDate><guid>http://www.blogjava.net/junky/archive/2006/06/02/49821.html</guid><wfw:comment>http://www.blogjava.net/junky/comments/49821.html</wfw:comment><comments>http://www.blogjava.net/junky/archive/2006/06/02/49821.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/junky/comments/commentRss/49821.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/junky/services/trackbacks/49821.html</trackback:ping><description><![CDATA[
		<p>      由于视图没有主键，所以在用hibernate对视图操作就需要做点处理了，网上搜了一通也没能找到相关文章，后来突然想到了myeclipse可以帮助生成hibernate的配置文件和对应的pojo代码。<br />      打开myeclipse，选择相关的视图，生成了配置文件和pojo类，发现pojo类生成了两个，而配置文件却一个，参看了配置文件和类，明白了是怎么回事。生成的配置文件通过了组合的方式生成，所以会对应两个类，一个类主要存放id信息，以个类存放对应的字段信息。<br />      下面是生成的类和配置文件：<br />类：<br />public class AllTablePb implements Serializable {</p>
		<p> // Fields<br /> private AllTablePbId id;</p>
		<p> // Property accessors<br /> public AllTablePbId getId() {<br />  return this.id;<br /> }</p>
		<p> public void setId(AllTablePbId id) {<br />  this.id = id;<br /> }<br />}</p>
		<p>public class AllTablePbId implements java.io.Serializable {</p>
		<p> // Fields</p>
		<p> private String owner;</p>
		<p> private String tableName;</p>
		<p> private String columnName;</p>
		<p> private String dataType;</p>
		<p> private String pbcCnam;</p>
		<p> private String pbcCmnt;</p>
		<p> // Property accessors</p>
		<p> public String getOwner() {<br />  return this.owner;<br /> }</p>
		<p> public void setOwner(String owner) {<br />  this.owner = owner;<br /> }</p>
		<p> public String getTableName() {<br />  return this.tableName;<br /> }</p>
		<p> public void setTableName(String tableName) {<br />  this.tableName = tableName;<br /> }</p>
		<p> public String getColumnName() {<br />  return this.columnName;<br /> }</p>
		<p> public void setColumnName(String columnName) {<br />  this.columnName = columnName;<br /> }</p>
		<p> public String getDataType() {<br />  return this.dataType;<br /> }</p>
		<p> public void setDataType(String dataType) {<br />  this.dataType = dataType;<br /> }</p>
		<p> public String getPbcCnam() {<br />  return this.pbcCnam;<br /> }</p>
		<p> public void setPbcCnam(String pbcCnam) {<br />  this.pbcCnam = pbcCnam;<br /> }</p>
		<p> public String getPbcCmnt() {<br />  return this.pbcCmnt;<br /> }</p>
		<p> public void setPbcCmnt(String pbcCmnt) {<br />  this.pbcCmnt = pbcCmnt;<br /> }</p>
		<p>}</p>
		<p>配置文件：<br />&lt;hibernate-mapping&gt;<br /> &lt;class name="com.hhkj.workflow.bean.AllTablePb" table="V_ALLTAB_PB" schema="CANP"&gt;<br />  &lt;composite-id name="id" class="com.hhkj.workflow.bean.AllTablePbId"&gt;<br />   &lt;key-property name="owner" type="string"&gt;<br />    &lt;column name="OWNER" length="30" /&gt;<br />   &lt;/key-property&gt;<br />   &lt;key-property name="tableName" type="string"&gt;<br />    &lt;column name="TABLE_NAME" length="30" /&gt;<br />   &lt;/key-property&gt;<br />   &lt;key-property name="columnName" type="string"&gt;<br />    &lt;column name="COLUMN_NAME" length="30" /&gt;<br />   &lt;/key-property&gt;<br />   &lt;key-property name="dataType" type="string"&gt;<br />    &lt;column name="DATA_TYPE" length="106" /&gt;<br />   &lt;/key-property&gt;<br />   &lt;key-property name="pbcCnam" type="string"&gt;<br />    &lt;column name="PBC_CNAM" length="30" /&gt;<br />   &lt;/key-property&gt;<br />   &lt;key-property name="pbcCmnt" type="string"&gt;<br />    &lt;column name="PBC_CMNT" length="254" /&gt;<br />   &lt;/key-property&gt;<br />  &lt;/composite-id&gt;<br /> &lt;/class&gt;<br />&lt;/hibernate-mapping&gt;<br />这样就可以通过AllTablePb.getId()取得相关的信息。<br />      感觉myeclipse的确不错，特别是对于使用hibernate还不是非常熟练的人来说，通过它可以帮解决不少问题。<br />      通过这样对视图的操作，同样也可以用到对于那些没有定义主键的表，操作方法是一样的。<br />      呵呵，个人的一点体会，欢迎大家多提意见。</p>
<img src ="http://www.blogjava.net/junky/aggbug/49821.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/junky/" target="_blank">junky</a> 2006-06-02 00:17 <a href="http://www.blogjava.net/junky/archive/2006/06/02/49821.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>