﻿<?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-Skynet-随笔分类-hibernate</title><link>http://www.blogjava.net/Good-Game/category/23018.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 29 May 2008 08:57:08 GMT</lastBuildDate><pubDate>Thu, 29 May 2008 08:57:08 GMT</pubDate><ttl>60</ttl><item><title>数据库表地址数据（中国地区）</title><link>http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Thu, 29 May 2008 02:51:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/203747.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/203747.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/203747.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 为方便项目使用，特留！表结构：Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> CREATE TABLE `location` (  `id` bigint(20) NOT NULL auto_increment,  `name` varchar(50...&nbsp;&nbsp;<a href='http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html'>阅读全文</a><img src ="http://www.blogjava.net/Good-Game/aggbug/203747.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-05-29 10:51 <a href="http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring 与 hibernate 整合(事务)</title><link>http://www.blogjava.net/Good-Game/archive/2008/05/09/199477.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Fri, 09 May 2008 05:47:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/05/09/199477.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/199477.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/05/09/199477.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/199477.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/199477.html</trackback:ping><description><![CDATA[参考：第 9 章 事务管理 - Spring Framework  reference 2.0.5  参考手册中文版<br /><font color="#0000ff">http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch09.html</font><br /><br />先从配置文件开始：<br />源码：<a href="/Files/Good-Game/springAop.rar">springAop.rar</a><br /><br />需要jar<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="UTF-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpath</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="src"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="java"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="con"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="org.eclipse.jdt.launching.JRE_CONTAINER"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/aspectjrt.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/aspectjweaver.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/spring.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/spring-sources.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/commons-logging-1.0.4.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/cglib-nodep-2.1_3.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/hibernate3.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/log4j-1.2.11.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="con"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/dom4j-1.6.1.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/commons-collections-2.1.1.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/mysql.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/jta.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="lib"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="lib/antlr-2.7.6.jar"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">classpathentry </span><span style="color: rgb(255, 0, 0);">kind</span><span style="color: rgb(0, 0, 255);">="output"</span><span style="color: rgb(255, 0, 0);"> path</span><span style="color: rgb(0, 0, 255);">="bin"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">classpath</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span></div><br /><br /><font color="#a52a2a">spring 配置<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="UTF-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">beans </span><span style="color: rgb(255, 0, 0);">xmlns</span><span style="color: rgb(0, 0, 255);">="http://www.springframework.org/schema/beans"</span><span style="color: rgb(255, 0, 0);"><br />       xmlns:xsi</span><span style="color: rgb(0, 0, 255);">="http://www.w3.org/2001/XMLSchema-instance"</span><span style="color: rgb(255, 0, 0);"><br />       xmlns:aop</span><span style="color: rgb(0, 0, 255);">="http://www.springframework.org/schema/aop"</span><span style="color: rgb(255, 0, 0);"><br />       xmlns:tx</span><span style="color: rgb(0, 0, 255);">="http://www.springframework.org/schema/tx"</span><span style="color: rgb(255, 0, 0);"><br />       xsi:schemaLocation</span><span style="color: rgb(0, 0, 255);">="<br />       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd<br />       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd<br />       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />       <br />  <br />  </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> daoCalss : extends HibernateDaoSupport implements BeanDao </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">bean </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="beanDao"</span><span style="color: rgb(255, 0, 0);"> class</span><span style="color: rgb(0, 0, 255);">="dao.imp.BeanDaoImp"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />      </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="sessionFactory"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />          </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">ref </span><span style="color: rgb(255, 0, 0);">bean</span><span style="color: rgb(0, 0, 255);">="sessionFactory"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">ref</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />      </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">bean</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />  <br />   <br />   </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> hibernate3 sessionFactory </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />   </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">bean </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="sessionFactory"</span><span style="color: rgb(255, 0, 0);">     <br />        class</span><span style="color: rgb(0, 0, 255);">="org.springframework.orm.hibernate3.LocalSessionFactoryBean"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">   <br />        </span><font><font color="#a52a2a"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> spring 与 hibernate 联系 </span><span style="color: rgb(0, 128, 0);">--&gt;</span></font></font><span style="color: rgb(0, 0, 0);">   <br />       </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="configLocation"</span><span style="color: rgb(255, 0, 0);"> value</span><span style="color: rgb(0, 0, 255);">="classpath:hibernate.cfg.xml"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);">  <br />   </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">bean</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">  <br />   <br />   </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> aop 与事务联系 aopBean&lt;-&gt;txAdvice  </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />   </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">aop:config</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />           </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> 逻辑拦截 </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />         </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">aop:pointcut </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="aopBean"</span><span style="color: rgb(255, 0, 0);"> expression</span><span style="color: rgb(0, 0, 255);">="execution(* *.*.*(..))"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />           </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">aop:advisor </span><span style="color: rgb(255, 0, 0);">advice-ref</span><span style="color: rgb(0, 0, 255);">="txAdvice"</span><span style="color: rgb(255, 0, 0);"> pointcut-ref</span><span style="color: rgb(0, 0, 255);">="aopBean"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />      </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">aop:config</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br />    </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> 事务适配器 </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />       </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">bean </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="txManager"</span><span style="color: rgb(255, 0, 0);"> class</span><span style="color: rgb(0, 0, 255);">="org.springframework.orm.hibernate3.HibernateTransactionManager"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />      </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="sessionFactory"</span><span style="color: rgb(255, 0, 0);"> ref</span><span style="color: rgb(0, 0, 255);">="sessionFactory"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">bean</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br />    </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> 事务原子 具体方法进行什么事务 </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />      </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">tx:advice </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="txAdvice"</span><span style="color: rgb(255, 0, 0);"> transaction-manager</span><span style="color: rgb(0, 0, 255);">="txManager"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />           </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">tx:attributes</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />             </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">tx:method </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="get*"</span><span style="color: rgb(255, 0, 0);"> read-only</span><span style="color: rgb(0, 0, 255);">="true"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />          </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">tx:method </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="*"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">tx:attributes</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />      </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">tx:advice</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">beans</span><span style="color: rgb(0, 0, 255);">&gt;</span></div><br /></font><br /><font color="#a52a2a"><b>hibernate 配置</b></font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="UTF-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;!</span><span style="color: rgb(255, 0, 255);">DOCTYPE hibernate-configuration PUBLIC<br />        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"<br />        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">hibernate-configuration</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">session-factory </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="asdf"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="hibernate.dialect"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">mysql</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="myeclipse.connection.profile"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        com.mysql.jdbc.Driver<br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="connection.url"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        jdbc:mysql://localhost/aop<br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="show_sql"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">true</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    <br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="connection.username"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">root</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="connection.password"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="connection.driver_class"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        com.mysql.jdbc.Driver<br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="dialect"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        org.hibernate.dialect.MySQLDialect<br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    <br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mapping </span><span style="color: rgb(255, 0, 0);">resource</span><span style="color: rgb(0, 0, 255);">="bean/UnitBean.hbm.xml"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    <br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">session-factory</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">hibernate-configuration</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span></div><br /><br />dao 类（接口）<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> dao.imp;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.List;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.springframework.orm.hibernate3.support.HibernateDaoSupport;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> bean.UnitBean;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> dao.BeanDao;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> BeanDaoImp </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> HibernateDaoSupport </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> BeanDao{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> addBean(UnitBean unitBean) {<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.getHibernateTemplate().save(unitBean);<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">UnitBean</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> getBeanByAll() {<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.getHibernateTemplate().find(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> from </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">UnitBean.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">.getName());<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> removeBean(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> beanId) {<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.getHibernateTemplate().delete(<br />                getHibernateTemplate().get(UnitBean.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">, beanId)<br />            );<br />    }<br />    <br />}<br /></span></div><br />Main 类<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> unit;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.springframework.context.ApplicationContext;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.springframework.context.support.ClassPathXmlApplicationContext;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> dao.BeanDao;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> bean.UnitBean;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Main {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) {<br />           ApplicationContext ctx </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ClassPathXmlApplicationContext(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">beans.xml</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />           BeanDao dao </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (BeanDao) ctx.getBean(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">beanDao</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />           UnitBean bean </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> UnitBean();<br />           bean.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">xx</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />           bean.setPass(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">11</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />           dao.addBean(bean);<br />           <br />           </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(UnitBean unitBean : dao.getBeanByAll() ){<br />               System.out.println( unitBean.getId() );<br />           }<br />           <br />           dao.removeBean(bean.getId());<br />           <br />    }<br />}<br /></span></div>结果：<br /><font color="#006400">Hibernate: insert into bean (name, pass) values (?, ?)<br />Hibernate: select unitbean0_.id as id0_, unitbean0_.name as name0_, unitbean0_.pass as pass0_ from bean unitbean0_<br />1<br />Hibernate: select unitbean0_.id as id0_0_, unitbean0_.name as name0_0_, unitbean0_.pass as pass0_0_ from bean unitbean0_ where unitbean0_.id=?<br />Hibernate: delete from bean where id=?<br /></font><br /><br /><br /><br /><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/199477.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-05-09 13:47 <a href="http://www.blogjava.net/Good-Game/archive/2008/05/09/199477.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 数据结构（ 树、多对多 结构表示 ）</title><link>http://www.blogjava.net/Good-Game/archive/2008/04/26/196293.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Sat, 26 Apr 2008 10:30:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/04/26/196293.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/196293.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/04/26/196293.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/196293.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/196293.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: level 类：Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->package hbm;import java.util.Set;/** * @hibernate.class table = "level" * where = " visible = ...&nbsp;&nbsp;<a href='http://www.blogjava.net/Good-Game/archive/2008/04/26/196293.html'>阅读全文</a><img src ="http://www.blogjava.net/Good-Game/aggbug/196293.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-04-26 18:30 <a href="http://www.blogjava.net/Good-Game/archive/2008/04/26/196293.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 级联添加删除</title><link>http://www.blogjava.net/Good-Game/archive/2008/04/02/190283.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Wed, 02 Apr 2008 02:07:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/04/02/190283.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/190283.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/04/02/190283.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/190283.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/190283.html</trackback:ping><description><![CDATA[问题描述：实体类答案从属于实体类问题。（一对多）<br /> 效果-》<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />     * 添加 问题 和 选项<br />     * </span><span style="color: rgb(128, 128, 128);">@throws</span><span style="color: rgb(0, 128, 0);"> Exception<br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">@Test<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testQu() </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> Exception {<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> HibernateUtil.currentSession();<br />        Transaction tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">级联添加</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        Set options </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashSet();<br />        Options op1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Options();<br />        op1.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">op1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        options.add(op1);<br />        <br />        <br />        Options op2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Options();<br />        op2.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">op2</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        options.add(op2);<br />        <br />        <br />        Options op3 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Options();<br />        op3.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">op3</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        options.add(op3);<br />        Problems problems </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Problems();<br />        <br />        problems.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">problem_1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        problems.setOptions(options);<br />        problems.setTdesc(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">tdesc</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        problems.setType(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />        <br />        Long ll </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Long)session.save(problems);<br />        <br />        <br />        System.out.println(ll);<br />        tr.commit();<br />        <br /><font color="#008000">mysql&gt; select * from options ;<br />+----+------------+--------+------+---------+<br />| id | problemsid | answer | name | visible |<br />+----+------------+--------+------+---------+<br />|  1 |          1 |   NULL | op2  |       0 |<br />|  2 |          1 |   NULL | op3  |       0 |<br />|  3 |          1 |   NULL | op1  |       0 |<br />+----+------------+--------+------+---------+<br />3 rows in set (0.00 sec)<br /><br />mysql&gt; select * from problems ;<br />+----+-----------+------+-------+------------+---------+<br />| id | name      | type | tdesc | questionid | visible |<br />+----+-----------+------+-------+------------+---------+<br />|  1 | problem_1 |    1 | tdesc |       NULL |       0 |<br />+----+-----------+------+-------+------------+---------+<br />1 row in set (0.00 sec)</font><br /><br />        <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">级联删除</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        tr.begin();<br />            session.delete( session.get(Problems.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">,ll) );<br />        tr.commit();<br /><br /><font color="#006400">mysql&gt; select * from problems ;<br />Empty set (0.00 sec)<br /><br />mysql&gt; select * from options ;<br />Empty set (0.00 sec) </font><br /><br />        HibernateUtil.closeSession();<br />    }</span></div><br />Options类<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">.......<br /></span><span style="color: rgb(0, 128, 0);">    /**</span><span style="color: rgb(0, 128, 0);"><br />     * @hibernate.many-to-one <br />     *         cascade = "save-update"<br />     *         column = "Problemsid"<br />     *         class = "com.zhongqi.domain.Problems"<br />     * </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"><br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Problems getProblems() {<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> problems;<br />    }<br />............<br /></span></div><br /><span style="color: rgb(0, 0, 0);">Problems 类<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />     * @hibernate.set<br />     *         cascade="all-delete-orphan"<br />     *         inverse = "false"<br />     *         lazy = "true"<br />     *         @hibernate.collection-key  column = "problemsid"<br />     *         @hibernate.collection-one-to-many class = "com.zhongqi.domain.Options"<br />     * </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"><br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Set getOptions() {<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> options;<br />    }</span></div><br /></span><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/190283.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-04-02 10:07 <a href="http://www.blogjava.net/Good-Game/archive/2008/04/02/190283.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql Blob hbn 操作</title><link>http://www.blogjava.net/Good-Game/archive/2008/01/28/178122.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Mon, 28 Jan 2008 01:53:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/01/28/178122.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/178122.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/01/28/178122.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/178122.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/178122.html</trackback:ping><description><![CDATA[1.数据库<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">desc</span><span style="color: rgb(0, 0, 0);"> lotteryinformation ;<br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">-----------+--------------+------+-----+---------+-------+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> Field       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> Type         </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">Null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">Key</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">Default</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> Extra </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">-----------+--------------+------+-----+---------+-------+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> id          </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">int</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">11</span><span style="color: rgb(0, 0, 0);">)      </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> NO   </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> PRI </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">NULL</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> type        </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">255</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> NO   </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">NULL</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> title       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">255</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> YES  </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">NULL</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> inputTime   </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">datetime</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> YES  </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">NULL</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> content     </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> blob         </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> YES  </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">NULL</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> recommended </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">bit</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);">)       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> YES  </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">NULL</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> categories  </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">255</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> YES  </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">NULL</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">-----------+--------------+------+-----+---------+-------+</span></div><br />2.代码<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">        <b>//插入</b><br />        Session sess </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  HibernateSessionFactory.getSession();<br />        Transaction tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sess.beginTransaction();<br />        LotteryNew nn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> LotteryNew();<br />        nn.setInputTime(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Date());<br />        nn.setCategories(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">t</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        nn.setTitle(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">new</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        nn.setRecommended(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br />        Blob bo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Hibernate.createBlob(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ggggg</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.getBytes());<br />        nn.setContent(bo);<br />        sess.save(nn);<br />        tr.commit();<br />        HibernateSessionFactory.closeSession();<br />        <br />        //修改<br />        sess </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  HibernateSessionFactory.getSession();<br />        tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sess.beginTransaction();<br />        LotteryNew lo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (LotteryNew) sess.get(LotteryNew.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">, nn.getId());<br />        Blob bog </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Hibernate.createBlob(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">xxxxx</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.getBytes());<br />        lo.setContent(bog);<br />        tr.commit();<br />        HibernateSessionFactory.closeSession();<br />        <br />        //查找<br />        sess </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  HibernateSessionFactory.getSession();<br />        LotteryNew lo2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (LotteryNew) sess.get(LotteryNew.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">, lo.getId());<br />        InputStream in </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> lo2.getContent().getBinaryStream() ;<br />        </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">[] bbr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">[in.available()];<br />        in.read(bbr);<br />        System.out.println(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> String(bbr));<br />        HibernateSessionFactory.closeSession();</span></div><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/178122.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-01-28 09:53 <a href="http://www.blogjava.net/Good-Game/archive/2008/01/28/178122.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hbn 继承关系映射_1</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/26/170536.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Wed, 26 Dec 2007 03:41:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/26/170536.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/170536.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/26/170536.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/170536.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/170536.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 尝试着继承关系和xdoclet使用后，对数据库面向对象的使用更近一步。这次例子从开始到结束，都没有接触面向结构的数据库设计；并完全在代码中以面向对象的形式来设计的。hbn 的继承映射关系有这几种；继承关系类共同使用一个表； 每个子类一个表(mapping全)； 每个具体内一个表（mapping只有父类）。先说说3把，感觉这点最好用-》》继承关系描述：  Animals父类 -- Bird ...&nbsp;&nbsp;<a href='http://www.blogjava.net/Good-Game/archive/2007/12/26/170536.html'>阅读全文</a><img src ="http://www.blogjava.net/Good-Game/aggbug/170536.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-26 11:41 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/26/170536.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate + Myeclipes 敏捷(小例)</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/11/167003.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Tue, 11 Dec 2007 09:51:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/11/167003.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/167003.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/11/167003.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/167003.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/167003.html</trackback:ping><description><![CDATA[对hbn深入过程中，发现开发和设计持久层 到项目后期，越来越困难。在次仔细查分析。特总结一种开发方法。留下与大家分享，欢迎拍砖。<br /><font color="#ffa500"><br />开发过程描述：</font><br />1.使用 MyEclipes -&gt; uml  创建类图 <br />2.用 Generate java Code 根据类图生成 java文件<br />3.使用 Xdoclet 添加 Hbn 标签<br />4.配置myEclipes -&gt; XDoclet 自动生成 mapping.hbn.xml<br />5.使用myEclipes 把项目转化成 hibernate  项目<br />6.使用 org.hibernate.tool.hbm2ddl.SchemaExport 建表<br /><font color="#ffa500"><br />开发过程好处：</font><br />1）完全是面向对象，不需要写xml配置文件(XDoclet);<br />2)项目后期修改容易面对uml <br />3）用myEclipes 这些都不用去找，直接拿来用(uml,XDoclet,hibernate ..)<br /><br /><font color="#ffa500">下面就来个 小例把 <br /></font>1.MyEclipes 使用 uml 参考-&gt;<a id="viewpost1_TitleUrl" class="singleposttitle" href="/beansoft/archive/2007/08/21/138445.html">MyEclipse 5.5 UML 入门视频</a> (<a id="Header1_HeaderTitle" class="headermaintitle" href="/beansoft/">作者：BeanSoft</a>)<br /><img src="http://www.blogjava.net/images/blogjava_net/Good-Game/28028/r_a.bmp" height="342" width="339" /><br /><br />2.由uml生成类文件 <br /><img src="http://www.blogjava.net/images/blogjava_net/Good-Game/28028/r_a2.bmp" /><br /><br />3.先使用 eclipes的 快键方法写 get/set 方法， 类文件文件添加 hbn XDoclet的注解<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> bean;<br /><br /></span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> <br /> * @hibernate.class table="t1oo"<br /> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> T1oo {<br /><br />  </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> id;<br />  </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String name;<br />  </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> avg;<br />  <br />  </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> <br />   * @hibernate.property <br />   * column="avg"<br />   * length="4"<br />   * not-null="true"<br />   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getAvg() {<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> avg;<br />}<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> setAvg(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> avg) {<br />    </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.avg </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> avg;<br />}<br /></span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br /> * @hibernate.id <br /> * column="id"<br /> * generator-class="hilo"<br /> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getId() {<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> id;<br />}<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> setId(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> id) {<br />    </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.id </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> id;<br />}<br /></span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br /> * @hibernate.property <br /> * column="name"<br /> * not-null="true" <br /> * </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String getName() {<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> name;<br />}<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> setName(String name) {<br />    </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.name </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> name;<br />}<br />  <br /><br />}</span></div><br />4.用myEclipes 生成 XDoclet<br />在项目点右键-&gt; properties -&gt; MyEclipse-XDoclet -&gt;<br />在Configuration 空白初点右键 选 add standard -&gt; ... hbn 后面不太好描述 可以查下很简单的 。配置好了运行后就可以看见 多了 个 T1oo.hbm.xml 文件;<br /><br />5.myEclipes + hbn 就不多说了<br />6. hbn2java:<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testCreateTable()</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> Exception{<br />         HibernateSessionFactory.currentSession();<br />         HibernateSessionFactory.closeSession();<br />         <br />         Field[] ff </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> HibernateSessionFactory.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">.getDeclaredFields();<br />         Field fie </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">ff.length;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">){<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( ff[i].getType().equals( Configuration.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ) ){<br />                 fie </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ff[i];<br />             }<br />         }<br />         fie.setAccessible(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br />         Configuration cfg </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Configuration)fie.get(HibernateSessionFactory.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />         cfg.addInputStream( </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.getClass().getResourceAsStream(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/bean/T1oo.hbm.xml</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) );<br />         <font color="#006400">//建表</font><br />          SchemaExport dbExport </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> SchemaExport(cfg);<br />          dbExport.setOutputFile(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c:\\db\\test.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />          dbExport.create(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">); <br />    }</span></div><br /><br />sql：<br /><font color="#a52a2a">drop table if exists t1oo<br />drop table if exists hibernate_unique_key<br />create table t1oo (<br />    id integer not null,<br />    avg integer not null,<br />    name varchar(255) not null,<br />    primary key (id)<br />)<br />create table hibernate_unique_key (<br />     next_hi integer <br />)<br />insert into hibernate_unique_key values ( 0 )<br /></font><br /><br />效果：<br />mysql&gt; show tables;<br />+----------------------+<br />| Tables_in_hbn        |<br />+----------------------+<br />| hibernate_unique_key |<br />| t1oo                 |<br />+----------------------+<br />2 rows in set (0.00 sec)<br /><br /><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/167003.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-11 17:51 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/11/167003.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate 查询语句总结</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/10/166625.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Mon, 10 Dec 2007 04:52:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/10/166625.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/166625.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/10/166625.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/166625.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/166625.html</trackback:ping><description><![CDATA[表关系 T1oo -&gt;ont-to-many-&gt; T2oo (t1oo.id-t2oo.aid)<br /><br />1.单条select延迟加载<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">        Iterator it </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> session.createQuery("</span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> T1oo ").<font color="#a52a2a">iterate</font>();<br />        </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(it.hasNext()){<br />            T1oo t1 </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> (T1oo)it.</span><span style="color: rgb(0, 0, 255);">next</span><span style="color: rgb(0, 0, 0);">();<br />            t1.getName();<br />        }<br /></span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);">运行语句 n+1<br /><b>这就只加载 id </b><br />Hibernate: select t1oo0_.id as col_0_0_ from t1oo t1oo0_<br /><b>此是在 t1.getName(); 延迟加载的 </b><br />Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?<br />Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?<br /></span><span style="color: rgb(0, 128, 128);">*/</span></div><br />2.级连查询：<br />1)set排序  &lt;set ... order-by="avg desc" ...&gt; 从大到小<br />2）batch-size="10" 用法是 <br />    select * from t2oo  where aid in (?,?,?....)<br />3)<br /><br />如果想忽略延迟，并有一定逻辑全部加载，这有两中解决办法：<br />1).内连<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> t1oo t1oo0_ </span><span style="color: rgb(0, 0, 255);">inner</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">join</span><span style="color: rgb(0, 0, 0);"> t2oo t2ooset1_ </span><span style="color: rgb(0, 0, 255);">on</span><span style="color: rgb(0, 0, 0);"> t1oo0_.id</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);">t2ooset1_.aid;<br /><font color="#ffa500">+----+-----------+----+-----+------+---------+<br />| id | name      | id | avg | aid  | version |<br />+----+-----------+----+-----+------+---------+<br />|  1 | liukaiyi  |  1 |  23 |    1 |       1 |<br />|  1 | liukaiyi  |  2 |  24 |    1 |       1 |<br />|  1 | liukaiyi  |  3 |  25 |    1 |       1 |<br />|  2 | liukaiyi2 |  4 |  26 |    2 |       0 |<br />+----+-----------+----+-----+------+---------+</font><br /></span></div> <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">         Iterator it </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashSet(session.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><font color="#a52a2a"><span style="color: rgb(0, 0, 0);"><b><font color="#a52a2a">from T1oo t1 inner join fetch t1.t2ooSet t2</font></b><font color="#a52a2a"><b>where t2.id&lt;=3</b></font></span></font><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">).list()).iterator();<br />        </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(it.hasNext()){<br />            T1oo t1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (T1oo)it.next();<br />            System.out.println(t1.getName());<br />            <br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(Iterator itr</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">t1.getT2ooSet().iterator();itr.hasNext(); ){<br />                T2oo t2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (T2oo)itr.next();<br />                System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> t2.getAvg() );<br />            }<br />        }</span></div>结果是：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Hibernate: </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> t1oo0_.id </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> id0_, t2ooset1_.id </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> id1_, t1oo0_.name </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> name0_0_, t2ooset1_.version </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> version1_1_, t2ooset1_.</span><span style="color: rgb(255, 0, 255);">avg</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> avg1_1_, t2ooset1_.aid </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> aid1_1_, t2ooset1_.aid </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> aid0__, t2ooset1_.id </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> id0__ </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> t1oo t1oo0_ </span><span style="color: rgb(0, 0, 255);">inner</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">join</span><span style="color: rgb(0, 0, 0);"> t2oo t2ooset1_ </span><span style="color: rgb(0, 0, 255);">on</span><span style="color: rgb(0, 0, 0);"> t1oo0_.id</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);">t2ooset1_.aid </span><span style="color: rgb(0, 0, 255);">where</span><span style="color: rgb(0, 0, 0);"> t2ooset1_.id</span><span style="color: rgb(128, 128, 128);">&lt;=</span><span style="color: rgb(128, 0, 0); font-weight: bold;">3</span><span style="color: rgb(0, 0, 0);"><br />liukaiyi<br />  </span><span style="color: rgb(128, 0, 0); font-weight: bold;">24</span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(128, 0, 0); font-weight: bold;">23</span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(128, 0, 0); font-weight: bold;">25</span><span style="color: rgb(0, 0, 0);"><br /></span></div><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/166625.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-10 12:52 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/10/166625.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate cache2</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/07/166061.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Fri, 07 Dec 2007 07:36:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/07/166061.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/166061.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/07/166061.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/166061.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/166061.html</trackback:ping><description><![CDATA[在hibernate.cfg.xml 中添加缓存  t1oo 一对多 t2oo (t2ooSet)<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="cache.provider_class"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">org.hibernate.cache.EhCacheProvider</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="hibernate.cache.use_query_cache"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">true</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mapping </span><span style="color: rgb(255, 0, 0);">resource</span><span style="color: rgb(0, 0, 255);">="hbn/bean/T1oo.hbm.xml"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mapping </span><span style="color: rgb(255, 0, 0);">resource</span><span style="color: rgb(0, 0, 255);">="hbn/bean/T2oo.hbm.xml"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">class-cache </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="hbn.bean.T1oo"</span><span style="color: rgb(255, 0, 0);"> usage</span><span style="color: rgb(0, 0, 255);">="read-only"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">collection-cache </span><span style="color: rgb(255, 0, 0);">collection</span><span style="color: rgb(0, 0, 255);">="hbn.bean.T1oo.t2ooSet"</span><span style="color: rgb(255, 0, 0);"> usage</span><span style="color: rgb(0, 0, 255);">="read-only"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">class-cache </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="hbn.bean.T2oo"</span><span style="color: rgb(255, 0, 0);"> usage</span><span style="color: rgb(0, 0, 255);">="read-only"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/&gt;</span></div><br />在src根目录下 ehcache.xml <b><font color="#a52a2a"></font></b><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="UTF-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">ehcache</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">diskStore </span><span style="color: rgb(255, 0, 0);">path</span><span style="color: rgb(0, 0, 255);">="java.io.tmpdir"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">defaultCache<br />        </span><span style="color: rgb(255, 0, 0);">maxElementsInMemory</span><span style="color: rgb(0, 0, 255);">="10000" <font color="#006400">//最大缓存数目</font></span><span style="color: rgb(255, 0, 0);"><br />        eternal</span><span style="color: rgb(0, 0, 255);">="false"</span><font color="#006400">＜!-- 缓存是否持久 --＞</font><span style="color: rgb(255, 0, 0);"><br />        timeToIdleSeconds</span><span style="color: rgb(0, 0, 255);">="120" </span><font color="#006400">＜!-- 当缓存闲置n秒后销毁 --＞</font><span style="color: rgb(255, 0, 0);"><br />        timeToLiveSeconds</span><span style="color: rgb(0, 0, 255);">="120"</span><font color="#006400">＜!-- 当缓存存活n秒后销毁--＞</font><span style="color: rgb(255, 0, 0);"><br />        overflowToDisk</span><span style="color: rgb(0, 0, 255);">="true"</span><font color="#006400">＜!-- 是否保存到磁盘，当系统当机时--＞</font><span style="color: rgb(255, 0, 0);"><br />        diskPersistent</span><span style="color: rgb(0, 0, 255);">="false"</span><span style="color: rgb(255, 0, 0);"><br />        diskExpiryThreadIntervalSeconds</span><span style="color: rgb(0, 0, 255);">="120"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/&gt;<br /><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">cache<br />     </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="hbn.bean.T1oo"</span><span style="color: rgb(255, 0, 0);"><br />     maxElementsInMemory</span><span style="color: rgb(0, 0, 255);">="450"</span><span style="color: rgb(255, 0, 0);"><br />     eternal</span><span style="color: rgb(0, 0, 255);">="false"</span><span style="color: rgb(255, 0, 0);"><br />     timeToLiveSeconds</span><span style="color: rgb(0, 0, 255);">="600"</span><span style="color: rgb(255, 0, 0);"><br />     overflowToDisk</span><span style="color: rgb(0, 0, 255);">="true"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">ehcache</span><span style="color: rgb(0, 0, 255);">&gt;</span></div><br />测试：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testCa()</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> Exception{<br />        System.out.println( getT1ooAll() );<br />        <br />        Thread.sleep(</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">);<br />        <br />        System.out.println( </span><span style="color: rgb(0, 0, 0);">getT1ooAll</span><span style="color: rgb(0, 0, 0);">() );<br />    }</span></div><br />控制台输出<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Hibernate: </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> t1oo0_.id </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> id, t1oo0_.name </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> name0_ </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> t1oo t1oo0_ limit ?<br /><br />Hibernate: </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> t2ooset0_.aid </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> aid1_, t2ooset0_.id </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> id1_, t2ooset0_.id </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> id0_, t2ooset0_.version </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> version1_0_, t2ooset0_.</span><span style="color: rgb(255, 0, 255);">avg</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> avg1_0_, t2ooset0_.aid </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> aid1_0_ </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> t2oo t2ooset0_ </span><span style="color: rgb(0, 0, 255);">where</span><span style="color: rgb(0, 0, 0);"> t2ooset0_.aid</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);">?<br />Hibernate: </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> t2ooset0_.aid </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> aid1_, t2ooset0_.id </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> id1_, t2ooset0_.id </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> id0_, t2ooset0_.version </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> version1_0_, t2ooset0_.</span><span style="color: rgb(255, 0, 255);">avg</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> avg1_0_, t2ooset0_.aid </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> aid1_0_ </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> t2oo t2ooset0_ </span><span style="color: rgb(0, 0, 255);">where</span><span style="color: rgb(0, 0, 0);"> t2ooset0_.aid</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);">?<br /></span><span style="color: rgb(128, 0, 0); font-weight: bold;">24</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(128, 0, 0); font-weight: bold;">23</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(128, 0, 0); font-weight: bold;">25</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(128, 0, 0); font-weight: bold;">2</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);"></span><span style="color: rgb(0, 0, 0);"><br /><font color="#006400">//在这缓存成功 没向数据库提交 sql语句 </font><br /></span><span style="color: rgb(128, 0, 0); font-weight: bold;">24</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(128, 0, 0); font-weight: bold;">23</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(128, 0, 0); font-weight: bold;">25</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(128, 0, 0); font-weight: bold;">2</span></div><br /><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/166061.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-07 15:36 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/07/166061.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate 多服务器数据同步问题（支持延迟同步）</title><link>http://www.blogjava.net/Good-Game/archive/2007/12/03/164813.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Mon, 03 Dec 2007 03:29:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/12/03/164813.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/164813.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/12/03/164813.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/164813.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/164813.html</trackback:ping><description><![CDATA[本文是本人实际开发中遇到，特留文记录。在次我花了3天的时间解决，这个问题还是点难度的。<br /><br /><b><font color="#ffa500">所用到知识点：<br /></font></b><font color="#ffa500"><font color="#000000">一般jdk <br />|-私有属性反射<br />|-序列化<br />|-正则表达使用<br />|-多线程使用<br />|-dom4j的xml读取<br />|+hibernate <br />   |-拦截器<br />   |-一个Session工厂同时连接不同数据库（<b><font color="#a52a2a">本文关键</font></b>）<br />   |-oracle Blob 存取<br /><br />等........<br /><br /><b><font color="#ffa500">需求功能介绍：<br /></font></b><font color="#ffa500"><font color="#000000"> 为性能考虑，单一服务器改成集群（每太服务器数据允许在一定时间内保持相步），给出的修改时间短，不过代码持久层比较统一（hibernate 感谢天还好是她！ ）。网络连接不稳定（铁路内网！）。<br /><br /><b><font color="#ffa500">完成后效果：<br />  </font></b><font color="#ffa500"><font color="#000000">当网络连接成功时，多数据库的同步。<br />   当</font></font></font></font></font></font><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000">网络连接失败时，本地应用程序运用hibernate拦截器拦截正操作对象并记录下操作动作，序列化到本地时局库 z_jcyy_tb 表中。表数据属性为：id,inputdate</font></font></font></font></font></font>(记录时间),object（序列对象）,action(操作动作)。并安一定时间测试连接。如果成功，读取 <font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000">z_jcyy_tb 表中数据 反序列化 再同步到 其他数据库中。<br /><br /><b><font color="#ffa500">代码说明：<br /></font></b><font color="#ffa500"><font color="#000000">1.新Session 建立<br />   </font></font></font></font></font></font></font></font><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000">hibernate.cfg.xml 在文件</font></font></font></font><font><font><font><font><font><font><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><span style="color: rgb(0, 0, 0);">&lt;session-factory&gt;</span></font></font></font></font></font></font></font></font></font></font></font></font><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000">中添加 <br /></font></font></font></font><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);"></span><span style="color: rgb(0, 0, 255);"></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="connection.url_b"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">jdbc:oracle:thin:@192.168.1.114:1521:JCYY</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="connection.username_b"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">jcyy</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="connection.password_b"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">jcyy</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">property</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span></div></font></font></font></font></font></font><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000">   TBDao -&gt; OpenSession() <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> String url_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> String use_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> String pass_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> String dirver_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />    </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> {</span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br /><font color="#006400">        //取得hibernate.cfg.xml逻辑路径，和原来程序关联上   </font><br />        Field field   </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">   SessionManager.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">.getDeclaredField(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">CONFIG_FILE_LOCATION</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        field.setAccessible( </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);"> );<br />        String path  </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  (String) field.get(SessionManager. </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> );<br /><font color="#006400"><br />        //通过 dom4j 加载 配置文件     </font><br />        Document docT </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> SAXReader().read( TBDao.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">.getResourceAsStream(path) );<br /><br /><font color="#006400">         //正则+xpath读取 在hbn文件中加入的</font></span><font><font><font><font><font><font><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><font color="#006400"><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="..._b"</span><span style="color: rgb(0, 0, 255);">&gt; </span></font><span style="color: rgb(0, 0, 0);"></span></font></font></font></font></font></font></font></font></font></font></font></font><span style="color: rgb(0, 0, 0);"><font color="#008000">的属性</font><br />        String xpath </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/hibernate-configuration/session-factory/property[@name='XPATH_I']</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ;<br />        Pattern p </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Pattern.compile(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">(XPATH_I)</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        Matcher ma </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> p.matcher(xpath);<br />        url_b  </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DocumentHelper.createXPath( ma.replaceAll(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">connection.url_b</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) ).selectSingleNode(docT).getText();<br />        use_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DocumentHelper.createXPath( ma.replaceAll(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">connection.username_b</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)).selectSingleNode(docT).getText();<br />        pass_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DocumentHelper.createXPath( ma.replaceAll(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">connection.password_b</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)).selectSingleNode(docT).getText();<br />        dirver_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DocumentHelper.createXPath( ma.replaceAll(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">connection.driver_class</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)).selectSingleNode(docT).getText();<br />    } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {e.printStackTrace();}}<br />    <br />    <font color="#006400">//利用hbn的SessionFactory得到 openSession(Connection); 打开异地数据库连接。 <br />    //利用私有反射得到 加载完成的SessionFactory <br /></font></span><font><font><font><font><font color="#ffa500"><font color="#000000"><font color="#ffa500"><font color="#000000"><span style="color: rgb(0, 0, 0);"></span></font></font></font></font></font></font></font></font><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Session openSessionb(){<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            Class.forName(dirver_b);<br />            Connection conn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  DriverManager.getConnection(url_b,use_b,pass_b);<br />            <br />            Field[] fields   </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">   SessionManager.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">.getDeclaredFields();<br />            Field field </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fields.length;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( SessionFactory.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">.equals(  fields[i].getType() )  )<br />                    field </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fields[i];<br />            }<br />            field.setAccessible(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br />            SessionFactory sessionFactory  </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  (SessionFactory) field.get(SessionManager.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> );<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> sessionFactory.openSession(conn);<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">--没有连接到总服务(openSessionb)--</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        }<br />    }<br /></span></div> <br />2.异地数据同步失败后动作  TBDao-&gt;save() 冻结状态到数据库<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> save(Object obj,String action) {<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> SessionManager.currentSession(</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">);<br />            Transaction tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />            ZJcyyTb zj </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ZJcyyTb();<br />            zj.setAction(action);<br />            zj.setInputdate(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Date());<br />            session.save(zj);<br />            session.flush();<br />            session.refresh(zj,LockMode.UPGRADE);<br />           <font color="#006400"> //oracle Blob数据持久 请参考--&gt;</font></span><a id="viewpost1_TitleUrl" class="singleposttitle" href="/Good-Game/archive/2007/11/29/164051.html">序列化和反序列化对象到 数据库</a><br /><span style="color: rgb(0, 0, 0);">            zj.setObject( </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ObjectConvert().ObjectToBlob(obj) );<br />            tr.commit();<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            e.printStackTrace();<br />        }</span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(session</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);"> session.isOpen() )session.close();<br />        }<br /><br />    }<br /></span></div><br />3,失败后又成功连接后 （线程实现） TBDao-&gt;action()<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> isSql(){<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> is_count </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> ;<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            <font color="#006400">//得到本地Session  查看是否有连接失败后序列动作被保存</font><br />            session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> SessionManager.currentSession(</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">);<br />            Transaction tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  session.beginTransaction();<br />            Connection conn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.connection();<br />            Statement stat </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> conn.createStatement();<br />            ResultSet rs </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> stat.executeQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select count(*) from z_jcyy_tb</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />            rs.next();<br />            is_count </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> rs.getInt(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />            tr.commit();<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            e.printStackTrace();<br />        }</span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(session</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);"> session.isOpen() )session.close();<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> is_count ;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> action(){<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> isSql </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> ;<br />        ObjectConvert oc </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ObjectConvert();<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        Session session_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        <br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />           <font color="#006400"> //有失败连接动作后尝试 远程数据库   </font><br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( (isSql</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">isSql())</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> ){<br />                session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> SessionManager.currentSession(</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">);<br /><font color="#006400">                //远程数据库连接  <br />                //如果成功连接：z_jcyy_tb表中数据同步到其他数据库中</font><br />                session_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> openSessionb();<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(session_b</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">){<br />                    Transaction tr_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session_b.beginTransaction();<br />                    Transaction tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />                    <br />                    Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> from ZJcyyTb t order by t.inputdate</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />                    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">isSql</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">){<br />                        qu.setFirstResult(i</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">);<br />                        qu.setMaxResults(</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">);<br />                        List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> qu.list();                <br />                        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(Iterator it</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">list.iterator();it.hasNext();){<br />                            ZJcyyTb tb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (ZJcyyTb)it.next();<br />                            Object obj </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />                            obj </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> oc.BlobToObject(tb.getObject(),obj);<br />                            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(obj</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">){<br />                                String action </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tb.getAction();<br />                                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(action.equals( TBDao.DELETE )){<br />                                    session_b.delete(obj);<br />                                }<br />                                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(action.equals( TBDao.INSERT )){<br />                                    session_b.save(obj);<br />                                }<br />                                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(action.equals( TBDao.UPDATE )){<br />                                    session_b.update(obj);<br />                                }<br />                            }<br />                            session.delete(tb);<br />                            tr.commit();<br />                        }<br />                    }<br />                    tr_b.commit();<br />                }<br />            }    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);"> ;<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">--没有连接到总服务(action)--</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }</span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(session_b</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">session_b.isOpen())session_b.close();<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(session</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);"> session.isOpen() )session.close();<br />            SessionManager.closeSession();    <br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);"> ;<br />    }</span></div><br />4.hbn 拦截器 -&gt;Interceptor <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> com.jjm.hibernate;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.io.File;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.io.FileInputStream;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.io.InputStream;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.io.Serializable;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.lang.reflect.Field;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.sql.Connection;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.sql.DriverManager;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.HashSet;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.Iterator;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.Set;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.dom4j.Document;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.dom4j.DocumentHelper;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.dom4j.XPath;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.dom4j.io.SAXReader;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.CallbackException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.EntityMode;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.Hibernate;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.HibernateException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.Interceptor;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.Session;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.SessionFactory;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.Transaction;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.cfg.Configuration;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.hibernate.type.Type;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.jjm.rlzy.dao.TBDao;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> TestInterceptor </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> Interceptor,Serializable{<br />    <br />    </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> isConn_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);"> ;<br />    </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> TBDao tb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> TBDao();<br /><font color="#006400">    //线程 一分钟 检测连接失败 同步 </font><br />    </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">{<br />        </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Thread(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Runnable(){<br />            </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> run() {<br />                </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">){<br />                    isConn_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tb.action();<br />                    </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />                        Thread.sleep(</span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">);<br />                    } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (InterruptedException e) {e.printStackTrace();}<br />                }<br />            }<br />        }).start();<br />    }<br /></span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> CallbackException {<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isConn_b){<br />                session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  tb.openSessionb();<br />                Transaction tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />                session.update(entity);<br />                tr.commit();<br />            }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />                tb.save(entity,TBDao.UPDATE);<br />            }<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            e.printStackTrace() ;<br />            tb.save(entity,TBDao.UPDATE);<br />            isConn_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);"> ;<br />        }</span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(session</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)session.close();<br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 拦截器中 绝对不能有这句 -&gt;  SessionManager.closeSession();</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> CallbackException {<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isConn_b){<br />                session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  tb.openSessionb();<br />                Transaction tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />                session.save(entity);<br />                tr.commit();<br />            }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />                tb.save(entity,TBDao.INSERT);<br />            }<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            e.printStackTrace() ;<br />            tb.save(entity,TBDao.INSERT);<br />            isConn_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);"> ;<br />        }</span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(session</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)session.close();</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> CallbackException {<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(isConn_b){<br />                session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  tb.openSessionb();<br />                Transaction tr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />                session.delete(entity);<br />                tr.commit();<br />            }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />                tb.save(entity,TBDao.DELETE);<br />            }<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            e.printStackTrace() ;<br />            tb.save(entity,TBDao.DELETE);<br />            isConn_b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);"> ;<br />        }</span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">{<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(session</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)session.close();</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        }<br />    }<br /><br />  ................................</span><span style="color: rgb(0, 0, 0);"><br /><br />}<br /></span></div><br /><br /><b><font color="#ffa500">后记：<br /><font color="#000080">由于</font><font color="#000000"><font color="#000080">一些原因代码写的有些简陋，但功能实现，表达的的意思也还可以（自己有点 ‘买瓜了’哈哈！）。我写出来的目的希望大家能共同进步，这等大家拍砖了 ：）</font><br /></font></font></b></font></font><br /></font><br /><br />          <br /></font><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/164813.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-12-03 11:29 <a href="http://www.blogjava.net/Good-Game/archive/2007/12/03/164813.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hbn 拦截器</title><link>http://www.blogjava.net/Good-Game/archive/2007/11/29/163922.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Thu, 29 Nov 2007 02:13:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/11/29/163922.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/163922.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/11/29/163922.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/163922.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/163922.html</trackback:ping><description><![CDATA[
		<font color="#ffa500">
				<b>拦截器</b>
				<br />
				<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 255);">package</span>
						<span style="color: rgb(0, 0, 0);"> hbn.test.supper.Interceptor;<br /><br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> java.io.Serializable;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> java.util.HashSet;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> java.util.Iterator;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> java.util.Set;<br /><br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> org.hibernate.CallbackException;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> org.hibernate.EntityMode;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> org.hibernate.Interceptor;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> org.hibernate.Transaction;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> org.hibernate.type.Type;<br /><br /></span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);"> TestInterceptor </span>
						<span style="color: rgb(0, 0, 255);">implements</span>
						<span style="color: rgb(0, 0, 0);"> Interceptor,Serializable{<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">private</span>
						<span style="color: rgb(0, 0, 0);"> Set inserts </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> HashSet();<br />    </span>
						<span style="color: rgb(0, 0, 255);">private</span>
						<span style="color: rgb(0, 0, 0);"> Set updates </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> HashSet();<br />    <br />    </span>
						<font>
								<font color="#ffa500">
										<span style="color: rgb(0, 0, 0);">
										</span>
										<span style="color: rgb(0, 128, 0);">//</span>
										<span style="color: rgb(0, 128, 0);">Session初化一个持久对象 如果这方法中改变了对象属性就返回true 否则null</span>
										<span style="color: rgb(0, 128, 0);">
										</span>
										<span style="color: rgb(0, 0, 0);">
										</span>
								</font>
						</font>
						<br />
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">boolean</span>
						<span style="color: rgb(0, 0, 0);"> onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {<br />    </span>
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">false</span>
						<span style="color: rgb(0, 0, 0);">;<br />    }<br /></span>
						<font>
								<font color="#ffa500">
										<span style="color: rgb(0, 0, 0);">
										</span>
										<span style="color: rgb(0, 128, 0);">    //</span>
										<span style="color: rgb(0, 128, 0);">Session flush()中检查到脏数据是调用 如：tr.commit() ....</span>
								</font>
						</font>
						<br />
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">boolean</span>
						<span style="color: rgb(0, 0, 0);"> onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {<br />        updates.add(entity);<br />        </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">false</span>
						<span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span>
						<font>
								<font color="#ffa500">
										<span style="color: rgb(0, 0, 0);">
										</span>
										<span style="color: rgb(0, 128, 0);">//</span>
										<span style="color: rgb(0, 128, 0);">Session Save() 当修改了对象属性返回true </span>
								</font>
						</font>
						<br />
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">boolean</span>
						<span style="color: rgb(0, 0, 0);"> onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {<br />        <br />        inserts.add(entity);<br />        </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">false</span>
						<span style="color: rgb(0, 0, 0);">;<br />    }<br />     </span>
						<font>
								<font color="#ffa500">
										<span style="color: rgb(0, 0, 0);">
										</span>
										<span style="color: rgb(0, 128, 0);">//</span>
										<span style="color: rgb(0, 128, 0);">delete </span>
								</font>
						</font>
						<br />
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {</span>
						<span style="color: rgb(0, 0, 0);">
								<br />    }<br /></span>
						<font>
								<font color="#ffa500">
										<span style="color: rgb(0, 0, 0);"> </span>
										<span style="color: rgb(0, 128, 0);">   //flush() 之前调用</span>
										<span style="color: rgb(0, 128, 0);">
										</span>
								</font>
						</font>
						<br />
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> preFlush(Iterator entities) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {</span>
						<span style="color: rgb(0, 0, 0);">
								<br />    }<br />     </span>
						<font>
								<font color="#ffa500">
										<span style="color: rgb(0, 0, 0);">
										</span>
										<span style="color: rgb(0, 128, 0);">//</span>
										<span style="color: rgb(0, 128, 0);">flush() 执行SQL语句之后调用 </span>
								</font>
						</font>
						<br />
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> postFlush(Iterator entities) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {<br />        <br />        </span>
						<span style="color: rgb(0, 0, 255);">try</span>
						<span style="color: rgb(0, 0, 0);"> {<br />            </span>
						<span style="color: rgb(0, 0, 255);">for</span>
						<span style="color: rgb(0, 0, 0);">(Iterator it </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> updates.iterator();it.hasNext();){<br />                System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">update=</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">+</span>
						<span style="color: rgb(0, 0, 0);"> it.next() );    <br />            }<br />            </span>
						<span style="color: rgb(0, 0, 255);">for</span>
						<span style="color: rgb(0, 0, 0);">(Iterator it </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> inserts.iterator();it.hasNext();){<br />                System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">insert</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">+</span>
						<span style="color: rgb(0, 0, 0);"> it.next() );    <br />            }<br />            <br />        } </span>
						<span style="color: rgb(0, 0, 255);">catch</span>
						<span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            e.printStackTrace();<br />        }<br />        <br />    }<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> Boolean isTransient(Object entity) {<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">null</span>
						<span style="color: rgb(0, 0, 0);">;<br />    }<br /></span>
						<font>
								<font color="#ffa500">
										<span style="color: rgb(0, 0, 0);"> </span>
										<span style="color: rgb(0, 128, 0);">  //决定Session中那些对象是脏数据 如果null Session使用默认处理脏数据</span>
										<span style="color: rgb(0, 128, 0);">
										</span>
								</font>
						</font>
						<br />
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">int</span>
						<span style="color: rgb(0, 0, 0);">[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {</span>
						<span style="color: rgb(0, 128, 0);">
						</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">null</span>
						<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />     </span>
						<font>
								<font color="#ffa500">
										<span style="color: rgb(0, 0, 0);">
										</span>
										<span style="color: rgb(0, 128, 0);">// 当Session构造实体类对象前调用</span>
										<span style="color: rgb(0, 128, 0);">
										</span>
								</font>
						</font>
						<br />
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> Object instantiate(String entityName, EntityMode entityMode, Serializable id) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {</span>
						<span style="color: rgb(0, 128, 0);">
						</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">null</span>
						<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> String getEntityName(Object object) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">null</span>
						<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> Object getEntity(String entityName, Serializable id) </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> CallbackException {<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">null</span>
						<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> afterTransactionBegin(Transaction tx) {<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        <br />    }<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> beforeTransactionCompletion(Transaction tx) {<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        <br />    }<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> afterTransactionCompletion(Transaction tx) {<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        <br />    }<br /><br />}<br /></span>
				</div>
				<b>
						<br />测试</b>
				<br />
				<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 255);">package</span>
						<span style="color: rgb(0, 0, 0);"> hbn.test.supper.Interceptor;<br /><br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> java.lang.reflect.Field;<br /><br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> org.hibernate.Session;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> org.hibernate.SessionFactory;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> org.hibernate.Transaction;<br /><br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> hbn.HibernateSessionFactory;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> hbn.bean.T2oo;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> junit.framework.TestCase;<br /><br /></span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);"> TestIC </span>
						<span style="color: rgb(0, 0, 255);">extends</span>
						<span style="color: rgb(0, 0, 0);"> TestCase {<br />    </span>
						<span style="color: rgb(0, 0, 255);">private</span>
						<span style="color: rgb(0, 0, 0);"> SessionFactory sessionFactory;<br />    </span>
						<span style="color: rgb(0, 0, 255);">protected</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> setUp() </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> Exception {<br />        </span>
						<span style="color: rgb(0, 0, 255);">super</span>
						<span style="color: rgb(0, 0, 0);">.setUp();<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">利用java反射得到 HibernateSessionFactory -&gt;<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">private  static org.hibernate.SessionFactory sessionFactory;<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">要模拟 并发 要 HibernateSessionFactory 得出的 有 threadLocal 不行 </span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        HibernateSessionFactory.currentSession();<br />        HibernateSessionFactory.closeSession();<br />        Field field </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> HibernateSessionFactory.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">.getDeclaredField(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">sessionFactory</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        field.setAccessible(</span>
						<span style="color: rgb(0, 0, 255);">true</span>
						<span style="color: rgb(0, 0, 0);">);<br />        sessionFactory </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> (SessionFactory) field.get(HibernateSessionFactory.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">);<br />    }<br />    <br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> testInc() </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> Exception {<br />        TestInterceptor intx </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> TestInterceptor();<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">加载拦截器</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        Session session </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession(intx);<br />        <br />        Transaction tr </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        T2oo t2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> T2oo(</span>
						<span style="color: rgb(0, 0, 0);">23</span>
						<span style="color: rgb(0, 0, 0);">);<br />        session.save(t2);<br />        t2.setAvg(</span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> Integer(</span>
						<span style="color: rgb(0, 0, 0);">99</span>
						<span style="color: rgb(0, 0, 0);">));<br />        tr.commit();<br />    }<br />}<br /></span>
				</div>结果<br /><font color="#006400">Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />Hibernate: update t2oo set version=?, avg=?, aid=? where id=? and version=?<br />//拦截到的<br />update=hbn.bean.T2oo@277<br />inserthbn.bean.T2oo@277</font><br /><b><br /><br /></b></font>
<img src ="http://www.blogjava.net/Good-Game/aggbug/163922.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-11-29 10:13 <a href="http://www.blogjava.net/Good-Game/archive/2007/11/29/163922.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate 问题_3(锁)</title><link>http://www.blogjava.net/Good-Game/archive/2007/11/26/163310.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Mon, 26 Nov 2007 10:57:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/11/26/163310.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/163310.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/11/26/163310.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/163310.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/163310.html</trackback:ping><description><![CDATA[
		<b>
				<font color="#ffa500">环境:<br /></font>
		</b>
		<font color="#ffa500">
				<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 0, 255);">private</span>
						<span style="color: rgb(0, 0, 0);"> SessionFactory sessionFactory;<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">protected</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> setUp() </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> Exception {<br />        </span>
						<span style="color: rgb(0, 0, 255);">super</span>
						<span style="color: rgb(0, 0, 0);">.setUp();<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">利用java反射得到 HibernateSessionFactory -&gt;<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">private  static org.hibernate.SessionFactory sessionFactory;<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">要模拟 并发 要 HibernateSessionFactory 得出的 有 threadLocal 不行 <br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">要 </span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        HibernateSessionFactory.currentSession();<br />        HibernateSessionFactory.closeSession();<br />        Field field </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> HibernateSessionFactory.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">.getDeclaredField(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">sessionFactory</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        field.setAccessible(</span>
						<span style="color: rgb(0, 0, 255);">true</span>
						<span style="color: rgb(0, 0, 0);">);<br />        sessionFactory </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> (SessionFactory) field.get(HibernateSessionFactory.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">);<br />    }<br /><br />    </span>
						<span style="color: rgb(0, 0, 255);">protected</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> tearDown() </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> Exception {<br />        </span>
						<span style="color: rgb(0, 0, 255);">super</span>
						<span style="color: rgb(0, 0, 0);">.tearDown();<br />    }</span>
				</div>
		</font>
		<b>
				<font color="#ffa500">
						<br />悲观锁:<br /></font>
		</b>
		<font color="#ffa500">
				<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 128, 0);">/**</span>
						<span style="color: rgb(0, 128, 0);"> 悲观锁问题 线程模拟 并发 <br />     * Table -&gt; T1oo(id,name)<br />     *     +----+------+<br />     *    | id | name |<br />     *    +----+------+<br />     *    |  4 | xx1  |<br />     *    +----+------+<br />     </span>
						<span style="color: rgb(0, 128, 0);">*/</span>
						<span style="color: rgb(0, 0, 0);">
								<br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> ctestLock() </span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> Exception {<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);"> insert test Data 测试数据 </span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        Session seInsert </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />        Transaction tr </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> seInsert.beginTransaction();<br />        T1oo testUse </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> testUse </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> T1oo();<br />        testUse.setName(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">liukaiyi_test</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        seInsert.save(testUse);<br />        tr.commit();<br />        System.out.println( </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">**************Test Use Bean : insert*******************</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> );<br />        seInsert.close();<br />        Integer testId </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> testUse.getId();<br />        <br /> <font color="#006400">       //并发模拟</font><br />        Session session1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />        Session session2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />        <br />        </span>
						<span style="color: rgb(0, 0, 255);">final</span>
						<span style="color: rgb(0, 0, 0);"> Transaction tr1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session1.beginTransaction();<br />        Transaction tr2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session2.beginTransaction();<br />        <br />        Query qu1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session1.createQuery(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> from T1oo t1oo where t1oo.name='liukaiyi_test'</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        Query qu2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session2.createQuery(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> from T1oo t1oo where t1oo.name='liukaiyi_test'</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        <br />        <font color="#008000">//加悲观锁</font><br />        qu1.setLockMode(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">t1oo</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">,LockMode.UPGRADE);<br />        </span>
						<span style="color: rgb(0, 0, 255);">final</span>
						<span style="color: rgb(0, 0, 0);"> Object bean1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> qu1.uniqueResult();<br />        Object bean2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> qu2.uniqueResult();<br /><br />        T1oo t1oo2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> (T1oo)bean2;<br />        <br />        t1oo2.setName(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">run1</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        <br />        </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> Thread(<br />                </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> Runnable(){<br />                    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> run()  {<br />                        </span>
						<span style="color: rgb(0, 0, 255);">try</span>
						<span style="color: rgb(0, 0, 0);"> {<br />                            System.out.println( </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">********解锁准备*********</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> );<br />                            Thread.sleep(</span>
						<span style="color: rgb(0, 0, 0);">10</span>
						<span style="color: rgb(0, 0, 0);">*</span>
						<span style="color: rgb(0, 0, 0);">1000</span>
						<span style="color: rgb(0, 0, 0);">);<br />                            T1oo t1oo1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> (T1oo)bean1;<br />                            t1oo1.setName(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">run2</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />                            tr1.commit();<br />                            System.out.println( </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">********解锁成功t*********</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> );<br />                        } </span>
						<span style="color: rgb(0, 0, 255);">catch</span>
						<span style="color: rgb(0, 0, 0);"> (InterruptedException e) {e.printStackTrace();}<br />                    }<br />                }<br />        ).start();<br /><br />        </span>
						<font color="#ffa500">
								<span style="color: rgb(0, 0, 0);">
										<font color="#006400">/</font>
								</span>
						</font>
						<font color="#ffa500">
								<span style="color: rgb(0, 0, 0);">
										<font color="#006400">/这里会 等待十秒后 tr1解锁 /</font>
								</span>
						</font>
						<span style="color: rgb(0, 0, 0);">
								<br />        tr2.commit();<br />        <br />        session1.close();<br />        session2.close();<br />        <br />        <br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);"> delete Test Date 删除测试数据</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        Session seDelete </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />        Transaction trD </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> seDelete.beginTransaction();<br />        Object obj </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> seDelete.createQuery(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> from T1oo t where t.id=:id </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">).<br />                        setInteger(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">id</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">,testId.intValue()).<br />                        uniqueResult();<br />        seDelete.delete(obj);<br />        trD.commit();<br />        System.out.println( </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">**************Test Use Bean : delete**************</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> );<br />        seDelete.close();<br />    }</span>
				</div>
		</font>
		<b>
				<font color="#ffa500">
						<br />乐观锁:<br /></font>
		</b>
		<font color="#ffa500">
				<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 128, 0);">/**</span>
						<span style="color: rgb(0, 128, 0);"> 乐观锁问题<br />     * Table -&gt; T1oo(id,name)<br />     * T2oo.hbn.xml &lt;class&gt; +&gt; optimistic-lock="version"<br />     * id下面 +&gt; &lt;version name="version" type="integer" column="version" /&gt;<br />     *    +---------+---------+------+<br />     *    | Field   | Type    | Null |<br />     *    +---------+---------+------+<br />     *    | id      | int(11) |      |<br />     *    | avg     | int(11) |      |<br />     *    | aid     | int(11) | YES  |<br />     *    | version | int(11) | YES  |<br />     *    +---------+---------+------+<br />     *  注意：要在数据库中多加一列 <br />     *  mysql&gt; alter table T2OO add version int;<br />     </span>
						<span style="color: rgb(0, 128, 0);">*/</span>
						<span style="color: rgb(0, 0, 0);">
								<br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> testOpLock()</span>
						<span style="color: rgb(0, 0, 255);">throws</span>
						<span style="color: rgb(0, 0, 0);"> Exception{<br />       <font color="#006400"> //添加测试数据  </font><br />        Session session </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />        Transaction trI </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        Connection conn </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.connection();<br />        T2oo t2oo </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> T2oo();<br />        T1oo t1oo </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> T1oo(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">t1ooOpLock</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        t1oo.setT2ooSet(</span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> HashSet());<br />        t2oo.setAvg(</span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> Integer(</span>
						<span style="color: rgb(0, 0, 0);">23</span>
						<span style="color: rgb(0, 0, 0);">));    <br />        t2oo.setT1oo(t1oo);<br />        t1oo.getT2ooSet().add(t2oo);<br />        session.save(t2oo);<br />        trI.commit();<br />        session.clear();<br />        session.close();<br /><br />        <font color="#006400">//并发模拟</font><br />        Session se1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />        Session se2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />        <br />        Transaction tr1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> se1.beginTransaction();<br />        Transaction tr2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> se2.beginTransaction();<br />        <br />        T1oo obj1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> (T1oo)se1.load(T1oo.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">,</span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> Integer(</span>
						<span style="color: rgb(0, 0, 0);">1</span>
						<span style="color: rgb(0, 0, 0);">));<br />        T1oo obj2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> (T1oo)se2.load(T1oo.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">,</span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> Integer(</span>
						<span style="color: rgb(0, 0, 0);">1</span>
						<span style="color: rgb(0, 0, 0);">));<br />        <br />        ((T2oo)obj1.getT2ooSet().iterator().next()).setAvg(</span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> Integer(</span>
						<span style="color: rgb(0, 0, 0);">9</span>
						<span style="color: rgb(0, 0, 0);">));<br />        ((T2oo)obj2.getT2ooSet().iterator().next()).setAvg(</span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> Integer(</span>
						<span style="color: rgb(0, 0, 0);">10</span>
						<span style="color: rgb(0, 0, 0);">));<br />                <br />        tr1.commit();<br />        <br />        </span>
						<span style="color: rgb(0, 0, 255);">try</span>
						<span style="color: rgb(0, 0, 0);"> {<br />            tr2.commit();<br />        } </span>
						<span style="color: rgb(0, 0, 255);">catch</span>
						<span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            se2.clear();<br />            tr2.commit();<br />            </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">tr2报错</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        }</span>
						<span style="color: rgb(0, 0, 255);">finally</span>
						<span style="color: rgb(0, 0, 0);">{<br />            se1.close();<br />            se2.close();<br />        }<br />        <br />        <br />       <font color="#006400"> //测试数据删除</font><br />        Session dele </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> sessionFactory.openSession();<br />        Transaction tr </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> dele.beginTransaction();<br />        dele.delete(obj1);<br />        tr.commit();<br />        dele.close();<br />        <br />        HibernateSessionFactory.closeSession();<br />    }</span>
				</div>
		</font>
		<b>
				<font color="#ffa500">
						<br />
				</font>
		</b>
