﻿<?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>Fri, 09 May 2008 23:14:49 GMT</lastBuildDate><pubDate>Fri, 09 May 2008 23:14:49 GMT</pubDate><ttl>60</ttl><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="c