<img src ="http://www.blogjava.net/Good-Game/aggbug/163310.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-11-26 18:57 <a href="http://www.blogjava.net/Good-Game/archive/2007/11/26/163310.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate 问题小结2 (inverse和cascade)</title><link>http://www.blogjava.net/Good-Game/archive/2007/11/16/161100.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Fri, 16 Nov 2007 11:20:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/11/16/161100.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/161100.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/11/16/161100.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/161100.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/161100.html</trackback:ping><description><![CDATA[
		<font color="#ffa500">
				<b>inverse 使用<font color="#000000"><br /></font></b>
		</font>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 0);">
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">说明约定-如：<br /></font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">
								<b>1.表属性 :</b>T1oo-&gt;id,name<br />
            T2oo-&gt;id,avg,aid(</font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">外键</font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">),version<br /><br /></font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">
								<b>2.代码过程是：</b>Save-&gt;t1oo对象(T1oo 外键 T2oo)<br />        T1oo t1oo = new T1oo();<br />        t1oo.setName("xx");        <br />        t1oo.setT2ooSet(new HashSet());<br />        T2oo t2oo2 = new T2oo(24);<br />        //t2oo2.setT1oo(t1oo); <font color="#a52a2a">(在下面的本用例表中的第3属性 )</font><br />        t1oo.getT2ooSet().add(t2oo2);</font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">
								<font color="#a52a2a">(在下面的本用例表中的第2属性 )</font>
						</font>
				</span>
				<br />
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">        session.save(t1oo);<br /></font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">
								<br />
								<b>3.本例表使用：</b>
								<br />
						</font>
						<font color="#0000ff">T1oo.hbm.xml</font> -&gt;<br />        &lt;set name="t2ooSet" inverse="true" cascade = "all"  &gt;</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">
								<font color="#a52a2a"> (在下面的本用例表中的第1，2属性 )</font>
						</font>
				</span>
				<br />
				<span style="color: rgb(128, 128, 128);">            &lt;key column="aid"/&gt;<br />            &lt;one-to-many class="T2oo"/&gt;<br />        &lt;/set&gt;<br /></span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#0000ff">T2oo.hbm.xml</font> -&gt;<br />        &lt;many-to-one name="t1oo" column="aid"  class="T1oo" cascade="all" /&gt;</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">
								<font color="#a52a2a">(在下面的本用例表中的第1，2属性 )</font>
						</font>
				</span>
				<br />
				<span style="color: rgb(128, 128, 128);">
						<font color="#0000ff">结合上面说明得表为：</font>
						<br />
				</span>
				<span style="color: rgb(128, 128, 128);">+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  | all   |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+</span>
				<span style="color: rgb(128, 128, 128);">
						<br />
				</span>
				<br />
				<span style="color: rgb(128, 128, 128);">
						<b>
								<font color="#006400">4.执行</font>
						</b>
				</span>
				<font color="#006400">
						<span style="color: rgb(0, 0, 0);">
								<b>
										<font color="#006400">Hibernate语句</font>
								</b>
								<font color="#006400">
										<b>：</b>
								</font>
						</span>
						<span style="color: rgb(0, 0, 0);">
						</span>
				</font>
				<br />
				<span style="color: rgb(128, 128, 128);">    Hibernate: insert into t1oo (name, id) values (?, ?)<br />    Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br /><b><font color="#006400">5.结果为：<br /></font></b><font color="#006400"><font color="#808080">mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  2 |  24 | <font color="#a52a2a">NULL </font>|       0 | <font color="#006400">//<font color="#ff0000"><b>主要就是看 aid属性</b></font></font><br />+----+-----+------+---------+<br />1 row in set (0.00 sec)<br /><br /><br />mysql&gt; select * from t1oo; <font color="#006400">//</font></font></font></span>
				<font color="#008000">
						<span style="color: rgb(128, 128, 128);">
								<font color="#006400">因为</font>
								<font color="#006400">T1oo总是可以 Save 下面就不再提了</font>
						</span>
				</font>
				<br />
				<span style="color: rgb(128, 128, 128);">
						<font color="#006400">
								<font color="#808080">+----+------+<br />| id | name |<br />+----+------+<br />|  2 | xx   |<br />+----+------+<br />1 row in set (0.00 sec)<br /><font color="#006400"><b><br />可改项：</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | <font color="#a52a2a">可改4</font> | <font color="#a52a2a">可改2</font>  |          |          |<br />+-------+-------+-------+           |  <font color="#a52a2a"> 可改1</font>  |<br />| t2oo  |       | <font color="#a52a2a">可改3</font> |           |          |<br />+-------+-------+-------+----------+----------+<br /><br /><br /><br /><font color="#ffa500"><b>主本:</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  | all   |          |          |<br />+-------+-------+-------+  true    |  true    |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  1 |  24 |   <b><font color="#ff0000"> 1</font></b> |       0 |<br />+----+-----+------+---------+<br />1 row in set (0.00 sec)<br /><br /><br /><b><font color="#ffa500">改1</font></b><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  | all   |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  1 |  24 |<b><font color="#ff0000"> NULL</font></b>|       0 |<br />+----+-----+------+---------+<br />1 row in set (0.00 sec)<br /><br /><b><font color="#ffa500">改2</font></b><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  |       |          |          |<br />+-------+-------+-------+  true    |  true    |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br /></font>
						</font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#808080">
								<font color="#ff0000">Empty set (0.00 sec)<br /><br /><font color="#808080"><font color="#ffa500"><b>改2  改1</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  |       |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br /></font></font>
						</font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#808080">
								<font color="#ff0000">Empty set (0.00 sec)<br /><br /><br /></font>
						</font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#ffa500">
								<b>改3</b>
						</font>
						<br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  | all   |          |          |<br />+-------+-------+-------+  true    |  true    |<br />| t2oo  |       |       |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  1 |  24 |    <font color="#ff0000">1</font> |       0 |<br />+----+-----+------+---------+<br />1 row in set (0.00 sec)<br /><br /><font color="#ffa500"><b>改3 改2</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  |       |          |          |<br />+-------+-------+-------+  true    |  true    |<br />| t2oo  |       |       |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br /></span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#808080">
								<font color="#ff0000">Empty set (0.00 sec)<br /><br /><br /></font>
						</font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#ffa500">
								<b>改3 改1</b>
						</font>
						<br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  | all   |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       |       |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  1 |  24 | <font color="#ff0000">NULL</font> |       0 |<br />+----+-----+------+---------+<br />1 row in set (0.00 sec)<br /><br /><font color="#ffa500"><b>改3 改2 改1</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | true  |       |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       |       |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br /><font color="#ff0000">Empty set (0.00 sec)</font><br /><br /><font color="#ffa500"><b>改4</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | false | all   |          |          |<br />+-------+-------+-------+  true    |  true    |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br />mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  1 |  24 |    <font color="#ff0000">1</font> |       0 |<br />+----+-----+------+---------+<br />1 row in set (0.02 sec)<br /><br /><b><font color="#ffa500">改4 改1</font></b><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | false | all   |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br />mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  1 |  24 |    <font color="#ff0000">1</font> |       0 |<br />+----+-----+------+---------+<br />1 row in set (0.00 sec)<br /><br /><font color="#ffa500"><b>改4 改2 </b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | false |       |          |          |<br />+-------+-------+-------+  true    |  true    |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br /><font color="#ff0000">Empty set (0.00 sec)</font><br /><br /><font color="#ffa500"><b>改4 改3</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | false | all   |          |          |<br />+-------+-------+-------+  true    |  true    |<br />| t2oo  |       |       |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br />mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  1 |  24 |   <font color="#ff0000"> 1</font> |       0 |<br />+----+-----+------+---------+<br />1 row in set (0.00 sec)<br /><font color="#ffa500"><b><br />改4 改3 改2</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | false |       |          |          |<br />+-------+-------+-------+  true    |  true    |<br />| t2oo  |       |       |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br /><font color="#ff0000">Empty set (0.00 sec)<br /><br /><font color="#808080">改4 改1 改2<br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | false |       |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       | all   |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br /></font></font></span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#ff0000">Empty set (0.00 sec)<br /><font color="#808080"><br /><br /><font color="#ffa500"><b>改4 改1 改3</b></font><br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | false |  all  |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       |       |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br />mysql&gt; select * from t2oo;<br />+----+-----+------+---------+<br />| id | avg | aid  | version |<br />+----+-----+------+---------+<br />|  1 |  24 |    <font color="#ff0000">1</font> |       0 |<br />+----+-----+------+---------+<br />1 row in set (0.02 sec)<br /><br />改4 改3 改2 改1<br />+-------+-------+-------+----------+----------+<br />|hbn.xml|inverse|cascade|t1oo-&gt;t2oo|t2oo-&gt;t1oo|<br />+-------+-------+-------+----------+----------+<br />| t1oo  | false |       |          |          |<br />+-------+-------+-------+  true    |  false   |<br />| t2oo  |       |       |          |          |<br />+-------+-------+-------+----------+----------+<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br /><font color="#ff0000">Empty set (0.00 sec)</font><br /></font></font>
				</span>
				<span style="color: rgb(128, 128, 128);">
						<font color="#ff0000">
								<font color="#808080">
										<br />
								</font>
						</font>
						<br />
						<br />
						<br />
						<br />
				</span>
				<span style="color: rgb(0, 128, 128);">
				</span>
		</div>
		<br />
<img src ="http://www.blogjava.net/Good-Game/aggbug/161100.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-11-16 19:20 <a href="http://www.blogjava.net/Good-Game/archive/2007/11/16/161100.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate 部分问题总结——1</title><link>http://www.blogjava.net/Good-Game/archive/2007/11/12/160000.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Mon, 12 Nov 2007 08:29:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/11/12/160000.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/160000.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/11/12/160000.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/160000.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/160000.html</trackback:ping><description><![CDATA[
		<b>
				<font color="#ffa500">缓存问题：<br /></font>
		</b>
		<font color="#ffa500">
				<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">    </span>
						<span style="color: rgb(0, 128, 0);">/**</span>
						<span style="color: rgb(0, 128, 0);"> 使用 Query.executeUpdate() 缓存中数据不同步 解决办法<br />     *  Table -&gt; T1oo(id,name)<br />     * Table -&gt; T2oo(id,avg,aid)<br />     * 外键 T1oo.id-&gt;T2oo.aid<br />     *  Session.createQuery("delete T1oo") -&gt;  Query.executeUpdate() <br />    </span>
						<span style="color: rgb(0, 128, 0);">*/</span>
						<span style="color: rgb(0, 0, 0);">
								<br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> testExecuteUpdate(){<br />        System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">\r\n\r\n********************ExecuteUpdate************************</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        T1oo t1oo </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">new</span>
						<span style="color: rgb(0, 0, 0);"> T1oo();<br />        t1oo.setName(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">liukaiyi</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        <br />        HibernateSessionFactory.closeSession();<br />        <br />        Session session </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> HibernateSessionFactory.currentSession();<br />    Transaction tr1 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">t1 成为 持久状态 一级缓存中 加载</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        session.saveOrUpdate(t1oo); <br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">直接一条语句删除T1oo，缓存无法同步<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">一级缓存中还有 t1</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        Query qu </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.createQuery(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">delete T1oo</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        </span>
						<span style="color: rgb(0, 0, 255);">try</span>
						<span style="color: rgb(0, 0, 0);"> {<br />            qu.executeUpdate();<br />        } </span>
						<span style="color: rgb(0, 0, 255);">catch</span>
						<span style="color: rgb(0, 0, 0);"> (Exception e) {    <br />            System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">//err: 有级联 单使用 delete T1oo 还要delete T2oo.aid = T1oo.id//</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />            List list </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.createQuery(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">from T1oo</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">).list();<br />            </span>
						<span style="color: rgb(0, 0, 255);">for</span>
						<span style="color: rgb(0, 0, 0);">(Iterator it</span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);">list.iterator();it.hasNext();){<br />                Query t2qu </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.createQuery(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">delete T2oo  where aid=:id</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />                t2qu.setInteger(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">id</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, ((T1oo)it.next()).getId().intValue());<br />                t2qu.executeUpdate();                <br />            }<br />            qu.executeUpdate();<br />        }<br />        <br />        tr1.commit();<br />        <br />    Transaction tr2 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">这直接通过一级缓存中加载t2，但DB中以没有此条数据</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        t1oo </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> (T1oo)session.load(T1oo.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">,t1oo.getId());<br />        t1oo.setName(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">google</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        </span>
						<span style="color: rgb(0, 0, 255);">try</span>
						<span style="color: rgb(0, 0, 0);"> {<br />            tr2.commit();    <br />        } </span>
						<span style="color: rgb(0, 0, 255);">catch</span>
						<span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">//err: update(t1oo)-&gt;DB 中数据库中没有 t1oo //</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">);<br />        }<br />        <br /><br />        System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> 一级缓存清空前 </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">+</span>
						<span style="color: rgb(0, 0, 0);"> session.get(T1oo.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">,t1oo.getId()) );<br />        session.evict(t1oo);<br />        System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> 一级缓存清空后 </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">+</span>
						<span style="color: rgb(0, 0, 0);"> session.get(T1oo.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">,t1oo.getId()) );<br />        <br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">不把t1oo id 为空，否则当在saveOrUpdate时候就会以为是游离态 update了</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        t1oo.setId(</span>
						<span style="color: rgb(0, 0, 255);">null</span>
						<span style="color: rgb(0, 0, 0);">);<br />        </span>
						<span style="color: rgb(0, 128, 0);">//</span>
						<span style="color: rgb(0, 128, 0);">id=null insert 调用</span>
						<span style="color: rgb(0, 128, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">        session.saveOrUpdate(t1oo);        <br />        tr2.commit();<br />        <br />    Transaction tr3 </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        session.delete(t1oo);<br />        tr3.commit();<br />        <br />        session.close();<br />        HibernateSessionFactory.closeSession();<br />        <br />    }<br /></span>
				</div>结果是：<br /><font color="#006400">********************ExecuteUpdate************************<br /><font color="#ff0000">log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).<br />log4j:WARN Please initialize the log4j system properly.</font><br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: delete from t1oo<br />//err: 有级联 单使用 delete T1oo 还要delete T2oo.aid = T1oo.id//<br />Hibernate: select t1oo0_.id as id, t1oo0_.name as name0_ from t1oo t1oo0_<br />Hibernate: delete from t2oo where aid=?<br />Hibernate: delete from t2oo where aid=?<br />Hibernate: delete from t2oo where aid=?<br />Hibernate: delete from t1oo<br />Hibernate: update t1oo set name=? where id=?<br />//err: update(t1oo)-&gt;DB 中数据库中没有 t1oo //<br /> 一级缓存清空前 hbn.bean.T1oo@287<br />Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?<br /> 一级缓存清空后 null<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: delete from t1oo where id=?</font><br /><br /><b>Get Load 区别 ：<br /></b> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> Get Load 区别 (在commit前要 session.flush())<br />     * Table -&gt; T1oo(id,name)<br />     * 1.如果未能发现符合条件的记录，get方法返回null，而load方法会抛出异常<br />     * 2.Load方法可返回实体的代理类实例，而get方法永远直接返回实体类。<br />     * 3.load方法可以充分利用内部缓存和二级缓存中的现有数据，而get方法则仅仅在内部缓存中进行数据查找，<br />     *             如没有发现对应数据，将越过二级缓存，直接调用SQL完成数据读取。 <br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testGetLoad() </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> Exception {<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\r\n\r\n********************Get&lt;&gt;Load************************</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> HibernateSessionFactory.currentSession();<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">  数据准备</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        T1oo t1oo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> T1oo();<br />        t1oo.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">liu</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    Transaction t1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        session.saveOrUpdate(t1oo);<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 为什么这会错？<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">session.evict(t1oo);<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">session.flush();</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        t1.commit();<br />        session.evict(t1oo);<br />    <br />    Transaction t2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">一级缓存是否有t1oo(load)-&gt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">session.contains(t1oo)); <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">这时候t1oo为 CGlib生成的代理类</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        t1oo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (T1oo)session.load(T1oo.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">,t1oo.getId());<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> 延迟加载出现：select.. t1oo0_.id=? 表的其他属性加载 </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        t1oo.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">load list</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">后在 update</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        t2.commit();<br />        session.evict(t1oo);<br />        <br />    Transaction t3 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">一级缓存是否有t1oo(get)-&gt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">session.contains(t1oo)); <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">这时候t1oo为 CGlib生成的代理类</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        t1oo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (T1oo)session.get(T1oo.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">,t1oo.getId());<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> 没有延迟加载出现</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        t1oo.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">get list</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">后在 update</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        t3.commit();    <br />        session.evict(t1oo);<br />        <br />    Transaction tr3 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        session.delete(t1oo);<br />        tr3.commit();<br />        <br />        session.close();<br />        HibernateSessionFactory.closeSession();<br />        <br />    }<br /></span></div>结果<br /><font color="#006400">********************Get&lt;&gt;Load************************<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />一级缓存是否有t1oo(load)-&gt;false<br /> 延迟加载出现：select.. t1oo0_.id=? 表的其他属性加载 <br />Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?<br />Hibernate: update t1oo set name=? where id=?<br />一级缓存是否有t1oo(get)-&gt;false<br />Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?<br /> 没有延迟加载出现<br />Hibernate: update t1oo set name=? where id=?<br />Hibernate: select t2ooset0_.aid as aid1_, t2ooset0_.id as id1_, t2ooset0_.id as id0_, t2ooset0_.avg as avg1_0_, t2ooset0_.aid as aid1_0_ from t2oo t2ooset0_ where t2ooset0_.aid=?<br />Hibernate: delete from t1oo where id=?<br /><br /><br /><b><font color="#ffa500">Set 集合的识别</font></b></font>：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> Set 集合的识别<br />     * Table -&gt; T1oo(id,name)<br />     * Table -&gt; T2oo(id,avg,aid)<br />     * 外键 T1oo.id-&gt;T2oo.aid<br />     * T1oo  &lt;set name="t2ooSet" inverse="false" cascade = "all"  &gt;<br />     * T2oo  &lt;many-to-one name="t1oo" column="aid" class="T1oo" /&gt;<br />     * cascade='insert' 是一定要的<br />     *     当 T1oo没有 inverse="true" 主动权的时候，要双项关联<br />     *     t1oo.getT2ooSet().add(t2oo1);<br />     *    t1oo.getT2ooSet().add(t2oo2);<br />     *    t2oo1.setT1oo(t1oo);<br />     *    t2oo2.setT1oo(t1oo);<br />     *  要不后sql为：<br />     *  Hibernate: insert into t2oo (avg, aid, id) values (?, ?, ?)<br />     *    Hibernate: insert into t2oo (avg, aid, id) values (?, ?, ?)<br />     *    +----+-----+------+<br />     *    | id | avg | aid  |<br />     *    +----+-----+------+<br />     *    |  1 |  24 | NULL |<br />     *    |  2 |  23 | NULL |<br />     *    +----+-----+------+<br />     *    当 T1oo有 inverse="false"主动权的时候<br />     *  t1oo.getT2ooSet().add(t2oo1);<br />     *    t1oo.getT2ooSet().add(t2oo2);<br />     *  Sql语句为：<br />     *  Hibernate: insert into t1oo (name, id) values (?, ?)<br />     *    Hibernate: insert into t2oo (avg, aid, id) values (?, ?, ?)<br />     *    Hibernate: insert into t2oo (avg, aid, id) values (?, ?, ?)<br />     *    Hibernate: update t2oo set aid=? where id=?<br />     *    Hibernate: update t2oo set aid=? where id=?<br />     *    |  3 |  24 |   12 |<br />     *    |  4 |  23 |   12 |<br />     *    +----+-----+------+<br />    </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testSet(){<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\r\n\r\n********************Set************************</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        T1oo t1oo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> T1oo();<br />        t1oo.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">list</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        <br />        T2oo t2oo1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> T2oo(); t2oo1.setAvg(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Integer(</span><span style="color: rgb(0, 0, 0);">23</span><span style="color: rgb(0, 0, 0);">));<br />        T2oo t2oo2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> T2oo(); t2oo2.setAvg(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Integer(</span><span style="color: rgb(0, 0, 0);">24</span><span style="color: rgb(0, 0, 0);">));<br />        <br />        <br />        Session session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> HibernateSessionFactory.currentSession();<br />        Transaction tr1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        session.save(t1oo);<br />        t1oo </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (T1oo) session.load(T1oo.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">,t1oo.getId());<br /><br />        t1oo.setT2ooSet(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashSet()) ;<br />        t1oo.getT2ooSet().add(t2oo1);<br />        t1oo.getT2ooSet().add(t2oo2);<br />         <br />        System.out.println( t1oo.getT2ooSet().size()</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);"> );        <br />        tr1.commit();<br />        <br />        System.out.println();<br />        T2oo t2oo3 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> T2oo(); t2oo3.setAvg(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Integer(</span><span style="color: rgb(0, 0, 0);">25</span><span style="color: rgb(0, 0, 0);">));<br />        T1oo t1oo2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> T1oo(); t1oo2.setName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mz</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        t2oo3.setT1oo(t1oo2);<br />        <br />        Transaction tr2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        session.save(t2oo3);<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            tr2.commit();    <br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">//err: 没有主动权 cascade = 'all' 不可以级联save t1oo   //</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />        session.close();<br />        HibernateSessionFactory.closeSession();<br />    }<br />    <br /></span></div><br />结果是：<br /><font color="#006400">********************Set************************<br />2<br />Hibernate: insert into t1oo (name, id) values (?, ?)<br />Hibernate: insert into t2oo (avg, aid, id) values (?, ?, ?)<br />Hibernate: insert into t2oo (avg, aid, id) values (?, ?, ?)<br />Hibernate: update t2oo set aid=? where id=?<br />Hibernate: update t2oo set aid=? where id=?<br /><br />//err: 没有主动权 cascade = 'all' 不可以级联save t1oo   //</font><br /><br /></font>
<img src ="http://www.blogjava.net/Good-Game/aggbug/160000.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-11-12 16:29 <a href="http://www.blogjava.net/Good-Game/archive/2007/11/12/160000.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>源码分析</title><link>http://www.blogjava.net/Good-Game/archive/2007/10/23/155210.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Tue, 23 Oct 2007 02:34:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/10/23/155210.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/155210.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/10/23/155210.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/155210.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/155210.html</trackback:ping><description><![CDATA[1.CollectionHelper 对List,Set,Map 包装出不可修改的<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> CollectionHelper {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> List EMPTY_LIST </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Collections.unmodifiableList( </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ArrayList(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) );<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> Collection EMPTY_COLLECTION </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Collections.unmodifiableCollection( </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ArrayList(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) );<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> Map EMPTY_MAP </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Collections.unmodifiableMap( </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashMap(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) );<br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> CollectionHelper() {}<br />}<br /></span></div>    1.1 在Collections.unmodifiableList(...)是静态内部类个构造方法 <br />       悟： 从上面看出是一个非常好的  <font color="#a52a2a"><b>适配器  </b></font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">   <font color="#0000ff"><font color="#006400">//<b>1.</b>Collections 中 new 出内部类</font><br />    public static &lt;T&gt; Collection&lt;T&gt; unmodifiableCollection(Collection&lt;? extends T&gt; c) {<br />        return new UnmodifiableCollection&lt;T&gt;(c);<br />    }</font></span><b><font color="#006400"><span style="color: rgb(0, 0, 0);"><br /><br /><br />    //2.</span></font></b><span style="color: rgb(0, 0, 0);"><font color="#0000ff"><font color="#006400">内部类</font></font></span><b><font color="#006400"><span style="color: rgb(0, 0, 0);">也继承  </span><span style="color: rgb(0, 0, 0);">Collection</span></font></b><br /><span style="color: rgb(0, 0, 0);"><font color="#0000ff">    static class</font> UnmodifiableCollection&lt;E&gt; <font color="#a52a2a">implements </font>Collection&lt;E&gt;, Serializable {<br /></span><span style="color: rgb(0, 0, 255);">    private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> serialVersionUID </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1820017752578914078L</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> Collection</span><span style="color: rgb(0, 0, 0);">&lt;?</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> E</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> c;<br />   <font color="#006400"> //<b>3.</b>很好适配 Collection 通过他把 add remove 等功能 封装</font><br />    UnmodifiableCollection(Collection</span><span style="color: rgb(0, 0, 0);">&lt;?</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> E</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> c) {<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (c</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)<br />                </span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> NullPointerException();<br />            </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> c;<br />        }<br />    ...............<br /></span></div><br />2.大量使用内部类枚矩 ?如：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Mappings.PropertyReference upr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Mappings.PropertyReference) iter.next();<br /></span><span style="color: rgb(0, 128, 0);">////////////////////////////////////////////////<br /></span><span style="color: rgb(0, 0, 0);">Mappings中<br />    </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> PropertyReference </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> Serializable {<br />        String referencedClass;<br />        String propertyName;<br />        </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> unique;<br />    }<br /><font color="#006400"><br />//感觉是可以更好的代码编写 </font><br /></span></div><br /><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/155210.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-10-23 10:34 <a href="http://www.blogjava.net/Good-Game/archive/2007/10/23/155210.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>没有玩转的 hibernate hql 检索</title><link>http://www.blogjava.net/Good-Game/archive/2007/08/24/138889.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Fri, 24 Aug 2007 08:36:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/08/24/138889.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/138889.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/08/24/138889.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/138889.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/138889.html</trackback:ping><description><![CDATA[    1。文件的映射。由于使用 MyEclipse（傻瓜版^_^） 所以 hibernate工具（extensions，Middlegen，tools等）一直没好好研究这就不献丑了。<br />    2。Criteria <br />       1）查询条件通过 Criteria.add 添加 Expression 用于描述条件 <br />                Expression.( and or like in le lt ..... )<br />                   参见： <a accesskey="n" href="http://chinesedocument.com/onlinedocument/hib_docs/reference/zh-cn/html/querycriteria.html">Criteria查询</a><br />        2）criteria.setFirstResult(100);<br />          criteria.setMaxResults(20); <font color="#008000">//检索范围<br />          <font color="#000000">criteria.addOrder(Order.asc("name")); <font color="#006400">//排序</font></font></font><br />          .add( Expression.sql("lower($alias.name) like lower(?)", "Fritz%", Hibernate.STRING)   <font color="#006400">//直接嵌入SQL<br />       <font color="#000000">3）</font></font>Cat cat = new Cat();<br />            ....<br /><br />         List results = session.createCriteria(Cat.class).add( Example.create(cat) ).list(); <font color="#006400">//根据对象查询</font><br />      <br />     3。HQL 完全面向对象的，具备继承、多态和关联等特性。<br />        1）参见： <a accesskey="n" href="http://chinesedocument.com/onlinedocument/hib_docs/reference/zh-cn/html/queryhql.html">HQL</a><br />       2）内连接，inner join<tt class="literal"><br />          左外连接，left outer join</tt><tt class="literal"><br />          右外连接，right outer join</tt>  <br /><font color="#006400">          http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html<br />            代表性的语句</font>     <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><span style="color: rgb(0, 0, 0);"><font color="#0000ff">Hql.hbm.xml</font><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">set </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="fkSet"</span><span style="color: rgb(255, 0, 0);"> inverse</span><span style="color: rgb(0, 0, 255);">="true"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />            </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">key </span><span style="color: rgb(255, 0, 0);">column</span><span style="color: rgb(0, 0, 255);">="id"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />            </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">one-to-many </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="Fk"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">set</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><font color="#0000ff">SQL <br /></font></span><span style="color: rgb(0, 0, 0);"><font color="#ffa500"><font color="#800080">alter table fk add constraint  FK_hf foreign key(id) references hql(id)</font></font></span><span style="color: rgb(0, 0, 0);"><font color="#0000ff"><br /><br /></font></span><span style="color: rgb(0, 0, 0);"><font color="#ffa500"><font color="#800080">mysql&gt; select * from hql;</font><br />
+----+----------+<br />
| id | name     |<br />
+----+----------+<br />
|  0 | liukaiyi |<br />
|  1 | heha     |<br />
+----+----------+<br />
2 rows in set (0.00 sec)<br /><br /><font color="#800080">
mysql&gt; select * from fk;</font><br />
+----+------+<br />
| id | name |<br />
+----+------+<br />
|  0 | yy   |<br />
|  1 | xx   |<br />
+----+------+<br />
2 rows in set (0.00 sec)</font></span><span style="color: rgb(0, 0, 0);"><font color="#ffa500"><br /></font></span></div><br /><font color="#006400">//简单的 结果： 2  liukaiyi:heha<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">        Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select h.name </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                                        </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> from Hql h , Fk f</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                                        </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> where h.id = f.id</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br />        List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  qu.list() ;<br />        tr.commit();<br />        System.out.println( list.size() );<br />        System.out.println( list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">list.get(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">) );</span></div><br /></font><br /><font color="#006400">//给出对象 结果 : heha</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">from Hql h where h.fkSet.name='xx'</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />((Hql)list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)).getName() ;<br /><br /></span></div><br /><font color="#006400">//给出Object 结果：heha xx</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><font color="#0000ff"><span style="color: rgb(0, 0, 0);">select h.name,f.name from Hql h join h.fkSet f where f.name='xx'</span></font><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  qu.list() ;<br />tr.commit();<br />System.out.println(list.size());    <br />Object[] objs </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Object[]) list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) ;<br />System.out.println( objs[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">objs[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] ); <br /></span></div><br /><font color="#006400">//给出 Map  heha xx<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">        <br />        Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select new map( h.name as hn,f.name as fn)from Hql h join h.fkSet f where f.name='xx'</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  qu.list() ;<br /><br />        tr.commit();<br />        System.out.println(list.size());    <br />        Map map </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ((Map)list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">));<br />        System.out.println( map.get(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">hn</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) );<br />    }</span></div><br />//报表语句  结果: 1</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">        Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select count(*) from Hql h join h.fkSet f </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                                        </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> where h.name='heha' and h.id=f.id </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                                        </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> group by h.name </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  qu.list() ;<br />        tr.commit();<br />        System.out.println( list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) );</span></div><font color="#006400"><br />//集合内 结果 liukaiyi:heha <br /><font color="#008000">//</font></font><font color="#008000">元素集(<tt class="literal">elements</tt>与<tt class="literal">indices</tt> 函数) <tt class="literal">可以使用 any, some, all, exists, in<br /><font color="#006400">//</font></tt></font><font color="#006400">EG:  from Player p where 3 &gt; all elements(p.scores)</font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);"><font color="#ffa500"> +----+----------+<br />| id | name     |<br />+----+----------+<br />|  0 | liukaiyi |<br />|  1 | heha     |<br />|  3 | oo       |<br />+----+----------+</font><br />         <br />        Query qu </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select h.name </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                                        </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> from Hql h</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                                        </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> where h.id in elements(h.fkSet.id) </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br />        List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  qu.list() ;<br />        tr.commit();<br />        System.out.println( list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">list.get(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">) );</span></div><br /><br /><font color="#006400">//使用 javaBean 绑定 <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Query q </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> s.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">from foo Foo as foo where foo.name=:name and foo.size=:size</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />q.setProperties(fooBean); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> fooBean包含方法getName()与getSize()</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">List foos </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> q.list();</span></div><br /></font><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/138889.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-08-24 16:36 <a href="http://www.blogjava.net/Good-Game/archive/2007/08/24/138889.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hbn 笔记</title><link>http://www.blogjava.net/Good-Game/archive/2007/08/09/128154.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Thu, 09 Aug 2007 06:30:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/08/09/128154.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/128154.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/08/09/128154.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/128154.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/128154.html</trackback:ping><description><![CDATA[
		<p>
				<br />1.Criteria 的 or  </p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
				<img id="Codehighlighter1_37_245_Open_Image" onclick="this.style.display='none'; Codehighlighter1_37_245_Open_Text.style.display='none'; Codehighlighter1_37_245_Closed_Image.style.display='inline'; Codehighlighter1_37_245_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
				<img id="Codehighlighter1_37_245_Closed_Image" style="display: none;" onclick="this.style.display='none'; Codehighlighter1_37_245_Closed_Text.style.display='none'; Codehighlighter1_37_245_Open_Image.style.display='inline'; Codehighlighter1_37_245_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />
				<span style="color: rgb(0, 0, 0);">    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> List getFind(String[] dpids )</span>
				<span id="Codehighlighter1_37_245_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_37_245_Open_Text">
						<span style="color: rgb(0, 0, 0);">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        Criteria cr </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);">  getCriteriaBegin(Deptid.</span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);">);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        cr.add( Restrictions.or( <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                    Restrictions.eq(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">dpid</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, dpids[</span>
						<span style="color: rgb(0, 0, 0);">0</span>
						<span style="color: rgb(0, 0, 0);">] ) , <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                    Restrictions.eq( </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">dpid</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> ,dpids[</span>
						<span style="color: rgb(0, 0, 0);">1</span>
						<span style="color: rgb(0, 0, 0);">] ) )<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />                );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> getCriteriaEnd(cr);<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span>
				</span>
		</div>
		<br />2.<small> 容器储存 <font color="#ff0000">（从表没主键）</font> 1)Set  的 .xml<br />             引用 <font color="#006400"> http://caterpillar.onlyfun.net/GossipCN/HibernateGossip/HibernateGossip.html</font><br /></small><pre><span style="font-weight: bold;">	&lt;set name="emails" table="email"&gt;<font color="#0000ff" size="2"></font><font color="#0000ff" size="2"><font face="Times New Roman">//</font></font></span><font color="#0000ff" face="Times New Roman" size="2"><span style="font-weight: bold;"><font>email表</font></span> 就 2列 id address</font><br /><span style="font-weight: bold;">		&lt;key column="id"/&gt;  <font color="#0000ff"><font size="2">//</font></font></span><span style="font-weight: bold;"><font color="#0000ff" size="2">email表的主键 也是主表的主键</font><br /></span> <span style="font-weight: bold;">		&lt;element type="java.lang.String" </span><span style="font-weight: bold;">column="address"/&gt; </span><br style="font-weight: bold;" /><span style="font-weight: bold;">&lt;/set&gt; <br /></span><small>     		.java </small><span style="font-weight: bold;">private Set emails;  <font size="2"></font></span><font color="#006400"><small><font size="2">//存和外键同</font><br /></small></font><small>2)List  的 .xml</small><br /><span style="font-weight: bold;">&lt;list name="items" table="item"&gt; </span><br style="font-weight: bold;" /><span style="font-weight: bold;">	  &lt;key column="id"/&gt; <font color="#0000ff" size="2">//为</font></span><font color="#0000ff" size="2"><span style="font-weight: bold;"><font>item</font></span><span style="font-weight: bold;">表的 </span><span style="font-weight: bold;">idINT(11) NOT NULL,</span></font><br style="font-weight: bold;" /><span style="font-weight: bold;">	  &lt;index column="position"/&gt;    <font color="#0000ff"></font><font color="#0000ff" size="2">//为表的 </font></span><font color="#0000ff" size="2"><span style="font-weight: bold;">position INT(11) NOT NULL,</span></font><br style="font-weight: bold;" /><span style="font-weight: bold;"> &lt;element type="java.lang.String" column="name"/&gt;<font color="#0000ff"></font><font color="#0000ff" size="2">//为表的</font></span><font color="#0000ff" size="2"><span style="font-weight: bold;">name VARCHAR(100) NOT NULL default ''</span></font><br style="font-weight: bold;" /><span style="font-weight: bold;">&lt;/list&gt; </span><small><br /></small>	。。。。。<br />3. 列为对象 <br />        &lt;set name="emails" table="email"&gt; <br />            &lt;key column="id"/&gt; <br /><span style="font-weight: bold;">&lt;composite-element class="onlyfun.caterpillar.MailAddress"&gt; </span><br style="font-weight: bold;" /><span style="font-weight: bold;">&lt;property name="address" column="address"/&gt;  <font color="#0000ff"></font><font color="#0000ff" size="2">//调用 getAddress()</font></span><br style="font-weight: bold;" /><span style="font-weight: bold;">&lt;/composite-element&gt; </span><br />        &lt;/set&gt; <br /><br /><br /></pre><img src ="http://www.blogjava.net/Good-Game/aggbug/128154.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-08-09 14:30 <a href="http://www.blogjava.net/Good-Game/archive/2007/08/09/128154.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate 包装</title><link>http://www.blogjava.net/Good-Game/archive/2007/07/04/128143.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Wed, 04 Jul 2007 08:49:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/07/04/128143.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/128143.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/07/04/128143.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/128143.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/128143.html</trackback:ping><description><![CDATA[
		<font color="#0000ff">
				<b>
						<font color="#ffa500">07-11-9<br />由于我对hibernate理解的深入，在次我来终结下这篇文章的不足的地方（好多人在拍砖哈）<br />对复杂的逻辑这样开和关太冒失了。<br />  在普通的逻辑方法中 session都叠加的用上2次以上 这就要对一级缓冲的管理。事物的使用也是一样。<br /></font>
				</b>
				<br />
				<br />下载:</font> <br /><a href="/Files/Good-Game/hibernateUtil.rar">http://www.blogjava.net/Files/Good-Game/hibernateUtil.rar</a><br /><br />简单的继承包 : <br />      继承 <font color="#ff0000">DBManage</font> 类  把 数据包装类 管理 ( 填充 , set 等 ) 后就直接放到里 insert , update , delete 方法中<br />         填充可看看这个,很有实用价值 ----&gt; ( 当 Form -&gt; Bean  或 Bean -&gt;Bean  ) 例子很简单但想法表明   <a href="/Good-Game/archive/2007/04/20/112131.html"></a><br />                                                                           <br />      继承 <font color="#ff0000">DBSelect</font> 类 为 select  这只是 把 Query 给出 简单用法 是<br />                                                      Query qu =   dao.getQueryBegin(  " HQL " );<br />                                                              qu.add .....<br />                                                      List li = dao.getQuertyEnd( qu );<br />     扩展:  Criteria  cr , SQLQuery , 等等 把她包装成 各个 dao 中 不是 很 偷懒     : )<br /><br /><br />Eg :     <font color="#a52a2a">extends</font><font color="#000080">DBBasic 类</font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Codehighlighter1_42_515_Open_Image" onclick="this.style.display='none'; Codehighlighter1_42_515_Open_Text.style.display='none'; Codehighlighter1_42_515_Closed_Image.style.display='inline'; Codehighlighter1_42_515_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_42_515_Closed_Image" style="display: none;" onclick="this.style.display='none'; Codehighlighter1_42_515_Closed_Text.style.display='none'; Codehighlighter1_42_515_Open_Image.style.display='inline'; Codehighlighter1_42_515_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> List select(String id,String time)</span><span id="Codehighlighter1_42_515_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_42_515_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ArrayList();<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        Query qu = </span><span style="color: rgb(0, 0, 255);">getQueryBegin</span><span style="color: rgb(0, 0, 0);">("HSQL");  <font color="#006400"> //继承带来的好处<br /></font><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        <font color="#006400">.add..........</font><span style="color: rgb(0, 0, 0);"><font color="#006400"></font></span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />             <br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />       List list = getQuertyEnd(qu);</span><span style="color: rgb(0, 0, 0);">   <font color="#008000"> //继承结束<br /></font><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span><span style="color: rgb(0, 0, 0);"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">  list;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span></span></div><br /><strong><font color="#ff0000">缺点:</font></strong> 最好不要static 在一时间点上 此类不能支持多线程 <br />               basicBegin  basicEnd  方法 ( 我找不到好的方法 要的话来个 连接池什么的 大家讨论西西吧!!<img src="http://www.blogjava.net/Emoticons/red_smile.gif" border="0" height="19" width="19" />  )<br />               sess 为全局的 session 要是 写成 baseicBegin 返回的话 感觉下面的还有很多要写 什么 close 啊...<br />                     sess 也是缺点的所在  当然大家 有什么想法 改下 或自己来个 小包装 我只是  <img src="http://www.blogjava.net/Emoticons/emidea.gif" border="0" height="19" width="19" />  抛砖引玉<br />    <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Map transmap </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashMap();<br /><br />    </span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> Session basicBegin(){<br />        Session sess </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> SessionManager.currentSession();<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            transmap.put( sess, sess.beginTransaction() );<br />        }</span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e){System.out.println(e);}<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> sess ;<br />    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> basicEnd(Session sess){<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />            ((Transaction)transmap.remove(sess)).commit();<br />        }</span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception e){System.out.println(e);}<br />        </span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">{<br />            sess </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> ;<br />            SessionManager.closeSession();<br />        }<br />    }</span></div><br /><strong><font style="background-color: rgb(245, 245, 220);" color="#008000">心得: 多包装多快乐</font></strong><img src ="http://www.blogjava.net/Good-Game/aggbug/128143.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-07-04 16:49 <a href="http://www.blogjava.net/Good-Game/archive/2007/07/04/128143.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>