锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲人成人一区二区三区,亚洲AV无码乱码在线观看富二代,亚洲不卡AV影片在线播放http://www.tkk7.com/Good-Game/category/23018.htmlzh-cnThu, 29 May 2008 08:57:08 GMTThu, 29 May 2008 08:57:08 GMT60鏁版嵁搴撹〃鍦板潃鏁版嵁錛堜腑鍥藉湴鍖猴級(jí)http://www.tkk7.com/Good-Game/archive/2008/05/29/203747.htmlG_GG_GThu, 29 May 2008 02:51:00 GMThttp://www.tkk7.com/Good-Game/archive/2008/05/29/203747.htmlhttp://www.tkk7.com/Good-Game/comments/203747.htmlhttp://www.tkk7.com/Good-Game/archive/2008/05/29/203747.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/203747.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/203747.html聽CREATE聽TABLE聽`location`聽(聽聽`id`聽bigint(20)聽NOT聽NULL聽auto_increment,聽聽`name`聽varchar(50...  闃呰鍏ㄦ枃

G_G 2008-05-29 10:51 鍙戣〃璇勮
]]>
spring 涓?hibernate 鏁村悎(浜嬪姟)http://www.tkk7.com/Good-Game/archive/2008/05/09/199477.htmlG_GG_GFri, 09 May 2008 05:47:00 GMThttp://www.tkk7.com/Good-Game/archive/2008/05/09/199477.htmlhttp://www.tkk7.com/Good-Game/comments/199477.htmlhttp://www.tkk7.com/Good-Game/archive/2008/05/09/199477.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/199477.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/199477.htmlhttp://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch09.html

鍏堜粠閰嶇疆鏂囦歡寮濮嬶細(xì)
婧愮爜錛?a href="/Files/Good-Game/springAop.rar">springAop.rar

闇瑕乯ar
<?xml聽version="1.0"聽encoding="UTF-8"?>
<classpath>
聽聽聽聽
<classpathentry聽kind="src"聽path="java"/>
聽聽聽聽
<classpathentry聽kind="con"聽path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/aspectjrt.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/aspectjweaver.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/spring.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/spring-sources.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/commons-logging-1.0.4.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/cglib-nodep-2.1_3.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/hibernate3.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/log4j-1.2.11.jar"/>
聽聽聽聽
<classpathentry聽kind="con"聽path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/dom4j-1.6.1.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/commons-collections-2.1.1.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/mysql.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/jta.jar"/>
聽聽聽聽
<classpathentry聽kind="lib"聽path="lib/antlr-2.7.6.jar"/>
聽聽聽聽
<classpathentry聽kind="output"聽path="bin"/>
</classpath>


spring 閰嶇疆
<?xml聽version="1.0"聽encoding="UTF-8"?>
<beans聽xmlns="http://www.springframework.org/schema/beans"
聽聽聽聽聽聽聽xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
聽聽聽聽聽聽聽xmlns:aop
="http://www.springframework.org/schema/aop"
聽聽聽聽聽聽聽xmlns:tx
="http://www.springframework.org/schema/tx"
聽聽聽聽聽聽聽xsi:schemaLocation
="
聽聽聽聽聽聽聽http://www.springframework.org/schema/beans聽http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
聽聽聽聽聽聽聽http://www.springframework.org/schema/tx聽http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
聽聽聽聽聽聽聽http://www.springframework.org/schema/aop聽http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>
聽聽聽聽聽聽聽
聽聽
聽聽
<!--聽daoCalss聽:聽extends聽HibernateDaoSupport聽implements聽BeanDao聽-->
聽聽
<bean聽id="beanDao"聽class="dao.imp.BeanDaoImp">
聽聽聽聽聽聽
<property聽name="sessionFactory">
聽聽聽聽聽聽聽聽聽聽
<ref聽bean="sessionFactory"></ref>
聽聽聽聽聽聽
</property>
聽聽
</bean>
聽聽
聽聽聽
聽聽聽
<!--聽hibernate3聽sessionFactory聽-->
聽聽聽
<bean聽id="sessionFactory"聽聽聽聽聽
聽聽聽聽聽聽聽聽class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">聽聽
聽聽聽 聽聽聽
<!-- spring 涓?hibernate 鑱旂郴 -->
聽聽聽聽聽聽聽
<property聽name="configLocation"聽value="classpath:hibernate.cfg.xml"/>聽聽
聽聽聽
</bean>聽聽
聽聽聽
聽聽聽
<!--聽aop聽涓庝簨鍔¤仈緋宦燼opBean<->txAdvice聽聽-->
聽聽聽
<aop:config>
聽聽聽聽聽聽聽聽聽聽聽
<!--聽閫昏緫鎷︽埅聽-->
聽聽聽聽聽聽聽聽聽
<aop:pointcut聽id="aopBean"聽expression="execution(*聽*.*.*(..))"/>
聽聽聽聽聽聽聽聽聽聽聽
<aop:advisor聽advice-ref="txAdvice"聽pointcut-ref="aopBean"/>
聽聽聽聽聽聽
</aop:config>

聽聽聽聽
<!--聽浜嬪姟閫傞厤鍣?/span>-->
聽聽聽聽聽聽聽
<bean聽id="txManager"聽class="org.springframework.orm.hibernate3.HibernateTransactionManager">
聽聽聽聽聽聽
<property聽name="sessionFactory"聽ref="sessionFactory"/>
聽聽聽聽
</bean>

聽聽聽聽
<!--聽浜嬪姟鍘熷瓙聽鍏蜂綋鏂規(guī)硶榪涜浠涔堜簨鍔÷?/span>-->
聽聽聽聽聽聽
<tx:advice聽id="txAdvice"聽transaction-manager="txManager">
聽聽聽聽聽聽聽聽聽聽聽
<tx:attributes>
聽聽聽聽聽聽聽聽聽聽聽聽聽
<tx:method聽name="get*"聽read-only="true"/>
聽聽聽聽聽聽聽聽聽聽
<tx:method聽name="*"/>
聽聽聽聽聽聽聽聽
</tx:attributes>
聽聽聽聽聽聽
</tx:advice>

</beans>


hibernate 閰嶇疆
<?xml聽version="1.0"聽encoding="UTF-8"?>
<!DOCTYPE聽hibernate-configuration聽PUBLIC
聽聽聽聽聽聽聽聽"-//Hibernate/Hibernate聽Configuration聽DTD聽3.0//EN"
聽聽聽聽聽聽聽聽"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
<hibernate-configuration>
<session-factory聽name="asdf">
聽聽聽聽
<property聽name="hibernate.dialect">mysql</property>
聽聽聽聽
<property聽name="myeclipse.connection.profile">
聽聽聽聽聽聽聽聽com.mysql.jdbc.Driver
聽聽聽聽
</property>
聽聽聽聽
<property聽name="connection.url">
聽聽聽聽聽聽聽聽jdbc:mysql://localhost/aop
聽聽聽聽
</property>
聽聽聽聽
<property聽name="show_sql">true</property>
聽聽聽聽
聽聽聽聽
<property聽name="connection.username">root</property>
聽聽聽聽
<property聽name="connection.password"></property>
聽聽聽聽
<property聽name="connection.driver_class">
聽聽聽聽聽聽聽聽com.mysql.jdbc.Driver
聽聽聽聽
</property>
聽聽聽聽
<property聽name="dialect">
聽聽聽聽聽聽聽聽org.hibernate.dialect.MySQLDialect
聽聽聽聽
</property>
聽聽聽聽
聽聽聽聽
<mapping聽resource="bean/UnitBean.hbm.xml"/>
聽聽聽聽
</session-factory>
</hibernate-configuration>


dao 綾伙紙鎺ュ彛錛?br />
package聽dao.imp;

import聽java.util.List;

import聽org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import聽bean.UnitBean;

import聽dao.BeanDao;

publicclass聽BeanDaoImp聽extends聽HibernateDaoSupport聽implements聽BeanDao{
聽聽聽聽
publicvoid聽addBean(UnitBean聽unitBean)聽{
聽聽聽聽聽聽聽聽
this.getHibernateTemplate().save(unitBean);
聽聽聽聽}

聽聽聽聽
public聽List<UnitBean>聽getBeanByAll()聽{
聽聽聽聽聽聽聽聽
returnthis.getHibernateTemplate().find("聽from聽"+UnitBean.class.getName());
聽聽聽聽}

聽聽聽聽
publicvoid聽removeBean(long聽beanId)聽{
聽聽聽聽聽聽聽聽
this.getHibernateTemplate().delete(
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽getHibernateTemplate().get(UnitBean.
class,聽beanId)
聽聽聽聽聽聽聽聽聽聽聽聽);
聽聽聽聽}
聽聽聽聽
}

Main 綾?br />
package聽unit;

import聽org.springframework.context.ApplicationContext;
import聽org.springframework.context.support.ClassPathXmlApplicationContext;

import聽dao.BeanDao;
import聽bean.UnitBean;

publicclass聽Main聽{
聽聽聽聽
publicstaticvoid聽main(String[]聽args)聽{
聽聽聽聽聽聽聽聽聽聽聽ApplicationContext聽ctx聽
=new聽ClassPathXmlApplicationContext("beans.xml");
聽聽聽聽聽聽聽聽聽聽聽BeanDao聽dao聽
=聽(BeanDao)聽ctx.getBean("beanDao");
聽聽聽聽聽聽聽聽聽聽聽UnitBean聽bean聽
=new聽UnitBean();
聽聽聽聽聽聽聽聽聽聽聽bean.setName(
"xx");
聽聽聽聽聽聽聽聽聽聽聽bean.setPass(
"11");
聽聽聽聽聽聽聽聽聽聽聽dao.addBean(bean);
聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽
for(UnitBean聽unitBean聽:聽dao.getBeanByAll()聽){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(聽unitBean.getId()聽);
聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽dao.removeBean(bean.getId());
聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽}
}
緇撴灉錛?br />Hibernate: insert into bean (name, pass) values (?, ?)
Hibernate: select unitbean0_.id as id0_, unitbean0_.name as name0_, unitbean0_.pass as pass0_ from bean unitbean0_
1
Hibernate: select unitbean0_.id as id0_0_, unitbean0_.name as name0_0_, unitbean0_.pass as pass0_0_ from bean unitbean0_ where unitbean0_.id=?
Hibernate: delete from bean where id=?








G_G 2008-05-09 13:47 鍙戣〃璇勮
]]>
Hibernate 鏁版嵁緇撴瀯錛?鏍?wèi)銆佸瀵瑰 緇撴瀯琛ㄧず 錛?/title><link>http://www.tkk7.com/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.tkk7.com/Good-Game/archive/2008/04/26/196293.html</guid><wfw:comment>http://www.tkk7.com/Good-Game/comments/196293.html</wfw:comment><comments>http://www.tkk7.com/Good-Game/archive/2008/04/26/196293.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/Good-Game/comments/commentRss/196293.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/Good-Game/services/trackbacks/196293.html</trackback:ping><description><![CDATA[     鎽樿: level 綾伙細(xì)Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->package聽hbm;import聽java.util.Set;/**聽*聽@hibernate.class聽table聽=聽"level"聽*聽where聽=聽"聽visible聽=聽...  <a href='http://www.tkk7.com/Good-Game/archive/2008/04/26/196293.html'>闃呰鍏ㄦ枃</a><img src ="http://www.tkk7.com/Good-Game/aggbug/196293.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/Good-Game/" target="_blank">G_G</a> 2008-04-26 18:30 <a href="http://www.tkk7.com/Good-Game/archive/2008/04/26/196293.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Hibernate 綰ц仈娣誨姞鍒犻櫎http://www.tkk7.com/Good-Game/archive/2008/04/02/190283.htmlG_GG_GWed, 02 Apr 2008 02:07:00 GMThttp://www.tkk7.com/Good-Game/archive/2008/04/02/190283.htmlhttp://www.tkk7.com/Good-Game/comments/190283.htmlhttp://www.tkk7.com/Good-Game/archive/2008/04/02/190283.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/190283.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/190283.html聽鏁堟灉-銆?br />
聽聽聽聽/**
聽聽聽聽聽*聽娣誨姞聽闂聽鍜屄犻夐」
聽聽聽聽聽*聽
@throws聽Exception
聽聽聽聽聽
*/@Test
聽聽聽聽
publicvoid聽testQu()聽throws聽Exception聽{
聽聽聽聽聽聽聽聽Session聽session聽
=聽HibernateUtil.currentSession();
聽聽聽聽聽聽聽聽Transaction聽tr聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//綰ц仈娣誨姞
聽聽聽聽聽聽聽聽Set聽options聽=new聽HashSet();
聽聽聽聽聽聽聽聽Options聽op1聽
=new聽Options();
聽聽聽聽聽聽聽聽op1.setName(
"op1");
聽聽聽聽聽聽聽聽options.add(op1);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Options聽op2聽
=new聽Options();
聽聽聽聽聽聽聽聽op2.setName(
"op2");
聽聽聽聽聽聽聽聽options.add(op2);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Options聽op3聽
=new聽Options();
聽聽聽聽聽聽聽聽op3.setName(
"op3");
聽聽聽聽聽聽聽聽options.add(op3);
聽聽聽聽聽聽聽聽Problems聽problems聽
=new聽Problems();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽problems.setName(
"problem_1");
聽聽聽聽聽聽聽聽problems.setOptions(options);
聽聽聽聽聽聽聽聽problems.setTdesc(
"tdesc");
聽聽聽聽聽聽聽聽problems.setType(
1);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Long聽ll聽
=聽(Long)session.save(problems);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽System.out.println(ll);
聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽
mysql> select * from options ;
+----+------------+--------+------+---------+
| id | problemsid | answer | name | visible |
+----+------------+--------+------+---------+
|聽 1 |聽聽聽聽聽聽聽聽聽 1 |聽聽 NULL | op2聽 |聽聽聽聽聽聽 0 |
|聽 2 |聽聽聽聽聽聽聽聽聽 1 |聽聽 NULL | op3聽 |聽聽聽聽聽聽 0 |
|聽 3 |聽聽聽聽聽聽聽聽聽 1 |聽聽 NULL | op1聽 |聽聽聽聽聽聽 0 |
+----+------------+--------+------+---------+
3 rows in set (0.00 sec)

mysql> select * from problems ;
+----+-----------+------+-------+------------+---------+
| id | name聽聽聽聽聽 | type | tdesc | questionid | visible |
+----+-----------+------+-------+------------+---------+
|聽 1 | problem_1 |聽聽聽 1 | tdesc |聽聽聽聽聽聽 NULL |聽聽聽聽聽聽 0 |
+----+-----------+------+-------+------------+---------+
1 row in set (0.00 sec)


聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//綰ц仈鍒犻櫎
聽聽聽聽聽聽聽聽tr.begin();
聽聽聽聽聽聽聽聽聽聽聽聽session.delete(聽session.get(Problems.
class,ll)聽);
聽聽聽聽聽聽聽聽tr.commit();

mysql> select * from problems ;
Empty set (0.00 sec)

mysql> select * from options ;
Empty set (0.00 sec)聽


聽聽聽聽聽聽聽聽HibernateUtil.closeSession();
聽聽聽聽}

Options綾?br />
.......
聽聽聽 /**
聽聽聽聽聽*聽@hibernate.many-to-one聽
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽cascade聽=聽"save-update"
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽column聽=聽"Problemsid"
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽class聽=聽"com.zhongqi.domain.Problems"
聽聽聽聽聽*聽
@return
聽聽聽聽聽
*/
聽聽聽聽
public聽Problems聽getProblems()聽{
聽聽聽聽聽聽聽聽
return聽problems;
聽聽聽聽}
............

Problems 綾?br />
聽聽聽聽/**
聽聽聽聽聽*聽@hibernate.set
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽cascade="all-delete-orphan"
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽inverse聽=聽"false"
聽聽聽聽聽*聽聽聽聽聽聽聽聽 lazy聽=聽"true"
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽@hibernate.collection-key聽聽column聽=聽"problemsid"
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽@hibernate.collection-one-to-many聽class聽=聽"com.zhongqi.domain.Options"
聽聽聽聽聽*聽
@return
聽聽聽聽聽
*/
聽聽聽聽
public聽Set聽getOptions()聽{
聽聽聽聽聽聽聽聽
return聽options;
聽聽聽聽}





G_G 2008-04-02 10:07 鍙戣〃璇勮
]]>
mysql Blob hbn 鎿嶄綔http://www.tkk7.com/Good-Game/archive/2008/01/28/178122.htmlG_GG_GMon, 28 Jan 2008 01:53:00 GMThttp://www.tkk7.com/Good-Game/archive/2008/01/28/178122.htmlhttp://www.tkk7.com/Good-Game/comments/178122.htmlhttp://www.tkk7.com/Good-Game/archive/2008/01/28/178122.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/178122.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/178122.html
mysql>desc聽lotteryinformation聽;
+-------------+--------------+------+-----+---------+-------+
|聽Field聽聽聽聽聽聽聽|聽Type聽聽聽聽聽聽聽聽聽|Null|Key|Default|聽Extra聽|
+-------------+--------------+------+-----+---------+-------+
|聽id聽聽聽聽聽聽聽聽聽聽|int(11)聽聽聽聽聽聽|聽NO聽聽聽|聽PRI聽|NULL聽聽聽聽|聽聽聽聽聽聽聽|
|聽type聽聽聽聽聽聽聽聽|varchar(255)聽|聽NO聽聽聽|聽聽聽聽聽|NULL聽聽聽聽|聽聽聽聽聽聽聽|
|聽title聽聽聽聽聽聽聽|varchar(255)聽|聽YES聽聽|聽聽聽聽聽|NULL聽聽聽聽|聽聽聽聽聽聽聽|
|聽inputTime聽聽聽|datetime聽聽聽聽聽|聽YES聽聽|聽聽聽聽聽|NULL聽聽聽聽|聽聽聽聽聽聽聽|
|聽content聽聽聽聽聽|聽blob聽聽聽聽聽聽聽聽聽|聽YES聽聽|聽聽聽聽聽|NULL聽聽聽聽|聽聽聽聽聽聽聽|
|聽recommended聽|bit(1)聽聽聽聽聽聽聽|聽YES聽聽|聽聽聽聽聽|NULL聽聽聽聽|聽聽聽聽聽聽聽|
|聽categories聽聽|varchar(255)聽|聽YES聽聽|聽聽聽聽聽|NULL聽聽聽聽|聽聽聽聽聽聽聽|
+-------------+--------------+------+-----+---------+-------+

2.浠g爜
聽聽聽聽聽聽聽 //鎻掑叆
聽聽聽 聽聽聽 Session聽sess聽
=聽聽HibernateSessionFactory.getSession();
聽聽聽聽聽聽聽聽Transaction聽tr聽
=聽sess.beginTransaction();
聽聽聽聽聽聽聽聽LotteryNew聽nn聽
=new聽LotteryNew();
聽聽聽聽聽聽聽聽nn.setInputTime(
new聽Date());
聽聽聽聽聽聽聽聽nn.setCategories(
"t");
聽聽聽聽聽聽聽聽nn.setTitle(
"new");
聽聽聽聽聽聽聽聽nn.setRecommended(
true);
聽聽聽聽聽聽聽聽Blob聽bo聽
=聽Hibernate.createBlob("ggggg".getBytes());
聽聽聽聽聽聽聽聽nn.setContent(bo);
聽聽聽聽聽聽聽聽sess.save(nn);
聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽聽聽聽
聽聽聽 聽聽聽 //淇敼
聽聽聽聽聽聽聽聽sess聽
=聽聽HibernateSessionFactory.getSession();
聽聽聽聽聽聽聽聽tr聽
=聽sess.beginTransaction();
聽聽聽聽聽聽聽聽LotteryNew聽lo聽
=聽(LotteryNew)聽sess.get(LotteryNew.class,聽nn.getId());
聽聽聽聽聽聽聽聽Blob聽bog聽
=聽Hibernate.createBlob("xxxxx".getBytes());
聽聽聽聽聽聽聽聽lo.setContent(bog);
聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 //鏌ユ壘
聽聽聽聽聽聽聽聽sess聽
=聽聽HibernateSessionFactory.getSession();
聽聽聽聽聽聽聽聽LotteryNew聽lo2聽
=聽(LotteryNew)聽sess.get(LotteryNew.class,聽lo.getId());
聽聽聽聽聽聽聽聽I(yíng)nputStream聽in聽
=聽lo2.getContent().getBinaryStream()聽;
聽聽聽聽聽聽聽聽
byte[]聽bbr聽=newbyte[in.available()];
聽聽聽聽聽聽聽聽in.read(bbr);
聽聽聽聽聽聽聽聽System.out.println(
new聽String(bbr));
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();



G_G 2008-01-28 09:53 鍙戣〃璇勮
]]>
hbn 緇ф壙鍏崇郴鏄犲皠_1http://www.tkk7.com/Good-Game/archive/2007/12/26/170536.htmlG_GG_GWed, 26 Dec 2007 03:41:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/12/26/170536.htmlhttp://www.tkk7.com/Good-Game/comments/170536.htmlhttp://www.tkk7.com/Good-Game/archive/2007/12/26/170536.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/170536.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/170536.html闃呰鍏ㄦ枃

G_G 2007-12-26 11:41 鍙戣〃璇勮
]]>
hibernate + Myeclipes 鏁忔嵎(灝忎緥)http://www.tkk7.com/Good-Game/archive/2007/12/11/167003.htmlG_GG_GTue, 11 Dec 2007 09:51:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/12/11/167003.htmlhttp://www.tkk7.com/Good-Game/comments/167003.htmlhttp://www.tkk7.com/Good-Game/archive/2007/12/11/167003.html#Feedback1http://www.tkk7.com/Good-Game/comments/commentRss/167003.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/167003.html
寮鍙戣繃紼嬫弿榪幫細(xì)

1.浣跨敤 MyEclipes -> uml聽 鍒涘緩綾誨浘
2.鐢?Generate java Code 鏍規(guī)嵁綾誨浘鐢熸垚 java鏂囦歡
3.浣跨敤 Xdoclet 娣誨姞 Hbn 鏍囩
4.閰嶇疆myEclipes -> XDoclet 鑷姩鐢熸垚 mapping.hbn.xml
5.浣跨敤myEclipes 鎶婇」鐩漿鍖栨垚 hibernate聽 欏圭洰
6.浣跨敤 org.hibernate.tool.hbm2ddl.SchemaExport 寤鴻〃

寮鍙戣繃紼嬪ソ澶勶細(xì)

1錛夊畬鍏ㄦ槸闈㈠悜瀵硅薄錛屼笉闇瑕佸啓xml閰嶇疆鏂囦歡(XDoclet);
2)欏圭洰鍚庢湡淇敼瀹規(guī)槗闈㈠uml
3錛夌敤myEclipes 榪欎簺閮戒笉鐢ㄥ幓鎵撅紝鐩存帴鎷挎潵鐢?uml,XDoclet,hibernate ..)

涓嬮潰灝辨潵涓?灝忎緥鎶?
1.MyEclipes 浣跨敤 uml 鍙傝?>MyEclipse 5.5 UML 鍏ラ棬瑙嗛 (浣滆咃細(xì)BeanSoft)


2.鐢眜ml鐢熸垚綾繪枃浠?


3.鍏堜嬌鐢?eclipes鐨?蹇敭鏂規(guī)硶鍐?get/set 鏂規(guī)硶錛?綾繪枃浠舵枃浠舵坊鍔?hbn XDoclet鐨勬敞瑙?br />
package聽bean;

/**
聽*聽@hibernate.class聽table="t1oo"
*/
publicclass聽T1oo聽{

聽聽
publicint聽id;
聽聽
public聽String聽name;
聽聽
publicint聽avg;
聽聽
聽聽
/**
聽聽聽*聽@hibernate.property聽
聽聽聽*聽column="avg"
聽聽聽*聽length="4"
聽聽聽*聽not-null="true"
聽聽聽
*/
publicint聽getAvg()聽{
聽聽聽聽
return聽avg;
}
publicvoid聽setAvg(int聽avg)聽{
聽聽聽聽
this.avg聽=聽avg;
}
/**
聽*聽@hibernate.id聽
聽*聽column="id"
聽*聽generator-class="hilo"
*/
publicint聽getId()聽{
聽聽聽聽
return聽id;
}
publicvoid聽setId(int聽id)聽{
聽聽聽聽
this.id聽=聽id;
}
/**
聽*聽@hibernate.property聽
聽*聽column="name"
聽*聽not-null="true"聽
聽*聽
@return
*/
public聽String聽getName()聽{
聽聽聽聽
return聽name;
}
publicvoid聽setName(String聽name)聽{
聽聽聽聽
this.name聽=聽name;
}
聽聽

}

4.鐢╩yEclipes 鐢熸垚 XDoclet
鍦ㄩ」鐩偣鍙抽敭-> properties -> MyEclipse-XDoclet ->
鍦–onfiguration 絀虹櫧鍒濈偣鍙抽敭 閫?add standard -> ... hbn 鍚庨潰涓嶅お濂芥弿榪?鍙互鏌ヤ笅寰堢畝鍗曠殑 銆傞厤緗ソ浜?jiǎn)杩愯鍚帪鍙互鐪嬭?澶氫簡(jiǎn) 涓?T1oo.hbm.xml 鏂囦歡;

5.myEclipes + hbn 灝變笉澶氳浜?br />6. hbn2java:
聽聽聽聽publicvoid聽testCreateTable()throws聽Exception{
聽聽聽聽聽聽聽聽聽HibernateSessionFactory.currentSession();
聽聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽Field[]聽ff聽
=聽HibernateSessionFactory.class.getDeclaredFields();
聽聽聽聽聽聽聽聽聽Field聽fie聽
=null聽;
聽聽聽聽聽聽聽聽聽
for(int聽i=0;i<ff.length;i++){
聽聽聽聽聽聽聽聽聽聽聽聽聽
if(聽ff[i].getType().equals(聽Configuration.class聽)聽){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽fie聽
=聽ff[i];
聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽fie.setAccessible(
true);
聽聽聽聽聽聽聽聽聽Configuration聽cfg聽
=聽(Configuration)fie.get(HibernateSessionFactory.class);
聽聽聽聽聽聽聽聽聽cfg.addInputStream(聽
this.getClass().getResourceAsStream("/bean/T1oo.hbm.xml")聽);
聽聽聽聽聽聽聽聽 //寤鴻〃
聽聽聽聽聽聽聽聽聽聽SchemaExport聽dbExport聽
=new聽SchemaExport(cfg);
聽聽聽聽聽聽聽聽聽聽dbExport.setOutputFile(
"c:\\db\\test.txt");
聽聽聽聽聽聽聽聽聽聽dbExport.create(
true,聽true);聽
聽聽聽聽}


sql錛?br />drop table if exists t1oo
drop table if exists hibernate_unique_key
create table t1oo (
聽聽聽 id integer not null,
聽聽聽 avg integer not null,
聽聽聽 name varchar(255) not null,
聽聽聽 primary key (id)
)
create table hibernate_unique_key (
聽聽聽聽 next_hi integer
)
insert into hibernate_unique_key values ( 0 )


鏁堟灉錛?br />mysql> show tables;
+----------------------+
| Tables_in_hbn聽聽聽聽聽聽聽 |
+----------------------+
| hibernate_unique_key |
| t1oo聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 |
+----------------------+
2 rows in set (0.00 sec)





G_G 2007-12-11 17:51 鍙戣〃璇勮
]]>
hibernate 鏌ヨ璇彞鎬葷粨http://www.tkk7.com/Good-Game/archive/2007/12/10/166625.htmlG_GG_GMon, 10 Dec 2007 04:52:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/12/10/166625.htmlhttp://www.tkk7.com/Good-Game/comments/166625.htmlhttp://www.tkk7.com/Good-Game/archive/2007/12/10/166625.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/166625.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/166625.html
1.鍗曟潯select寤惰繜鍔犺澆
聽聽聽聽聽聽聽聽I(yíng)terator聽it聽=聽session.createQuery("from聽T1oo聽").iterate();
聽聽聽聽聽聽聽聽
while(it.hasNext()){
聽聽聽聽聽聽聽聽聽聽聽聽T1oo聽t1聽
=聽(T1oo)it.next();
聽聽聽聽聽聽聽聽聽聽聽聽t1.getName();
聽聽聽聽聽聽聽聽}
/*榪愯璇彞聽n+1
榪欏氨鍙姞杞?id
Hibernate:聽select聽t1oo0_.id聽as聽col_0_0_聽from聽t1oo t1oo0_
姝ゆ槸鍦?t1.getName(); 寤惰繜鍔犺澆鐨?
Hibernate:聽select聽t1oo0_.id聽as聽id0_,聽t1oo0_.name聽as聽name0_0_聽from聽t1oo聽t1oo0_聽where聽t1oo0_.id=?
Hibernate:聽select聽t1oo0_.id聽as聽id0_,聽t1oo0_.name聽as聽name0_0_聽from聽t1oo聽t1oo0_聽where聽t1oo0_.id=?
*/

2.綰ц繛鏌ヨ錛?br />1)set鎺掑簭聽 <set ... order-by="avg desc" ...> 浠庡ぇ鍒板皬
2錛塨atch-size="10" 鐢ㄦ硶鏄?
聽聽聽 select * from t2oo聽 where aid in (?,?,?....)
3)

濡傛灉鎯沖拷鐣ュ歡榪燂紝騫舵湁涓瀹氶昏緫鍏ㄩ儴鍔犺澆錛岃繖鏈変袱涓В鍐沖姙娉曪細(xì)
1).鍐呰繛
mysql>select聽聽聽*
聽聽聽聽
->from聽t1oo聽t1oo0_聽innerjoin聽t2oo聽t2ooset1_聽on聽t1oo0_.id=t2ooset1_.aid;
+----+-----------+----+-----+------+---------+
| id | name聽聽聽聽聽 | id | avg | aid聽 | version |
+----+-----------+----+-----+------+---------+
|聽 1 | liukaiyi聽 |聽 1 |聽 23 |聽聽聽 1 |聽聽聽聽聽聽 1 |
|聽 1 | liukaiyi聽 |聽 2 |聽 24 |聽聽聽 1 |聽聽聽聽聽聽 1 |
|聽 1 | liukaiyi聽 |聽 3 |聽 25 |聽聽聽 1 |聽聽聽聽聽聽 1 |
|聽 2 | liukaiyi2 |聽 4 |聽 26 |聽聽聽 2 |聽聽聽聽聽聽 0 |
+----+-----------+----+-----+------+---------+


聽 聽 聽 聽 Iterator聽it聽=new聽HashSet(session.createQuery("from聽T1oo聽t1聽inner聽join聽fetch聽t1.t2ooSet聽t2where t2.id<=3").list()).iterator();
聽聽聽聽聽聽聽聽
while(it.hasNext()){
聽聽聽聽聽聽聽聽聽聽聽聽T1oo聽t1聽
=聽(T1oo)it.next();
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(t1.getName());
聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽
for(Iterator聽itr=t1.getT2ooSet().iterator();itr.hasNext();聽){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽T2oo聽t2聽
=聽(T2oo)itr.next();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(
"聽聽"+聽t2.getAvg()聽);
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}
緇撴灉鏄細(xì)
Hibernate:聽select聽t1oo0_.id聽as聽id0_,聽t2ooset1_.id聽as聽id1_,聽t1oo0_.name聽as聽name0_0_,聽t2ooset1_.version聽as聽version1_1_,聽t2ooset1_.avgas聽avg1_1_,聽t2ooset1_.aid聽as聽aid1_1_,聽t2ooset1_.aid聽as聽aid0__,聽t2ooset1_.id聽as聽id0__聽from聽t1oo聽t1oo0_聽innerjoin聽t2oo聽t2ooset1_聽on聽t1oo0_.id=t2ooset1_.aid聽where聽t2ooset1_.id<=3
liukaiyi
聽聽
24
聽聽
23
聽聽
25



G_G 2007-12-10 12:52 鍙戣〃璇勮
]]>
hibernate cache2http://www.tkk7.com/Good-Game/archive/2007/12/07/166061.htmlG_GG_GFri, 07 Dec 2007 07:36:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/12/07/166061.htmlhttp://www.tkk7.com/Good-Game/comments/166061.htmlhttp://www.tkk7.com/Good-Game/archive/2007/12/07/166061.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/166061.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/166061.html
聽聽聽聽<property聽name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
聽聽聽聽
<property聽name="hibernate.cache.use_query_cache">true</property>

聽聽聽聽
<mapping聽resource="hbn/bean/T1oo.hbm.xml"/>
聽聽聽聽
<mapping聽resource="hbn/bean/T2oo.hbm.xml"/>

聽聽聽聽
<class-cache聽class="hbn.bean.T1oo"聽usage="read-only"/>
聽聽聽聽
<collection-cache聽collection="hbn.bean.T1oo.t2ooSet"聽usage="read-only"/>
聽聽聽聽
<class-cache聽class="hbn.bean.T2oo"聽usage="read-only"/>

鍦╯rc鏍圭洰褰曚笅 ehcache.xml
<?xml聽version="1.0"聽encoding="UTF-8"?>
<ehcache>
聽聽聽聽
<diskStore聽path="java.io.tmpdir"/>
聽聽聽聽
<defaultCache
聽聽聽聽聽聽聽聽
maxElementsInMemory="10000" //鏈澶х紦瀛樻暟鐩?/font>
聽聽聽聽聽聽聽聽eternal
="false"錛?-- 緙撳瓨鏄惁鎸佷箙 --錛?/font>
聽聽聽聽聽聽聽聽timeToIdleSeconds
="120" 錛?-- 褰撶紦瀛橀棽緗畁縐掑悗閿姣?--錛?/font>
聽聽聽聽聽聽聽聽timeToLiveSeconds
="120"錛?-- 褰撶紦瀛樺瓨?gòu)z籲縐掑悗閿姣?-錛?/font>
聽聽聽聽聽聽聽聽overflowToDisk
="true"錛?-- 鏄惁淇濆瓨鍒扮鐩橈紝褰撶郴緇熷綋鏈烘椂--錛?/font>
聽聽聽聽聽聽聽聽diskPersistent
="false"
聽聽聽聽聽聽聽聽diskExpiryThreadIntervalSeconds
="120"/>

聽聽聽聽<cache
聽聽聽聽聽
name="hbn.bean.T1oo"
聽聽聽聽聽maxElementsInMemory
="450"
聽聽聽聽聽eternal
="false"
聽聽聽聽聽timeToLiveSeconds
="600"
聽聽聽聽聽overflowToDisk
="true"/>

</ehcache>

嫻嬭瘯錛?br />
聽聽聽聽publicvoid聽testCa()throws聽Exception{
聽聽聽聽聽聽聽聽System.out.println( getT1ooAll()聽);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Thread.sleep(
2*1000);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽System.out.println(
getT1ooAll()聽);
聽聽聽聽}

鎺у埗鍙拌緭鍑?br />
Hibernate:聽select聽t1oo0_.id聽as聽id,聽t1oo0_.name聽as聽name0_聽from聽t1oo聽t1oo0_聽limit聽?

Hibernate:聽
select聽t2ooset0_.aid聽as聽aid1_,聽t2ooset0_.id聽as聽id1_,聽t2ooset0_.id聽as聽id0_,聽t2ooset0_.version聽as聽version1_0_,聽t2ooset0_.avgas聽avg1_0_,聽t2ooset0_.aid聽as聽aid1_0_聽from聽t2oo聽t2ooset0_聽where聽t2ooset0_.aid=?
Hibernate:聽
select聽t2ooset0_.aid聽as聽aid1_,聽t2ooset0_.id聽as聽id1_,聽t2ooset0_.id聽as聽id0_,聽t2ooset0_.version聽as聽version1_0_,聽t2ooset0_.avgas聽avg1_0_,聽t2ooset0_.aid聽as聽aid1_0_聽from聽t2oo聽t2ooset0_聽where聽t2ooset0_.aid=?
24聽:聽23聽:聽25聽:聽2

//鍦ㄨ繖緙撳瓨鎴愬姛 娌″悜鏁版嵁搴撴彁浜?sql璇彞
24聽:聽23聽:聽25聽:聽2





G_G 2007-12-07 15:36 鍙戣〃璇勮
]]>
hibernate 澶氭湇鍔″櫒鏁版嵁鍚屾闂錛堟敮鎸佸歡榪熷悓姝ワ級(jí)http://www.tkk7.com/Good-Game/archive/2007/12/03/164813.htmlG_GG_GMon, 03 Dec 2007 03:29:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/12/03/164813.htmlhttp://www.tkk7.com/Good-Game/comments/164813.htmlhttp://www.tkk7.com/Good-Game/archive/2007/12/03/164813.html#Feedback10http://www.tkk7.com/Good-Game/comments/commentRss/164813.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/164813.html
鎵鐢ㄥ埌鐭ヨ瘑鐐癸細(xì)
涓鑸琷dk
|-縐佹湁灞炴у弽灝?br />|-搴忓垪鍖?br />|-姝e垯琛ㄨ揪浣跨敤
|-澶氱嚎紼嬩嬌鐢?br />|-dom4j鐨剎ml璇誨彇
|+hibernate
聽聽 |-鎷︽埅鍣?br />聽聽 |-涓涓猄ession宸ュ巶鍚屾椂榪炴帴涓嶅悓鏁版嵁搴擄紙鏈枃鍏抽敭錛?br />聽聽 |-oracle Blob 瀛樺彇

絳?.......

闇姹傚姛鑳戒粙緇嶏細(xì)
聽涓烘ц兘鑰冭檻錛屽崟涓鏈嶅姟鍣ㄦ敼鎴愰泦緹わ紙姣忓お鏈嶅姟鍣ㄦ暟鎹厑璁稿湪涓瀹氭椂闂村唴淇濇寔鐩告錛夛紝緇欏嚭鐨勪慨鏀規(guī)椂闂寸煭錛屼笉榪囦唬鐮佹寔涔呭眰姣旇緝緇熶竴錛坔ibernate 鎰熻阿澶╄繕濂芥槸濂癸紒 錛夈傜綉緇滆繛鎺ヤ笉紼沖畾錛堥搧璺唴緗戯紒錛夈?br />
瀹屾垚鍚庢晥鏋滐細(xì)
褰撶綉緇滆繛鎺ユ垚鍔熸椂錛屽鏁版嵁搴撶殑鍚屾銆?br />聽聽 褰?/font>
緗戠粶榪炴帴澶辮觸鏃訛紝鏈湴搴旂敤紼嬪簭榪愮敤hibernate鎷︽埅鍣ㄦ嫤鎴鎿嶄綔瀵硅薄騫惰褰曚笅鎿嶄綔鍔ㄤ綔錛屽簭鍒楀寲鍒版湰鍦版椂灞搴?z_jcyy_tb 琛ㄤ腑銆傝〃鏁版嵁灞炴т負(fù)錛歩d,inputdate(璁板綍鏃墮棿),object錛堝簭鍒楀璞★級(jí),action(鎿嶄綔鍔ㄤ綔)銆傚茍瀹変竴瀹氭椂闂存祴璇曡繛鎺ャ傚鏋滄垚鍔燂紝璇誨彇 z_jcyy_tb 琛ㄤ腑鏁版嵁 鍙嶅簭鍒楀寲 鍐嶅悓姝ュ埌 鍏朵粬鏁版嵁搴撲腑銆?br />
浠g爜璇存槑錛?br />1.鏂癝ession 寤虹珛
聽聽
hibernate.cfg.xml 鍦ㄦ枃浠?/font><session-factory>涓坊鍔?
聽聽聽 <property聽name="connection.url_b">jdbc:oracle:thin:@192.168.1.114:1521:JCYY</property>

聽聽聽聽
<property聽name="connection.username_b">jcyy</property>
聽聽聽聽
<property聽name="connection.password_b">jcyy</property>
聽聽 TBDao -> OpenSession()
聽聽聽聽privatestatic聽String聽url_b聽=null聽;
聽聽聽聽
privatestatic聽String聽use_b聽=null聽;
聽聽聽聽
privatestatic聽String聽pass_b聽=null聽;
聽聽聽聽
privatestatic聽String聽dirver_b聽=null聽;
聽聽聽聽
static聽{try聽{
聽聽聽聽聽聽聽 //鍙栧緱hibernate.cfg.xml閫昏緫璺緞錛屽拰鍘熸潵紼嬪簭鍏寵仈涓娐?
聽聽聽聽聽聽聽聽Field聽field聽聽聽
=聽聽聽SessionManager.class.getDeclaredField("CONFIG_FILE_LOCATION");
聽聽聽聽聽聽聽聽field.setAccessible(聽
true聽);
聽聽聽聽聽聽聽聽String聽path聽聽
=聽聽(String)聽field.get(SessionManager.聽class聽);

聽聽聽聽聽聽聽 //閫氳繃 dom4j 鍔犺澆 閰嶇疆鏂囦歡聽 聽

聽聽聽聽聽聽聽聽Document聽docT聽
=new聽SAXReader().read(聽TBDao.class.getResourceAsStream(path)聽);

聽聽聽 聽聽聽 //姝e垯+xpath璇誨彇 鍦╤bn鏂囦歡涓姞鍏ョ殑
<property聽name="..._b"> 鐨勫睘鎬?/font>
聽聽聽聽聽聽聽聽String聽xpath聽
="/hibernate-configuration/session-factory/property[@name='XPATH_I']"聽;
聽聽聽聽聽聽聽聽Pattern聽p聽
=聽Pattern.compile("(XPATH_I)");
聽聽聽聽聽聽聽聽Matcher聽ma聽
=聽p.matcher(xpath);
聽聽聽聽聽聽聽聽url_b聽聽
=聽DocumentHelper.createXPath(聽ma.replaceAll("connection.url_b")聽).selectSingleNode(docT).getText();
聽聽聽聽聽聽聽聽use_b聽
=聽DocumentHelper.createXPath(聽ma.replaceAll("connection.username_b")).selectSingleNode(docT).getText();
聽聽聽聽聽聽聽聽pass_b聽
=聽DocumentHelper.createXPath(聽ma.replaceAll("connection.password_b")).selectSingleNode(docT).getText();
聽聽聽聽聽聽聽聽dirver_b聽
=聽DocumentHelper.createXPath(聽ma.replaceAll("connection.driver_class")).selectSingleNode(docT).getText();
聽聽聽聽}聽
catch聽(Exception聽e)聽{e.printStackTrace();}}
聽聽聽
聽聽聽 //鍒╃敤hbn鐨凷essionFactory寰楀埌 openSession(Connection); 鎵撳紑寮傚湴鏁版嵁搴撹繛鎺ャ?
聽聽聽 //鍒╃敤縐佹湁鍙嶅皠寰楀埌 鍔犺澆瀹屾垚鐨凷essionFactory
聽聽聽聽public聽Session聽openSessionb(){
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽Class.forName(dirver_b);
聽聽聽聽聽聽聽聽聽聽聽聽Connection聽conn聽
=聽聽DriverManager.getConnection(url_b,use_b,pass_b);
聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽Field[]聽fields聽聽聽
=聽聽聽SessionManager.class.getDeclaredFields();
聽聽聽聽聽聽聽聽聽聽聽聽Field聽field聽
=null聽;
聽聽聽聽聽聽聽聽聽聽聽聽
for(int聽i=0;i<fields.length;i++){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if(聽SessionFactory.class.equals(聽聽fields[i].getType()聽)聽聽)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽field聽
=聽fields[i];
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽field.setAccessible(
true);
聽聽聽聽聽聽聽聽聽聽聽聽SessionFactory聽sessionFactory聽聽
=聽聽(SessionFactory)聽field.get(SessionManager.class聽);
聽聽聽聽聽聽聽聽聽聽聽聽
return聽sessionFactory.openSession(conn);
聽聽聽聽聽聽聽聽}聽
catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(
"--娌℃湁榪炴帴鍒版繪湇鍔?openSessionb)--");
聽聽聽聽聽聽聽聽聽聽聽聽
returnnull聽;
聽聽聽聽聽聽聽聽}
聽聽聽聽}

2.寮傚湴鏁版嵁鍚屾澶辮觸鍚庡姩浣溌?TBDao->save() 鍐葷粨鐘舵佸埌鏁版嵁搴?br />
聽聽聽聽publicvoid聽save(Object聽obj,String聽action)聽{
聽聽聽聽聽聽聽聽Session聽session聽
=null聽;
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽session聽
=聽SessionManager.currentSession(null,null);
聽聽聽聽聽聽聽聽聽聽聽聽Transaction聽tr聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽聽聽聽聽ZJcyyTb聽zj聽
=new聽ZJcyyTb();
聽聽聽聽聽聽聽聽聽聽聽聽zj.setAction(action);
聽聽聽聽聽聽聽聽聽聽聽聽zj.setInputdate(
new聽Date());
聽聽聽聽聽聽聽聽聽聽聽聽session.save(zj);
聽聽聽聽聽聽聽聽聽聽聽聽session.flush();
聽聽聽聽聽聽聽聽聽聽聽聽session.refresh(zj,LockMode.UPGRADE);
聽聽聽聽聽聽聽聽聽聽聽 //oracle Blob鏁版嵁鎸佷箙 璇峰弬鑰?->
搴忓垪鍖栧拰鍙嶅簭鍒楀寲瀵硅薄鍒?鏁版嵁搴?/a>
聽聽聽聽聽聽聽聽聽聽聽 zj.setObject(聽new聽ObjectConvert().ObjectToBlob(obj)聽);
聽聽聽聽聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽}聽
catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();
聽聽聽聽聽聽聽聽}
finally{
聽聽聽聽聽聽聽聽聽聽聽聽
if(session!=null&&聽session.isOpen()聽)session.close();
聽聽聽聽聽聽聽聽}

聽聽聽聽}

3,澶辮觸鍚庡張鎴愬姛榪炴帴鍚?錛堢嚎紼嬪疄鐜幫級(jí) TBDao->action()
聽聽聽聽publicint聽isSql(){
聽聽聽聽聽聽聽聽
int聽is_count聽=0聽;
聽聽聽聽聽聽聽聽Session聽session聽
=null聽;
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽 //寰楀埌鏈湴Session 鏌ョ湅鏄惁鏈夎繛鎺ュけ璐ュ悗搴忓垪鍔ㄤ綔琚繚瀛?/font>
聽聽聽聽聽聽聽聽聽聽聽聽session聽
=聽SessionManager.currentSession(null,null);
聽聽聽聽聽聽聽聽聽聽聽聽Transaction聽tr聽
=聽聽session.beginTransaction();
聽聽聽聽聽聽聽聽聽聽聽聽Connection聽conn聽
=聽session.connection();
聽聽聽聽聽聽聽聽聽聽聽聽Statement聽stat聽
=聽conn.createStatement();
聽聽聽聽聽聽聽聽聽聽聽聽ResultSet聽rs聽
=聽stat.executeQuery("select聽count(*)聽from聽z_jcyy_tb");
聽聽聽聽聽聽聽聽聽聽聽聽rs.next();
聽聽聽聽聽聽聽聽聽聽聽聽is_count聽
=聽rs.getInt(1);
聽聽聽聽聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽}聽
catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();
聽聽聽聽聽聽聽聽}
finally{
聽聽聽聽聽聽聽聽聽聽聽聽
if(session!=null&&聽session.isOpen()聽)session.close();
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
return聽is_count聽;
聽聽聽聽}

聽聽聽聽
publicboolean聽action(){
聽聽聽聽聽聽聽聽
int聽isSql聽=0聽;
聽聽聽聽聽聽聽聽ObjectConvert聽oc聽
=new聽ObjectConvert();
聽聽聽聽聽聽聽聽Session聽session聽
=null聽;
聽聽聽聽聽聽聽聽Session聽session_b聽
=null聽;
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽 //鏈夊け璐ヨ繛鎺ュ姩浣滃悗灝濊瘯 榪滅▼鏁版嵁搴撀?
聽聽聽聽聽聽聽聽聽聽聽聽
if(聽(isSql=isSql())>0聽){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session聽
=聽SessionManager.currentSession(null,null);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //榪滅▼鏁版嵁搴撹繛鎺ヂ?
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //濡傛灉鎴愬姛榪炴帴錛歾_jcyy_tb琛ㄤ腑鏁版嵁鍚屾鍒板叾浠栨暟鎹簱涓?/font>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session_b聽
=聽openSessionb();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if(session_b!=null){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Transaction聽tr_b聽
=聽session_b.beginTransaction();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Transaction聽tr聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Query聽qu聽
=聽session.createQuery("聽from聽ZJcyyTb聽t聽order聽by聽t.inputdate");
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
for(int聽i=0;i<=isSql/10;i++){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽qu.setFirstResult(i
*10);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽qu.setMaxResults(
10);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽List聽list聽
=聽qu.list();聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
for(Iterator聽it=list.iterator();it.hasNext();){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽ZJcyyTb聽tb聽
=聽(ZJcyyTb)it.next();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Object聽obj聽
=null聽;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽obj聽
=聽oc.BlobToObject(tb.getObject(),obj);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if(obj!=null){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽String聽action聽
=聽tb.getAction();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if(action.equals(聽TBDao.DELETE聽)){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session_b.delete(obj);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if(action.equals(聽TBDao.INSERT聽)){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session_b.save(obj);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if(action.equals(聽TBDao.UPDATE聽)){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session_b.update(obj);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session.delete(tb);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tr_b.commit();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽}聽聽聽聽
returntrue聽;
聽聽聽聽聽聽聽聽}聽
catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(
"--娌℃湁榪炴帴鍒版繪湇鍔?action)--");
聽聽聽聽聽聽聽聽}
finally{
聽聽聽聽聽聽聽聽聽聽聽聽
if(session_b!=null&&session_b.isOpen())session_b.close();
聽聽聽聽聽聽聽聽聽聽聽聽
if(session!=null&&聽session.isOpen()聽)session.close();
聽聽聽聽聽聽聽聽聽聽聽聽SessionManager.closeSession();聽聽聽聽
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
returnfalse聽;
聽聽聽聽}

4.hbn 鎷︽埅鍣?->Interceptor
package聽com.jjm.hibernate;

import聽java.io.File;
import聽java.io.FileInputStream;
import聽java.io.InputStream;
import聽java.io.Serializable;
import聽java.lang.reflect.Field;
import聽java.sql.Connection;
import聽java.sql.DriverManager;
import聽java.util.HashSet;
import聽java.util.Iterator;
import聽java.util.Set;

import聽org.dom4j.Document;
import聽org.dom4j.DocumentHelper;
import聽org.dom4j.XPath;
import聽org.dom4j.io.SAXReader;
import聽org.hibernate.CallbackException;
import聽org.hibernate.EntityMode;
import聽org.hibernate.Hibernate;
import聽org.hibernate.HibernateException;
import聽org.hibernate.Interceptor;
import聽org.hibernate.Session;
import聽org.hibernate.SessionFactory;
import聽org.hibernate.Transaction;
import聽org.hibernate.cfg.Configuration;
import聽org.hibernate.type.Type;

import聽com.jjm.rlzy.dao.TBDao;

publicclass聽TestInterceptor聽implements聽I(yíng)nterceptor,Serializable{
聽聽聽聽
聽聽聽聽
staticprivateboolean聽isConn_b聽=false聽;
聽聽聽聽
staticprivate聽TBDao聽tb聽=new聽TBDao();
聽聽聽 //綰跨▼ 涓鍒嗛挓 媯(gè)嫻嬭繛鎺ュけ璐?鍚屾
聽聽聽聽
static{
聽聽聽聽聽聽聽聽
new聽Thread(new聽Runnable(){
聽聽聽聽聽聽聽聽聽聽聽聽
publicvoid聽run()聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
while(true){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽isConn_b聽
=聽tb.action();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Thread.sleep(
60*1000);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}聽
catch聽(InterruptedException聽e)聽{e.printStackTrace();}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}).start();
聽聽聽聽}

聽聽聽聽
publicboolean聽onFlushDirty(Object聽entity,聽Serializable聽id,聽Object[]聽currentState,聽Object[]聽previousState,聽String[]聽propertyNames,聽Type[]聽types)聽throws聽CallbackException聽{
聽聽聽聽聽聽聽聽Session聽session聽
=null聽;
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽
if(isConn_b){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session聽
=聽聽tb.openSessionb();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Transaction聽tr聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session.update(entity);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽聽聽聽聽}
else{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tb.save(entity,TBDao.UPDATE);
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}聽
catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace()聽;
聽聽聽聽聽聽聽聽聽聽聽聽tb.save(entity,TBDao.UPDATE);
聽聽聽聽聽聽聽聽聽聽聽聽isConn_b聽
=false聽;
聽聽聽聽聽聽聽聽}
finally{
聽聽聽聽聽聽聽聽聽聽聽聽
if(session!=null)session.close();
// 鎷︽埅鍣ㄤ腑 緇濆涓嶈兘鏈夎繖鍙?->聽 SessionManager.closeSession();
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
returnfalse;
聽聽聽聽}

聽聽聽聽
publicboolean聽onSave(Object聽entity,聽Serializable聽id,聽Object[]聽state,聽String[]聽propertyNames,聽Type[]聽types)聽throws聽CallbackException聽{
聽聽聽聽聽聽聽聽Session聽session聽
=null聽;
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽
if(isConn_b){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session聽
=聽聽tb.openSessionb();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Transaction聽tr聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session.save(entity);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽聽聽聽聽}
else{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tb.save(entity,TBDao.INSERT);
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}聽
catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace()聽;
聽聽聽聽聽聽聽聽聽聽聽聽tb.save(entity,TBDao.INSERT);
聽聽聽聽聽聽聽聽聽聽聽聽isConn_b聽
=false聽;
聽聽聽聽聽聽聽聽}
finally{
聽聽聽聽聽聽聽聽聽聽聽聽
if(session!=null)session.close();
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
returnfalse;
聽聽聽聽}

聽聽聽聽
publicvoid聽onDelete(Object聽entity,聽Serializable聽id,聽Object[]聽state,聽String[]聽propertyNames,聽Type[]聽types)聽throws聽CallbackException聽{
聽聽聽聽聽聽聽聽Session聽session聽
=null聽;
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽
if(isConn_b){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session聽
=聽聽tb.openSessionb();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Transaction聽tr聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽session.delete(entity);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽聽聽聽聽}
else{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tb.save(entity,TBDao.DELETE);
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}聽
catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace()聽;
聽聽聽聽聽聽聽聽聽聽聽聽tb.save(entity,TBDao.DELETE);
聽聽聽聽聽聽聽聽聽聽聽聽isConn_b聽
=false聽;
聽聽聽聽聽聽聽聽}
finally{
聽聽聽聽聽聽聽聽聽聽聽聽
if(session!=null)session.close();
聽聽聽聽聽聽聽聽}
聽聽聽聽}

聽 ................................


}


鍚庤錛?br />鐢變簬涓浜涘師鍥犱唬鐮佸啓鐨勬湁浜涚畝闄嬶紝浣嗗姛鑳藉疄鐜幫紝琛ㄨ揪鐨勭殑鎰忔濅篃榪樺彲浠ワ紙鑷繁鏈夌偣 鈥樹拱鐡滀簡(jiǎn)鈥欏搱鍝堬紒錛夈傛垜鍐欏嚭鏉ョ殑鐩殑甯屾湜澶у鑳藉叡鍚岃繘姝ワ紝榪欑瓑澶у鎷嶇爾浜?錛氾級(jí)



聽聽聽聽 聽聽聽




G_G 2007-12-03 11:29 鍙戣〃璇勮
]]>
hbn 鎷︽埅鍣?/title><link>http://www.tkk7.com/Good-Game/archive/2007/11/29/163922.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Thu, 29 Nov 2007 02:13:00 GMT</pubDate><guid>http://www.tkk7.com/Good-Game/archive/2007/11/29/163922.html</guid><wfw:comment>http://www.tkk7.com/Good-Game/comments/163922.html</wfw:comment><comments>http://www.tkk7.com/Good-Game/archive/2007/11/29/163922.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/Good-Game/comments/commentRss/163922.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/Good-Game/services/trackbacks/163922.html</trackback:ping><description><![CDATA[ <font color="#ffa500"> <b>鎷︽埅鍣?/b> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"> <!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <span style="color: rgb(0, 0, 255);">package</span> <span style="color: rgb(0, 0, 0);">聽hbn.test.supper.Interceptor;<br /><br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽java.io.Serializable;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽java.util.HashSet;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽java.util.Iterator;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽java.util.Set;<br /><br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽org.hibernate.CallbackException;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽org.hibernate.EntityMode;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽org.hibernate.Interceptor;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽org.hibernate.Transaction;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽org.hibernate.type.Type;<br /><br /></span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">class</span> <span style="color: rgb(0, 0, 0);">聽TestInterceptor聽</span> <span style="color: rgb(0, 0, 255);">implements</span> <span style="color: rgb(0, 0, 0);">聽I(yíng)nterceptor,Serializable{<br /><br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 0);">聽Set聽inserts聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);">聽HashSet();<br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 0);">聽Set聽updates聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);">聽HashSet();<br />聽聽聽聽<br />聽聽 聽</span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">Session鍒濆寲涓涓寔涔呭璞?濡傛灉榪欐柟娉曚腑鏀瑰彉浜?jiǎn)瀵硅薄灞炴у氨榪斿洖true 鍚﹀垯null</span> <span style="color: rgb(0, 128, 0);"> </span> <span style="color: rgb(0, 0, 0);"> </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">boolean</span> <span style="color: rgb(0, 0, 0);">聽onLoad(Object聽entity,聽Serializable聽id,聽Object[]聽state,聽String[]聽propertyNames,聽Type[]聽types)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{<br />聽聽聽聽</span> <span style="color: rgb(0, 0, 0);">聽聽聽 </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">false</span> <span style="color: rgb(0, 0, 0);">;<br />聽聽聽聽}<br /></span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">聽聽聽 //</span> <span style="color: rgb(0, 128, 0);">Session flush()涓鏌ュ埌鑴忔暟鎹槸璋冪敤 濡傦細(xì)tr.commit() ....</span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">boolean</span> <span style="color: rgb(0, 0, 0);">聽onFlushDirty(Object聽entity,聽Serializable聽id,聽Object[]聽currentState,聽Object[]聽previousState,聽String[]聽propertyNames,聽Type[]聽types)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{<br />聽聽聽聽聽聽聽聽updates.add(entity);<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">false</span> <span style="color: rgb(0, 0, 0);">;<br />聽聽聽聽}<br />聽聽聽 </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">Session Save() 褰撲慨鏀逛簡(jiǎn)瀵硅薄灞炴ц繑鍥瀟rue </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">boolean</span> <span style="color: rgb(0, 0, 0);">聽onSave(Object聽entity,聽Serializable聽id,聽Object[]聽state,聽String[]聽propertyNames,聽Type[]聽types)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{<br />聽聽聽聽聽聽聽聽<br />聽聽聽聽聽聽聽聽inserts.add(entity);<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">false</span> <span style="color: rgb(0, 0, 0);">;<br />聽聽聽聽}<br />聽聽聽聽 </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">delete </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">聽onDelete(Object聽entity,聽Serializable聽id,聽Object[]聽state,聽String[]聽propertyNames,聽Type[]聽types)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{</span> <span style="color: rgb(0, 0, 0);"> <br />聽聽聽聽}<br /></span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 128, 0);">聽聽 //flush() 涔嬪墠璋冪敤</span> <span style="color: rgb(0, 128, 0);"> </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">聽preFlush(Iterator聽entities)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{</span> <span style="color: rgb(0, 0, 0);"> <br />聽聽聽聽}<br />聽聽聽聽 </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">flush() 鎵цSQL璇彞涔嬪悗璋冪敤 </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">聽postFlush(Iterator聽entities)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{<br />聽聽聽聽聽聽聽聽<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">try</span> <span style="color: rgb(0, 0, 0);">聽{<br />聽聽聽聽聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">for</span> <span style="color: rgb(0, 0, 0);">(Iterator聽it聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽updates.iterator();it.hasNext();){<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">update=</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">+</span> <span style="color: rgb(0, 0, 0);">聽it.next()聽);聽聽聽聽<br />聽聽聽聽聽聽聽聽聽聽聽聽}<br />聽聽聽聽聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">for</span> <span style="color: rgb(0, 0, 0);">(Iterator聽it聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽inserts.iterator();it.hasNext();){<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">insert</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">+</span> <span style="color: rgb(0, 0, 0);">聽it.next()聽);聽聽聽聽<br />聽聽聽聽聽聽聽聽聽聽聽聽}<br />聽聽聽聽聽聽聽聽聽聽聽聽<br />聽聽聽聽聽聽聽聽}聽</span> <span style="color: rgb(0, 0, 255);">catch</span> <span style="color: rgb(0, 0, 0);">聽(Exception聽e)聽{<br />聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();<br />聽聽聽聽聽聽聽聽}<br />聽聽聽聽聽聽聽聽<br />聽聽聽聽}<br /><br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽Boolean聽isTransient(Object聽entity)聽{<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">聽TODO聽Auto-generated聽method聽stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />聽聽聽聽}<br /></span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 128, 0);">聽 //鍐沖畾Session涓偅浜涘璞℃槸鑴忔暟鎹?濡傛灉null Session浣跨敤榛樿澶勭悊鑴忔暟鎹?/span> <span style="color: rgb(0, 128, 0);"> </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">int</span> <span style="color: rgb(0, 0, 0);">[]聽findDirty(Object聽entity,聽Serializable聽id,聽Object[]聽currentState,聽Object[]聽previousState,聽String[]聽propertyNames,聽Type[]聽types)聽{</span> <span style="color: rgb(0, 128, 0);"> </span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />聽聽聽聽}<br /><br />聽聽聽聽 </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">// 褰揝ession鏋勯犲疄浣撶被瀵硅薄鍓嶈皟鐢?/span> <span style="color: rgb(0, 128, 0);"> </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽Object聽instantiate(String聽entityName,聽EntityMode聽entityMode,聽Serializable聽id)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{</span> <span style="color: rgb(0, 128, 0);"> </span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />聽聽聽聽}<br /><br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽String聽getEntityName(Object聽object)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">聽TODO聽Auto-generated聽method聽stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />聽聽聽聽}<br /><br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽Object聽getEntity(String聽entityName,聽Serializable聽id)聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽CallbackException聽{<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">聽TODO聽Auto-generated聽method聽stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />聽聽聽聽}<br /><br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">聽afterTransactionBegin(Transaction聽tx)聽{<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">聽TODO聽Auto-generated聽method聽stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽<br />聽聽聽聽}<br /><br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">聽beforeTransactionCompletion(Transaction聽tx)聽{<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">聽TODO聽Auto-generated聽method聽stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽<br />聽聽聽聽}<br /><br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">聽afterTransactionCompletion(Transaction聽tx)聽{<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">聽TODO聽Auto-generated聽method聽stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽<br />聽聽聽聽}<br /><br />}<br /></span> </div> <b> <br />嫻嬭瘯</b> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"> <!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <span style="color: rgb(0, 0, 255);">package</span> <span style="color: rgb(0, 0, 0);">聽hbn.test.supper.Interceptor;<br /><br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽java.lang.reflect.Field;<br /><br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽org.hibernate.Session;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽org.hibernate.SessionFactory;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽org.hibernate.Transaction;<br /><br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽hbn.HibernateSessionFactory;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽hbn.bean.T2oo;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">聽junit.framework.TestCase;<br /><br /></span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">class</span> <span style="color: rgb(0, 0, 0);">聽TestIC聽</span> <span style="color: rgb(0, 0, 255);">extends</span> <span style="color: rgb(0, 0, 0);">聽TestCase聽{<br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 0);">聽SessionFactory聽sessionFactory;<br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">聽setUp()聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽Exception聽{<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">super</span> <span style="color: rgb(0, 0, 0);">.setUp();<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">鍒╃敤java鍙嶅皠寰楀埌聽HibernateSessionFactory聽-><br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">private聽聽static聽org.hibernate.SessionFactory聽sessionFactory;<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">瑕佹ā鎷熉犲茍鍙懧犺聽HibernateSessionFactory聽寰楀嚭鐨劼犳湁聽threadLocal聽涓嶈聽</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽HibernateSessionFactory.currentSession();<br />聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();<br />聽聽聽聽聽聽聽聽Field聽field聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽HibernateSessionFactory.</span> <span style="color: rgb(0, 0, 255);">class</span> <span style="color: rgb(0, 0, 0);">.getDeclaredField(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">sessionFactory</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">);<br />聽聽聽聽聽聽聽聽field.setAccessible(</span> <span style="color: rgb(0, 0, 255);">true</span> <span style="color: rgb(0, 0, 0);">);<br />聽聽聽聽聽聽聽聽sessionFactory聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽(SessionFactory)聽field.get(HibernateSessionFactory.</span> <span style="color: rgb(0, 0, 255);">class</span> <span style="color: rgb(0, 0, 0);">);<br />聽聽聽聽}<br />聽聽聽聽<br />聽聽聽聽</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">聽testInc()聽</span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);">聽Exception聽{<br />聽聽聽聽聽聽聽聽TestInterceptor聽intx聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);">聽TestInterceptor();<br />聽聽聽聽聽聽聽聽</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">鍔犺澆鎷︽埅鍣?/span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽Session聽session聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽sessionFactory.openSession(intx);<br />聽聽聽聽聽聽聽聽<br />聽聽聽聽聽聽聽聽Transaction聽tr聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽session.beginTransaction();<br />聽聽聽聽聽聽聽聽T2oo聽t2聽</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">聽</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);">聽T2oo(</span> <span style="color: rgb(0, 0, 0);">23</span> <span style="color: rgb(0, 0, 0);">);<br />聽聽聽聽聽聽聽聽session.save(t2);<br />聽聽聽聽聽聽聽聽t2.setAvg(</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);">聽I(yíng)nteger(</span> <span style="color: rgb(0, 0, 0);">99</span> <span style="color: rgb(0, 0, 0);">));<br />聽聽聽聽聽聽聽聽tr.commit();<br />聽聽聽聽}<br />}<br /></span> </div>緇撴灉<br /><font color="#006400">Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)<br />Hibernate: update t2oo set version=?, avg=?, aid=? where id=? and version=?<br />//鎷︽埅鍒扮殑<br />update=hbn.bean.T2oo@277<br />inserthbn.bean.T2oo@277</font><br /><b><br /><br /></b></font> <img src ="http://www.tkk7.com/Good-Game/aggbug/163922.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/Good-Game/" target="_blank">G_G</a> 2007-11-29 10:13 <a href="http://www.tkk7.com/Good-Game/archive/2007/11/29/163922.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>hibernate 闂_3(閿?http://www.tkk7.com/Good-Game/archive/2007/11/26/163310.htmlG_GG_GMon, 26 Nov 2007 10:57:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/11/26/163310.htmlhttp://www.tkk7.com/Good-Game/comments/163310.htmlhttp://www.tkk7.com/Good-Game/archive/2007/11/26/163310.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/163310.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/163310.html 鐜:
聽聽聽聽 private 聽SessionFactory聽sessionFactory;

聽聽聽聽
protected void 聽setUp()聽 throws 聽Exception聽{
聽聽聽聽聽聽聽聽
super .setUp();
聽聽聽聽聽聽聽聽
// 鍒╃敤java鍙嶅皠寰楀埌聽HibernateSessionFactory聽->
聽聽聽聽聽聽聽聽
// private聽聽static聽org.hibernate.SessionFactory聽sessionFactory;
聽聽聽聽聽聽聽聽
// 瑕佹ā鎷熉犲茍鍙懧犺聽HibernateSessionFactory聽寰楀嚭鐨劼犳湁聽threadLocal聽涓嶈聽
聽聽聽聽聽聽聽聽
// 瑕伮?/span>
聽聽聽聽聽聽聽聽HibernateSessionFactory.currentSession();
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽聽聽聽聽Field聽field聽
= 聽HibernateSessionFactory. class .getDeclaredField( " sessionFactory " );
聽聽聽聽聽聽聽聽field.setAccessible(
true );
聽聽聽聽聽聽聽聽sessionFactory聽
= 聽(SessionFactory)聽field.get(HibernateSessionFactory. class );
聽聽聽聽}

聽聽聽聽
protected void 聽tearDown()聽 throws 聽Exception聽{
聽聽聽聽聽聽聽聽
super .tearDown();
聽聽聽聽}

鎮(zhèn)茶閿?
聽聽聽聽 /** 聽鎮(zhèn)茶閿侀棶棰樎犵嚎紼嬫ā鎷熉犲茍鍙懧?br />聽聽聽聽聽*聽Table聽->聽T1oo(id,name)
聽聽聽聽聽*聽聽聽聽聽+----+------+
聽聽聽聽聽*聽聽聽聽|聽id聽|聽name聽|
聽聽聽聽聽*聽聽聽聽+----+------+
聽聽聽聽聽*聽聽聽聽|聽聽4聽|聽xx1聽聽|
聽聽聽聽聽*聽聽聽聽+----+------+
聽聽聽聽聽
*/
聽聽聽聽
public void 聽ctestLock()聽 throws 聽Exception聽{
聽聽聽聽聽聽聽聽
// 聽insert聽test聽Data 嫻嬭瘯鏁版嵁
聽聽聽聽聽聽聽聽Session聽seInsert聽 = 聽sessionFactory.openSession();
聽聽聽聽聽聽聽聽Transaction聽tr聽
= 聽seInsert.beginTransaction();
聽聽聽聽聽聽聽聽T1oo聽testUse聽
= 聽testUse聽 = new 聽T1oo();
聽聽聽聽聽聽聽聽testUse.setName(
" liukaiyi_test " );
聽聽聽聽聽聽聽聽seInsert.save(testUse);
聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽System.out.println(聽
" **************Test聽Use聽Bean聽:聽insert******************* " 聽);
聽聽聽聽聽聽聽聽seInsert.close();
聽聽聽聽聽聽聽聽I(yíng)nteger聽testId聽
= 聽testUse.getId();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽 //騫跺彂妯℃嫙
聽聽聽聽聽聽聽聽Session聽session1聽
= 聽sessionFactory.openSession();
聽聽聽聽聽聽聽聽Session聽session2聽
= 聽sessionFactory.openSession();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
final 聽Transaction聽tr1聽 = 聽session1.beginTransaction();
聽聽聽聽聽聽聽聽Transaction聽tr2聽
= 聽session2.beginTransaction();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Query聽qu1聽
= 聽session1.createQuery( " 聽from聽T1oo聽t1oo聽where聽t1oo.name='liukaiyi_test' " );
聽聽聽聽聽聽聽聽Query聽qu2聽
= 聽session2.createQuery( " 聽from聽T1oo聽t1oo聽where聽t1oo.name='liukaiyi_test' " );
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 //鍔犳?zhèn)茶閿?/font>
聽聽聽聽聽聽聽聽qu1.setLockMode(
" t1oo " ,LockMode.UPGRADE);
聽聽聽聽聽聽聽聽
final 聽Object聽bean1聽 = 聽qu1.uniqueResult();
聽聽聽聽聽聽聽聽Object聽bean2聽
= 聽qu2.uniqueResult();

聽聽聽聽聽聽聽聽T1oo聽t1oo2聽
= 聽(T1oo)bean2;
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽t1oo2.setName(
" run1 " );
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
new 聽Thread(
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
new 聽Runnable(){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
public void 聽run()聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
try 聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(聽
" ********瑙i攣鍑嗗********* " 聽);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Thread.sleep(
10 * 1000 );
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽T1oo聽t1oo1聽
= 聽(T1oo)bean1;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽t1oo1.setName(
" run2 " );
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tr1.commit();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(聽
" ********瑙i攣鎴愬姛t********* " 聽);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}聽
catch 聽(InterruptedException聽e)聽{e.printStackTrace();}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽).start();

聽聽聽聽聽聽聽
/ /榪欓噷浼?絳夊緟鍗佺鍚?tr1瑙i攣 /
聽聽聽聽聽聽聽聽tr2.commit();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽session1.close();
聽聽聽聽聽聽聽聽session2.close();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
// 聽delete聽Test聽Date 鍒犻櫎嫻嬭瘯鏁版嵁
聽聽聽聽聽聽聽聽Session聽seDelete聽 = 聽sessionFactory.openSession();
聽聽聽聽聽聽聽聽Transaction聽trD聽
= 聽seDelete.beginTransaction();
聽聽聽聽聽聽聽聽Object聽obj聽
= 聽seDelete.createQuery( " 聽from聽T1oo聽t聽where聽t.id=:id聽 " ).
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽setInteger(
" id " ,testId.intValue()).
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽uniqueResult();
聽聽聽聽聽聽聽聽seDelete.delete(obj);
聽聽聽聽聽聽聽聽trD.commit();
聽聽聽聽聽聽聽聽System.out.println(聽
" **************Test聽Use聽Bean聽:聽delete************** " 聽);
聽聽聽聽聽聽聽聽seDelete.close();
聽聽聽聽}

涔愯閿?
聽聽聽聽 /** 聽涔愯閿侀棶棰?br />聽聽聽聽聽*聽Table聽->聽T1oo(id,name)
聽聽聽聽聽*聽T2oo.hbn.xml聽<class>聽+>聽optimistic-lock="version"
聽聽聽聽聽*聽id涓嬮潰聽+>聽<version聽name="version"聽type="integer"聽column="version"聽/>
聽聽聽聽聽*聽聽聽聽+---------+---------+------+
聽聽聽聽聽*聽聽聽聽|聽Field聽聽聽|聽Type聽聽聽聽|聽Null聽|
聽聽聽聽聽*聽聽聽聽+---------+---------+------+
聽聽聽聽聽*聽聽聽聽|聽id聽聽聽聽聽聽|聽int(11)聽|聽聽聽聽聽聽|
聽聽聽聽聽*聽聽聽聽|聽avg聽聽聽聽聽|聽int(11)聽|聽聽聽聽聽聽|
聽聽聽聽聽*聽聽聽聽|聽aid聽聽聽聽聽|聽int(11)聽|聽YES聽聽|
聽聽聽聽聽*聽聽聽聽|聽version聽|聽int(11)聽|聽YES聽聽|
聽聽聽聽聽*聽聽聽聽+---------+---------+------+
聽聽聽聽聽*聽聽娉ㄦ剰錛氳鍦ㄦ暟鎹簱涓鍔犱竴鍒椔?br />聽聽聽聽聽*聽聽mysql>聽alter聽table聽T2OO聽add聽version聽int;
聽聽聽聽聽
*/
聽聽聽聽
public void 聽testOpLock() throws 聽Exception{
聽聽聽聽聽聽聽 //娣誨姞嫻嬭瘯鏁版嵁
聽聽聽聽聽聽聽聽Session聽session聽
= 聽sessionFactory.openSession();
聽聽聽聽聽聽聽聽Transaction聽trI聽
= 聽session.beginTransaction();
聽聽聽聽聽聽聽聽Connection聽conn聽
= 聽session.connection();
聽聽聽聽聽聽聽聽T2oo聽t2oo聽
= new 聽T2oo();
聽聽聽聽聽聽聽聽T1oo聽t1oo聽
= new 聽T1oo( " t1ooOpLock " );
聽聽聽聽聽聽聽聽t1oo.setT2ooSet(
new 聽HashSet());
聽聽聽聽聽聽聽聽t2oo.setAvg(
new 聽I(yíng)nteger( 23 ));聽聽聽聽
聽聽聽聽聽聽聽聽t2oo.setT1oo(t1oo);
聽聽聽聽聽聽聽聽t1oo.getT2ooSet().add(t2oo);
聽聽聽聽聽聽聽聽session.save(t2oo);
聽聽聽聽聽聽聽聽trI.commit();
聽聽聽聽聽聽聽聽session.clear();
聽聽聽聽聽聽聽聽session.close();

聽聽聽聽聽聽聽 //騫跺彂妯℃嫙
聽聽聽聽聽聽聽聽Session聽se1聽
= 聽sessionFactory.openSession();
聽聽聽聽聽聽聽聽Session聽se2聽
= 聽sessionFactory.openSession();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Transaction聽tr1聽
= 聽se1.beginTransaction();
聽聽聽聽聽聽聽聽Transaction聽tr2聽
= 聽se2.beginTransaction();
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽T1oo聽obj1聽
= 聽(T1oo)se1.load(T1oo. class , new 聽I(yíng)nteger( 1 ));
聽聽聽聽聽聽聽聽T1oo聽obj2聽
= 聽(T1oo)se2.load(T1oo. class , new 聽I(yíng)nteger( 1 ));
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽((T2oo)obj1.getT2ooSet().iterator().next()).setAvg(
new 聽I(yíng)nteger( 9 ));
聽聽聽聽聽聽聽聽((T2oo)obj2.getT2ooSet().iterator().next()).setAvg(
new 聽I(yíng)nteger( 10 ));
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽tr1.commit();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
try 聽{
聽聽聽聽聽聽聽聽聽聽聽聽tr2.commit();
聽聽聽聽聽聽聽聽}聽
catch 聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽se2.clear();
聽聽聽聽聽聽聽聽聽聽聽聽tr2.commit();
聽聽聽聽聽聽聽聽聽聽聽聽
// tr2鎶ラ敊
聽聽聽聽聽聽聽聽} finally {
聽聽聽聽聽聽聽聽聽聽聽聽se1.close();
聽聽聽聽聽聽聽聽聽聽聽聽se2.close();
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 //嫻嬭瘯鏁版嵁鍒犻櫎
聽聽聽聽聽聽聽聽Session聽dele聽
= 聽sessionFactory.openSession();
聽聽聽聽聽聽聽聽Transaction聽tr聽
= 聽dele.beginTransaction();
聽聽聽聽聽聽聽聽dele.delete(obj1);
聽聽聽聽聽聽聽聽tr.commit();
聽聽聽聽聽聽聽聽dele.close();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽}



G_G 2007-11-26 18:57 鍙戣〃璇勮
]]>
hibernate 闂灝忕粨2 (inverse鍜宑ascade)http://www.tkk7.com/Good-Game/archive/2007/11/16/161100.htmlG_GG_GFri, 16 Nov 2007 11:20:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/11/16/161100.htmlhttp://www.tkk7.com/Good-Game/comments/161100.htmlhttp://www.tkk7.com/Good-Game/archive/2007/11/16/161100.html#Feedback2http://www.tkk7.com/Good-Game/comments/commentRss/161100.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/161100.html inverse 浣跨敤
璇存槑綰﹀畾-濡傦細(xì)
1.琛ㄥ睘鎬?:T1oo->id,name
聽聽聽 聽聽 聽聽 T2oo->id,avg,aid(
澶栭敭 ),version

2.浠g爜榪囩▼鏄細(xì)Save->t1oo瀵硅薄(T1oo 澶栭敭 T2oo)
聽聽聽 聽聽聽 T1oo t1oo = new T1oo();
聽聽聽 聽聽聽 t1oo.setName("xx");聽聽聽 聽聽聽
聽聽聽 聽聽聽 t1oo.setT2ooSet(new HashSet());
聽聽聽 聽聽聽 T2oo t2oo2 = new T2oo(24);
聽聽聽 聽聽聽 //t2oo2.setT1oo(t1oo); (鍦ㄤ笅闈㈢殑鏈敤渚嬭〃涓殑絎?灞炴?)
聽聽聽 聽聽聽 t1oo.getT2ooSet().add(t2oo2);
(鍦ㄤ笅闈㈢殑鏈敤渚嬭〃涓殑絎?灞炴?)
聽聽聽 聽聽聽 session.save(t1oo);

3.鏈緥琛ㄤ嬌鐢細(xì)
T1oo.hbm.xml ->
聽聽聽聽聽聽聽 <set name="t2ooSet" inverse="true" cascade = "all"聽 >
(鍦ㄤ笅闈㈢殑鏈敤渚嬭〃涓殑絎?錛?灞炴?)
聽聽聽聽聽聽聽聽聽聽聽 <key column="aid"/>
聽聽聽聽聽聽聽聽聽聽聽 <one-to-many class="T2oo"/>
聽聽聽聽聽聽聽 </set>
T2oo.hbm.xml ->
聽聽聽 聽聽聽 <many-to-one name="t1oo" column="aid"聽 class="T1oo" cascade="all" />
(鍦ㄤ笅闈㈢殑鏈敤渚嬭〃涓殑絎?錛?灞炴?)
緇撳悎涓婇潰璇存槑寰楄〃涓猴細(xì)
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 | all聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+


4.鎵ц Hibernate璇彞 錛?/b>
聽聽聽 Hibernate: insert into t1oo (name, id) values (?, ?)
聽聽聽 Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
5.緇撴灉涓猴細(xì)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 2 |聽 24 | NULL |聽聽聽聽聽聽 0 | //涓昏灝辨槸鐪?aid灞炴?/b>
+----+-----+------+---------+
1 row in set (0.00 sec)


mysql> select * from t1oo; //
鍥犱負(fù) T1oo鎬繪槸鍙互 Save 涓嬮潰灝變笉鍐嶆彁浜?/font>
+----+------+
| id | name |
+----+------+
|聽 2 | xx聽聽 |
+----+------+
1 row in set (0.00 sec)

鍙敼欏癸細(xì)

+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | 鍙敼4 | 鍙敼2聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽聽聽聽聽聽聽聽聽聽 |聽聽 鍙敼1聽 |
| t2oo聽 |聽聽聽聽聽聽 | 鍙敼3 |聽聽聽 聽聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+



涓繪湰:
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 | all聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 true聽聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 1 |聽 24 |聽聽聽 1 |聽聽聽聽聽聽 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)


鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 | all聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 1 |聽 24 | NULL|聽聽聽聽聽聽 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)

鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 |聽聽聽聽聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 true聽聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Empty set (0.00 sec)

鏀?聽 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 |聽聽聽聽聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Empty set (0.00 sec)


鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 | all聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 true聽聽聽 |
| t2oo聽 |聽聽聽聽聽聽 |聽聽聽聽聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 1 |聽 24 |聽聽聽 1 |聽聽聽聽聽聽 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)

鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 |聽聽聽聽聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 true聽聽聽 |
| t2oo聽 |聽聽聽聽聽聽 |聽聽聽聽聽聽 |聽聽聽 聽 聽 聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Empty set (0.00 sec)


鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 | all聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 |聽聽聽聽聽聽 |聽聽聽 聽 聽 聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 1 |聽 24 | NULL |聽聽聽聽聽聽 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)

鏀? 鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | true聽 |聽聽聽聽聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 |聽聽聽聽聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Empty set (0.00 sec)

鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | false | all聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 true聽聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 1 |聽 24 |聽聽聽 1 |聽聽聽聽聽聽 0 |
+----+-----+------+---------+
1 row in set (0.02 sec)

鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | false | all聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽 聽 聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 1 |聽 24 |聽聽聽 1 |聽聽聽聽聽聽 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)

鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | false | 聽 聽 聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 true聽聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽 聽 聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: update t2oo set aid=? where id=?
Empty set (0.00 sec)

鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | false | all聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 true聽聽聽 |
| t2oo聽 |聽聽聽聽聽聽 |聽聽聽聽聽聽 |聽聽聽 聽 聽 聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 1 |聽 24 |聽聽聽 1 |聽聽聽聽聽聽 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)

鏀? 鏀? 鏀?

+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | false |聽聽聽聽聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 true聽聽聽 |
| t2oo聽 |聽聽聽聽聽聽 |聽聽聽聽聽聽 |聽聽聽 聽 聽 聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: update t2oo set aid=? where id=?
Empty set (0.00 sec)

鏀? 鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | false |聽聽聽聽聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 | all聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: update t2oo set aid=? where id=?
Empty set (0.00 sec)


鏀? 鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | false |聽 all聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 |聽聽聽聽聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid聽 | version |
+----+-----+------+---------+
|聽 1 |聽 24 |聽聽聽 1 |聽聽聽聽聽聽 0 |
+----+-----+------+---------+
1 row in set (0.02 sec)

鏀? 鏀? 鏀? 鏀?
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo聽 | false |聽聽聽聽聽聽 |聽聽 聽聽聽 聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+聽 true聽聽聽 |聽 false聽聽 |
| t2oo聽 |聽聽聽聽聽聽 |聽聽聽聽聽聽 |聽聽聽 聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽 |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: update t2oo set aid=? where id=?
Empty set (0.00 sec)








G_G 2007-11-16 19:20 鍙戣〃璇勮
]]>
hibernate 閮ㄥ垎闂鎬葷粨鈥斺?http://www.tkk7.com/Good-Game/archive/2007/11/12/160000.htmlG_GG_GMon, 12 Nov 2007 08:29:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/11/12/160000.htmlhttp://www.tkk7.com/Good-Game/comments/160000.htmlhttp://www.tkk7.com/Good-Game/archive/2007/11/12/160000.html#Feedback1http://www.tkk7.com/Good-Game/comments/commentRss/160000.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/160000.html 緙撳瓨闂錛?br />
聽聽聽聽 /** 聽浣跨敤聽Query.executeUpdate()聽緙撳瓨涓暟鎹笉鍚屾聽瑙e喅鍔炴硶
聽聽聽聽聽*聽聽Table聽->聽T1oo(id,name)
聽聽聽聽聽*聽Table聽->聽T2oo(id,avg,aid)
聽聽聽聽聽*聽澶栭敭聽T1oo.id->T2oo.aid
聽聽聽聽聽*聽聽Session.createQuery("delete聽T1oo")聽->聽聽Query.executeUpdate()聽
聽聽聽聽
*/
聽聽聽聽
public void 聽testExecuteUpdate(){
聽聽聽聽聽聽聽聽System.out.println(
" \r\n\r\n********************ExecuteUpdate************************ " );
聽聽聽聽聽聽聽聽T1oo聽t1oo聽
= new 聽T1oo();
聽聽聽聽聽聽聽聽t1oo.setName(
" liukaiyi " );
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Session聽session聽
= 聽HibernateSessionFactory.currentSession();
聽聽聽聽Transaction聽tr1聽
= 聽session.beginTransaction();
聽聽聽聽聽聽聽聽
// t1聽鎴愪負(fù)聽鎸佷箙鐘舵伮犱竴綰х紦瀛樹腑聽鍔犺澆
聽聽聽聽聽聽聽聽session.saveOrUpdate(t1oo);聽
聽聽聽聽聽聽聽聽
// 鐩存帴涓鏉¤鍙ュ垹闄1oo錛岀紦瀛樻棤娉曞悓姝?br />聽聽聽聽聽聽聽聽 // 涓綰х紦瀛樹腑榪樻湁聽t1
聽聽聽聽聽聽聽聽Query聽qu聽 = 聽session.createQuery( " delete聽T1oo " );
聽聽聽聽聽聽聽聽
try 聽{
聽聽聽聽聽聽聽聽聽聽聽聽qu.executeUpdate();
聽聽聽聽聽聽聽聽}聽
catch 聽(Exception聽e)聽{聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(
" //err:聽鏈夌駭鑱斅犲崟浣跨敤聽delete聽T1oo聽榪樿delete聽T2oo.aid聽=聽T1oo.id// " );
聽聽聽聽聽聽聽聽聽聽聽聽List聽list聽
= 聽session.createQuery( " from聽T1oo " ).list();
聽聽聽聽聽聽聽聽聽聽聽聽
for (Iterator聽it = list.iterator();it.hasNext();){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Query聽t2qu聽
= 聽session.createQuery( " delete聽T2oo聽聽where聽aid=:id " );
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽t2qu.setInteger(
" id " ,聽((T1oo)it.next()).getId().intValue());
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽t2qu.executeUpdate();聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽qu.executeUpdate();
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽tr1.commit();
聽聽聽聽聽聽聽聽
聽聽聽聽Transaction聽tr2聽
= 聽session.beginTransaction();
聽聽聽聽聽聽聽聽
// 榪欑洿鎺ラ氳繃涓綰х紦瀛樹腑鍔犺澆t2錛屼絾DB涓互娌℃湁姝ゆ潯鏁版嵁
聽聽聽聽聽聽聽聽t1oo聽 = 聽(T1oo)session.load(T1oo. class ,t1oo.getId());
聽聽聽聽聽聽聽聽t1oo.setName(
" google " );
聽聽聽聽聽聽聽聽
try 聽{
聽聽聽聽聽聽聽聽聽聽聽聽tr2.commit();聽聽聽聽
聽聽聽聽聽聽聽聽}聽
catch 聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(
" //err:聽update(t1oo)->DB聽涓暟鎹簱涓病鏈壜爐1oo聽// " );
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽System.out.println(
" 聽涓綰х紦瀛樻竻絀哄墠聽 " + 聽session.get(T1oo. class ,t1oo.getId())聽);
聽聽聽聽聽聽聽聽session.evict(t1oo);
聽聽聽聽聽聽聽聽System.out.println(
" 聽涓綰х紦瀛樻竻絀哄悗聽 " + 聽session.get(T1oo. class ,t1oo.getId())聽);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
// 涓嶆妸t1oo聽id聽涓虹┖錛屽惁鍒欏綋鍦╯aveOrUpdate鏃跺欏氨浼?xì)浠ヤ负鏄父绂绘伮爑pdate浜?/span>
聽聽聽聽聽聽聽聽t1oo.setId( null );
聽聽聽聽聽聽聽聽
// id=null聽insert聽璋冪敤
聽聽聽聽聽聽聽聽session.saveOrUpdate(t1oo);聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽tr2.commit();
聽聽聽聽聽聽聽聽
聽聽聽聽Transaction聽tr3聽
= 聽session.beginTransaction();
聽聽聽聽聽聽聽聽session.delete(t1oo);
聽聽聽聽聽聽聽聽tr3.commit();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽session.close();
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽聽聽聽聽
聽聽聽聽}
緇撴灉鏄細(xì)
********************ExecuteUpdate************************
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.

Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: delete from t1oo
//err: 鏈夌駭鑱?鍗曚嬌鐢?delete T1oo 榪樿delete T2oo.aid = T1oo.id//
Hibernate: select t1oo0_.id as id, t1oo0_.name as name0_ from t1oo t1oo0_
Hibernate: delete from t2oo where aid=?
Hibernate: delete from t2oo where aid=?
Hibernate: delete from t2oo where aid=?
Hibernate: delete from t1oo
Hibernate: update t1oo set name=? where id=?
//err: update(t1oo)->DB 涓暟鎹簱涓病鏈?t1oo //
聽涓綰х紦瀛樻竻絀哄墠 hbn.bean.T1oo@287
Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?
聽涓綰х紦瀛樻竻絀哄悗 null
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: delete from t1oo where id=?


Get Load 鍖哄埆 錛?br />
聽聽聽聽/**聽Get聽Load聽鍖哄埆聽(鍦╟ommit鍓嶈聽session.flush())
聽聽聽聽聽*聽Table聽->聽T1oo(id,name)
聽聽聽聽聽*聽1.濡傛灉鏈兘鍙戠幇絎﹀悎鏉′歡鐨勮褰曪紝get鏂規(guī)硶榪斿洖null錛岃宭oad鏂規(guī)硶浼?xì)鎶涘嚭寮傚?br />聽聽聽聽聽*聽2.Load鏂規(guī)硶鍙繑鍥炲疄浣撶殑浠g悊綾誨疄渚嬶紝鑰実et鏂規(guī)硶姘歌繙鐩存帴榪斿洖瀹炰綋綾匯?br />聽聽聽聽聽*聽3.load鏂規(guī)硶鍙互鍏呭垎鍒╃敤鍐呴儴緙撳瓨鍜屼簩綰х紦瀛樹腑鐨勭幇鏈夋暟鎹紝鑰実et鏂規(guī)硶鍒欎粎浠呭湪鍐呴儴緙撳瓨涓繘琛屾暟鎹煡鎵撅紝
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽聽濡傛病鏈夊彂鐜板搴旀暟鎹紝灝嗚秺榪囦簩綰х紦瀛橈紝鐩存帴璋冪敤SQL瀹屾垚鏁版嵁璇誨彇銆偮?br />聽聽聽聽聽
*/
聽聽聽聽
publicvoid聽testGetLoad()聽throws聽Exception聽{
聽聽聽聽聽聽聽聽System.out.println(
"\r\n\r\n********************Get<>Load************************");
聽聽聽聽聽聽聽聽Session聽session聽
=聽HibernateSessionFactory.currentSession();
聽聽聽聽
//聽聽鏁版嵁鍑嗗
聽聽聽聽聽聽聽聽T1oo聽t1oo聽=new聽T1oo();
聽聽聽聽聽聽聽聽t1oo.setName(
"liu");
聽聽聽聽Transaction聽t1聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽session.saveOrUpdate(t1oo);
聽聽聽聽聽聽聽聽
//聽涓轟粈涔堣繖浼?xì)閿欏Q?br />聽聽聽聽聽聽聽聽//session.evict(t1oo);
聽聽聽聽聽聽聽聽
//session.flush();
聽聽聽聽聽聽聽聽t1.commit();
聽聽聽聽聽聽聽聽session.evict(t1oo);
聽聽聽聽
聽聽聽聽Transaction聽t2聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽System.out.println(
"涓綰х紦瀛樻槸鍚︽湁t1oo(load)->"+session.contains(t1oo));聽
聽聽聽聽聽聽聽聽
//榪欐椂鍊檛1oo涓郝燙Glib鐢熸垚鐨勪唬鐞嗙被
聽聽聽聽聽聽聽聽t1oo聽=聽(T1oo)session.load(T1oo.class,t1oo.getId());
聽聽聽聽聽聽聽聽System.out.println(
"聽寤惰繜鍔犺澆鍑虹幇錛歴elect..聽t1oo0_.id=?聽琛ㄧ殑鍏朵粬灞炴у姞杞鉸?/span>");
聽聽聽聽聽聽聽聽t1oo.setName(
"load聽list");
聽聽聽聽聽聽聽聽
//鍚庡湪聽update
聽聽聽聽聽聽聽聽t2.commit();
聽聽聽聽聽聽聽聽session.evict(t1oo);
聽聽聽聽聽聽聽聽
聽聽聽聽Transaction聽t3聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽System.out.println(
"涓綰х紦瀛樻槸鍚︽湁t1oo(get)->"+session.contains(t1oo));聽
聽聽聽聽聽聽聽聽
//榪欐椂鍊檛1oo涓郝燙Glib鐢熸垚鐨勪唬鐞嗙被
聽聽聽聽聽聽聽聽t1oo聽=聽(T1oo)session.get(T1oo.class,t1oo.getId());
聽聽聽聽聽聽聽聽System.out.println(
"聽娌℃湁寤惰繜鍔犺澆鍑虹幇");
聽聽聽聽聽聽聽聽t1oo.setName(
"get聽list");
聽聽聽聽聽聽聽聽
//鍚庡湪聽update
聽聽聽聽聽聽聽聽t3.commit();聽聽聽聽
聽聽聽聽聽聽聽聽session.evict(t1oo);
聽聽聽聽聽聽聽聽
聽聽聽聽Transaction聽tr3聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽session.delete(t1oo);
聽聽聽聽聽聽聽聽tr3.commit();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽session.close();
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽聽聽聽聽
聽聽聽聽}
緇撴灉
********************Get<>Load************************
Hibernate: insert into t1oo (name, id) values (?, ?)
涓綰х紦瀛樻槸鍚︽湁t1oo(load)->false
聽寤惰繜鍔犺澆鍑虹幇錛歴elect.. t1oo0_.id=? 琛ㄧ殑鍏朵粬灞炴у姞杞?
Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?
Hibernate: update t1oo set name=? where id=?
涓綰х紦瀛樻槸鍚︽湁t1oo(get)->false
Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?
聽娌℃湁寤惰繜鍔犺澆鍑虹幇
Hibernate: update t1oo set name=? where id=?
Hibernate: select t2ooset0_.aid as aid1_, t2ooset0_.id as id1_, t2ooset0_.id as id0_, t2ooset0_.avg as avg1_0_, t2ooset0_.aid as aid1_0_ from t2oo t2ooset0_ where t2ooset0_.aid=?
Hibernate: delete from t1oo where id=?


Set 闆嗗悎鐨勮瘑鍒?/font>錛?br />
聽聽聽聽/**聽Set聽闆嗗悎鐨勮瘑鍒?br />聽聽聽聽聽*聽Table聽->聽T1oo(id,name)
聽聽聽聽聽*聽Table聽->聽T2oo(id,avg,aid)
聽聽聽聽聽*聽澶栭敭聽T1oo.id->T2oo.aid
聽聽聽聽聽*聽T1oo聽聽<set聽name="t2ooSet"聽inverse="false"聽cascade聽=聽"all"聽聽>
聽聽聽聽聽*聽T2oo聽聽<many-to-one聽name="t1oo"聽column="aid"聽class="T1oo"聽/>
聽聽聽聽聽*聽cascade='insert'聽鏄竴瀹氳鐨?br />聽聽聽聽聽*聽聽聽聽聽褰撀燭1oo娌℃湁聽inverse="true"聽涓誨姩鏉冪殑鏃跺欙紝瑕佸弻欏瑰叧鑱?br />聽聽聽聽聽*聽聽聽聽聽t1oo.getT2ooSet().add(t2oo1);
聽聽聽聽聽*聽聽聽聽t1oo.getT2ooSet().add(t2oo2);
聽聽聽聽聽*聽聽聽聽t2oo1.setT1oo(t1oo);
聽聽聽聽聽*聽聽聽聽t2oo2.setT1oo(t1oo);
聽聽聽聽聽*聽聽瑕佷笉鍚巗ql涓猴細(xì)
聽聽聽聽聽*聽聽Hibernate:聽insert聽into聽t2oo聽(avg,聽aid,聽id)聽values聽(?,聽?,聽?)
聽聽聽聽聽*聽聽聽聽Hibernate:聽insert聽into聽t2oo聽(avg,聽aid,聽id)聽values聽(?,聽?,聽?)
聽聽聽聽聽*聽聽聽聽+----+-----+------+
聽聽聽聽聽*聽聽聽聽|聽id聽|聽avg聽|聽aid聽聽|
聽聽聽聽聽*聽聽聽聽+----+-----+------+
聽聽聽聽聽*聽聽聽聽|聽聽1聽|聽聽24聽|聽NULL聽|
聽聽聽聽聽*聽聽聽聽|聽聽2聽|聽聽23聽|聽NULL聽|
聽聽聽聽聽*聽聽聽聽+----+-----+------+
聽聽聽聽聽*聽聽聽聽褰撀燭1oo鏈壜爄nverse="false"涓誨姩鏉冪殑鏃跺?br />聽聽聽聽聽*聽聽t1oo.getT2ooSet().add(t2oo1);
聽聽聽聽聽*聽聽聽聽t1oo.getT2ooSet().add(t2oo2);
聽聽聽聽聽*聽聽Sql璇彞涓猴細(xì)
聽聽聽聽聽*聽聽Hibernate:聽insert聽into聽t1oo聽(name,聽id)聽values聽(?,聽?)
聽聽聽聽聽*聽聽聽聽Hibernate:聽insert聽into聽t2oo聽(avg,聽aid,聽id)聽values聽(?,聽?,聽?)
聽聽聽聽聽*聽聽聽聽Hibernate:聽insert聽into聽t2oo聽(avg,聽aid,聽id)聽values聽(?,聽?,聽?)
聽聽聽聽聽*聽聽聽聽Hibernate:聽update聽t2oo聽set聽aid=?聽where聽id=?
聽聽聽聽聽*聽聽聽聽Hibernate:聽update聽t2oo聽set聽aid=?聽where聽id=?
聽聽聽聽聽*聽聽聽聽|聽聽3聽|聽聽24聽|聽聽聽12聽|
聽聽聽聽聽*聽聽聽聽|聽聽4聽|聽聽23聽|聽聽聽12聽|
聽聽聽聽聽*聽聽聽聽+----+-----+------+
聽聽聽聽
*/
聽聽聽聽
publicvoid聽testSet(){
聽聽聽聽聽聽聽聽System.out.println(
"\r\n\r\n********************Set************************");
聽聽聽聽聽聽聽聽T1oo聽t1oo聽
=new聽T1oo();
聽聽聽聽聽聽聽聽t1oo.setName(
"list");
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽T2oo聽t2oo1聽
=new聽T2oo();聽t2oo1.setAvg(new聽I(yíng)nteger(23));
聽聽聽聽聽聽聽聽T2oo聽t2oo2聽
=new聽T2oo();聽t2oo2.setAvg(new聽I(yíng)nteger(24));
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Session聽session聽
=聽HibernateSessionFactory.currentSession();
聽聽聽聽聽聽聽聽Transaction聽tr1聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽session.save(t1oo);
聽聽聽聽聽聽聽聽t1oo聽
=聽(T1oo)聽session.load(T1oo.class,t1oo.getId());

聽聽聽聽聽聽聽聽t1oo.setT2ooSet(
new聽HashSet())聽;
聽聽聽聽聽聽聽聽t1oo.getT2ooSet().add(t2oo1);
聽聽聽聽聽聽聽聽t1oo.getT2ooSet().add(t2oo2);
聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽System.out.println(聽t1oo.getT2ooSet().size()
+""聽);聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽tr1.commit();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽System.out.println();
聽聽聽聽聽聽聽聽T2oo聽t2oo3聽
=new聽T2oo();聽t2oo3.setAvg(new聽I(yíng)nteger(25));
聽聽聽聽聽聽聽聽T1oo聽t1oo2聽
=new聽T1oo();聽t1oo2.setName("mz");
聽聽聽聽聽聽聽聽t2oo3.setT1oo(t1oo2);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Transaction聽tr2聽
=聽session.beginTransaction();
聽聽聽聽聽聽聽聽session.save(t2oo3);
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽tr2.commit();聽聽聽聽
聽聽聽聽聽聽聽聽}聽
catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(
"//err:聽娌℃湁涓誨姩鏉兟燾ascade聽=聽'all'聽涓嶅彲浠ョ駭鑱攕ave聽t1oo聽聽聽//");
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽session.close();
聽聽聽聽聽聽聽聽HibernateSessionFactory.closeSession();
聽聽聽聽}
聽聽聽

緇撴灉鏄細(xì)
********************Set************************
2
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (avg, aid, id) values (?, ?, ?)
Hibernate: insert into t2oo (avg, aid, id) values (?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
Hibernate: update t2oo set aid=? where id=?

//err: 娌℃湁涓誨姩鏉?cascade = 'all' 涓嶅彲浠ョ駭鑱攕ave t1oo聽聽 //




G_G 2007-11-12 16:29 鍙戣〃璇勮
]]>婧愮爜鍒嗘瀽http://www.tkk7.com/Good-Game/archive/2007/10/23/155210.htmlG_GG_GTue, 23 Oct 2007 02:34:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/10/23/155210.htmlhttp://www.tkk7.com/Good-Game/comments/155210.htmlhttp://www.tkk7.com/Good-Game/archive/2007/10/23/155210.html#Feedback0http://www.tkk7.com/Good-Game/comments/commentRss/155210.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/155210.html
publicfinalclass聽CollectionHelper聽{
聽聽聽聽
publicstaticfinal聽List聽EMPTY_LIST聽=聽Collections.unmodifiableList(聽new聽ArrayList(0)聽);
聽聽聽聽
publicstaticfinal聽Collection聽EMPTY_COLLECTION聽=聽Collections.unmodifiableCollection(聽new聽ArrayList(0)聽);
聽聽聽聽
publicstaticfinal聽Map聽EMPTY_MAP聽=聽Collections.unmodifiableMap(聽new聽HashMap(0)聽);
聽聽聽聽
private聽CollectionHelper()聽{}
}
聽聽聽 1.1 鍦–ollections.unmodifiableList(...)鏄潤(rùn)鎬佸唴閮ㄧ被涓瀯閫犳柟娉?
聽聽聽 聽聽 鎮(zhèn)燂細(xì) 浠庝笂闈㈢湅鍑烘槸涓涓潪甯稿ソ鐨劼?閫傞厤鍣?聽
聽聽聽//1.Collections 涓?new 鍑哄唴閮ㄧ被
聽聽聽 public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
聽聽聽 聽聽聽 return new UnmodifiableCollection<T>(c);
聽聽聽 }



聽聽聽 //2.
鍐呴儴綾?/font>涔熺戶鎵? Collection
聽聽聽 static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
聽聽聽 privatestaticfinallong聽serialVersionUID聽=1820017752578914078L;

聽聽聽聽
final聽Collection<?extends聽E>聽c;
聽聽聽 //3.寰堝ソ閫傞厤 Collection 閫氳繃浠栨妸 add remove 絳夊姛鑳?灝佽
聽聽聽聽UnmodifiableCollection(Collection
<?extends聽E>聽c)聽{
聽聽聽聽聽聽聽聽聽聽聽聽
if聽(c==null)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
thrownew聽NullPointerException();
聽聽聽聽聽聽聽聽聽聽聽聽
this.c聽=聽c;
聽聽聽聽聽聽聽聽}
聽聽聽 ...............

2.澶ч噺浣跨敤鍐呴儴綾繪灇鐭??濡傦細(xì)
Mappings.PropertyReference聽upr聽=聽(Mappings.PropertyReference)聽iter.next();
////////////////////////////////////////////////
Mappings涓?br />聽聽聽聽staticfinalclass聽PropertyReference聽implements聽Serializable聽{
聽聽聽聽聽聽聽聽String聽referencedClass;
聽聽聽聽聽聽聽聽String聽propertyName;
聽聽聽聽聽聽聽聽
boolean聽unique;
聽聽聽聽}

//鎰熻鏄彲浠ユ洿濂界殑浠g爜緙栧啓






G_G 2007-10-23 10:34 鍙戣〃璇勮
]]>
娌℃湁鐜╄漿鐨?hibernate hql 媯(gè)绱?/title><link>http://www.tkk7.com/Good-Game/archive/2007/08/24/138889.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Fri, 24 Aug 2007 08:36:00 GMT</pubDate><guid>http://www.tkk7.com/Good-Game/archive/2007/08/24/138889.html</guid><wfw:comment>http://www.tkk7.com/Good-Game/comments/138889.html</wfw:comment><comments>http://www.tkk7.com/Good-Game/archive/2007/08/24/138889.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/Good-Game/comments/commentRss/138889.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/Good-Game/services/trackbacks/138889.html</trackback:ping><description><![CDATA[聽聽聽 1銆傛枃浠剁殑鏄犲皠銆傜敱浜庝嬌鐢?MyEclipse錛堝偦鐡滅増^_^錛?鎵浠?hibernate宸ュ叿錛坋xtensions錛孧iddlegen錛宼ools絳夛級(jí)涓鐩存病濂藉ソ鐮旂┒榪欏氨涓嶇尞涓戜簡(jiǎn)銆?br />聽聽聽 2銆侰riteria <br />聽聽聽 聽聽 1錛夋煡璇㈡潯浠墮氳繃 Criteria.add 娣誨姞 Expression 鐢ㄤ簬鎻忚堪鏉′歡 <br />聽聽聽 聽聽聽 聽聽聽 聽聽聽 Expression.( and or like in le lt ..... )<br />聽聽聽 聽聽 聽聽 聽聽 聽聽 鍙傝錛?<a accesskey="n" >Criteria鏌ヨ</a><br />聽聽聽 聽聽 2錛塩riteria.setFirstResult(100);<br />聽聽聽 聽聽聽 聽 criteria.setMaxResults(20); <font color="#008000">//媯(gè)绱㈣寖鍥?br />聽聽聽 聽聽聽 聽 <font color="#000000">criteria.addOrder(Order.asc("name")); <font color="#006400">//鎺掑簭</font></font></font><br />聽聽聽 聽聽聽 聽 .add( Expression.sql("lower($alias.name) like lower(?)", "Fritz%", Hibernate.STRING)聽聽 <font color="#006400">//鐩存帴宓屽叆SQL<br />聽聽聽 聽聽 <font color="#000000">3錛?/font></font>Cat cat = new Cat();<br />聽聽聽 聽聽 聽聽 ....<br /><br />聽聽聽 聽聽聽聽 List results = session.createCriteria(Cat.class).add( Example.create(cat) ).list(); <font color="#006400">//鏍規(guī)嵁瀵硅薄鏌ヨ</font><br />聽聽聽 聽 <br />聽聽聽 3銆侶QL 瀹屽叏闈㈠悜瀵硅薄鐨勶紝鍏峰緇ф壙銆佸鎬佸拰鍏寵仈絳夌壒鎬с?br />聽聽聽 聽聽 1錛夊弬瑙侊細(xì) <a accesskey="n" >HQL</a><br />聽聽聽 聽聽 2錛夊唴榪炴帴錛宨nner join<tt class="literal"><br />聽聽聽 聽聽聽聽聽 宸﹀榪炴帴錛宭eft outer join</tt><tt class="literal"><br />聽聽聽 聽聽聽聽聽 鍙沖榪炴帴錛宺ight outer join</tt>聽 <br /><font color="#006400">聽聽聽 聽聽聽 聽 http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html<br />聽聽聽 聽聽 聽聽 浠h〃鎬х殑璇彞</font>聽聽聽 聽<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><span style="color: rgb(0, 0, 0);"><font color="#0000ff">Hql.hbm.xml</font><br />聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">set聽</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="fkSet"</span><span style="color: rgb(255, 0, 0);">聽inverse</span><span style="color: rgb(0, 0, 255);">="true"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">key聽</span><span style="color: rgb(255, 0, 0);">column</span><span style="color: rgb(0, 0, 255);">="id"</span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">one-to-many聽</span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="Fk"</span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">set</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"><br /><font color="#0000ff">SQL <br /></font></span><span style="color: rgb(0, 0, 0);"><font color="#ffa500"><font color="#800080">alter聽table聽fk聽add聽constraint聽聽FK_hf聽foreign聽key(id)聽references聽hql(id)</font></font></span><span style="color: rgb(0, 0, 0);"><font color="#0000ff"><br /><br /></font></span><span style="color: rgb(0, 0, 0);"><font color="#ffa500"><font color="#800080">mysql> select * from hql;</font><br /> +----+----------+<br /> | id | name聽聽聽聽 |<br /> +----+----------+<br /> |聽 0 | liukaiyi |<br /> |聽 1 | heha聽聽聽聽 |<br /> +----+----------+<br /> 2 rows in set (0.00 sec)<br /><br /><font color="#800080"> mysql> select * from fk;</font><br /> +----+------+<br /> | id | name |<br /> +----+------+<br /> |聽 0 | yy聽聽 |<br /> |聽 1 | xx聽聽 |<br /> +----+------+<br /> 2 rows in set (0.00 sec)</font></span><span style="color: rgb(0, 0, 0);"><font color="#ffa500"><br /></font></span></div><br /><font color="#006400">//綆鍗曠殑 緇撴灉錛?2聽 liukaiyi:heha<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽Query聽qu聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select聽h.name聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽from聽Hql聽h聽,聽Fk聽f</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽where聽h.id聽=聽f.id</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽);<br />聽聽聽聽聽聽聽聽List聽list聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽聽qu.list()聽;<br />聽聽聽聽聽聽聽聽tr.commit();<br />聽聽聽聽聽聽聽聽System.out.println(聽list.size()聽);<br />聽聽聽聽聽聽聽聽System.out.println(聽list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">list.get(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)聽);</span></div><br /></font><br /><font color="#006400">//緇欏嚭瀵硅薄 緇撴灉 : heha</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Query聽qu聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">from聽Hql聽h聽where聽h.fkSet.name='xx'</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />((Hql)list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)).getName()聽;<br /><br /></span></div><br /><font color="#006400">//緇欏嚭Object 緇撴灉錛歨eha xx</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Query聽qu聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><font color="#0000ff"><span style="color: rgb(0, 0, 0);">select聽h.name,f.name聽from聽Hql聽h聽join聽h.fkSet聽f聽where聽f.name='xx'</span></font><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />List聽list聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽聽qu.list()聽;<br />tr.commit();<br />System.out.println(list.size());聽聽聽聽<br />Object[]聽objs聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽(Object[])聽list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)聽;<br />System.out.println(聽objs[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]聽</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">objs[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]聽); <br /></span></div><br /><font color="#006400">//緇欏嚭 Map聽 heha xx<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽<br />聽聽聽聽聽聽聽聽Query聽qu聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select聽new聽map(聽h.name聽as聽hn,f.name聽as聽fn)from聽Hql聽h聽join聽h.fkSet聽f聽where聽f.name='xx'</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />聽聽聽聽聽聽聽聽List聽list聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽聽qu.list()聽;<br /><br />聽聽聽聽聽聽聽聽tr.commit();<br />聽聽聽聽聽聽聽聽System.out.println(list.size());聽聽聽聽<br />聽聽聽聽聽聽聽聽Map聽map聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽((Map)list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">));<br />聽聽聽聽聽聽聽聽System.out.println(聽map.get(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">hn</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)聽);<br />聽聽聽聽}</span></div><br />//鎶ヨ〃璇彞聽 緇撴灉: 1</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">聽聽聽聽聽聽聽聽Query聽qu聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select聽count(*)聽from聽Hql聽h聽join聽h.fkSet聽f聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽where聽h.name='heha'聽and聽h.id=f.id聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽group聽by聽h.name聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />聽聽聽聽聽聽聽聽List聽list聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽聽qu.list()聽;<br />聽聽聽聽聽聽聽聽tr.commit();<br />聽聽聽聽聽聽聽聽System.out.println(聽list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)聽);</span></div><font color="#006400"><br />//闆嗗悎鍐?緇撴灉 liukaiyi:heha <br /><font color="#008000">//</font></font><font color="#008000">鍏冪礌闆?<tt class="literal">elements</tt>涓?tt class="literal">indices</tt> 鍑芥暟) <tt class="literal">鍙互浣跨敤 any, some, all, exists, in<br /><font color="#006400">//</font></tt></font><font color="#006400">EG:聽 from Player p where 3 > all elements(p.scores)</font><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);"><font color="#ffa500"> +----+----------+<br />| id | name聽聽聽聽 |<br />+----+----------+<br />|聽 0 | liukaiyi |<br />|聽 1 | heha聽聽聽聽 |<br />|聽 3 | oo聽聽聽聽聽聽 |<br />+----+----------+</font><br />聽聽 聽聽聽聽 <br />聽聽聽 聽聽聽 Query聽qu聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽se.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select聽h.name聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽from聽Hql聽h</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽where聽h.id聽in聽elements(h.fkSet.id)聽</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">聽);<br />聽聽聽聽聽聽聽聽List聽list聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽聽qu.list()聽;<br />聽聽聽聽聽聽聽聽tr.commit();<br />聽聽聽聽聽聽聽聽System.out.println(聽list.get(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">list.get(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)聽);</span></div><br /><br /><font color="#006400">//浣跨敤 javaBean 緇戝畾 <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Query聽q聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽s.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">from聽foo聽Foo聽as聽foo聽where聽foo.name=:name聽and聽foo.size=:size</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />q.setProperties(fooBean);聽</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">聽fooBean鍖呭惈鏂規(guī)硶getName()涓巊etSize()</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">List聽foos聽</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">聽q.list();</span></div><br /></font><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img src ="http://www.tkk7.com/Good-Game/aggbug/138889.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/Good-Game/" target="_blank">G_G</a> 2007-08-24 16:36 <a href="http://www.tkk7.com/Good-Game/archive/2007/08/24/138889.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>hbn 絎旇http://www.tkk7.com/Good-Game/archive/2007/08/09/128154.htmlG_GG_GThu, 09 Aug 2007 06:30:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/08/09/128154.htmlhttp://www.tkk7.com/Good-Game/comments/128154.htmlhttp://www.tkk7.com/Good-Game/archive/2007/08/09/128154.html#Feedback3http://www.tkk7.com/Good-Game/comments/commentRss/128154.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/128154.html
1.Criteria 鐨?or聽

聽聽聽聽 public 聽List聽getFind(String[]聽dpids聽) {
聽聽聽聽聽聽聽聽Criteria聽cr聽
= 聽聽getCriteriaBegin(Deptid. class );
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽cr.add(聽Restrictions.or(聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Restrictions.eq(
" dpid " ,聽dpids[ 0 ]聽)聽,聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Restrictions.eq(聽
" dpid " 聽,dpids[ 1 ]聽)聽)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
return 聽getCriteriaEnd(cr);
聽聽聽聽}

2. 瀹瑰櫒鍌ㄥ瓨 錛堜粠琛ㄦ病涓婚敭錛?/font> 1)Set聽 鐨?.xml
聽聽聽聽 聽聽聽 聽聽聽 寮曠敤聽 http://caterpillar.onlyfun.net/GossipCN/HibernateGossip/HibernateGossip.html
	<set name="emails" table="email">//email琛?/font> 灝?2鍒?id address
<key column="id"/> //email琛ㄧ殑涓婚敭 涔熸槸涓昏〃鐨勪富閿?/font>
<element type="java.lang.String" column="address"/>
</set>
.java private Set emails; //瀛樺拰澶栭敭鍚?/font>
2)List聽 鐨?.xml
<list name="items" table="item">
<key column="id"/> //涓?/font>item琛ㄧ殑 idINT(11) NOT NULL,
<index column="position"/> //涓鴻〃鐨?position INT(11) NOT NULL,
<element type="java.lang.String" column="name"/>//涓鴻〃鐨?/font>name VARCHAR(100) NOT NULL default ''
</list>
銆傘傘傘傘?br />3. 鍒椾負(fù)瀵硅薄
<set name="emails" table="email">
<key column="id"/>
<composite-element class="onlyfun.caterpillar.MailAddress">
<property name="address" column="address"/> //璋冪敤 getAddress()
</composite-element>
</set>




G_G 2007-08-09 14:30 鍙戣〃璇勮
]]>
hibernate 鍖呰http://www.tkk7.com/Good-Game/archive/2007/07/04/128143.htmlG_GG_GWed, 04 Jul 2007 08:49:00 GMThttp://www.tkk7.com/Good-Game/archive/2007/07/04/128143.htmlhttp://www.tkk7.com/Good-Game/comments/128143.htmlhttp://www.tkk7.com/Good-Game/archive/2007/07/04/128143.html#Feedback5http://www.tkk7.com/Good-Game/comments/commentRss/128143.htmlhttp://www.tkk7.com/Good-Game/services/trackbacks/128143.html 07-11-9
鐢變簬鎴戝hibernate鐞嗚В鐨勬繁鍏ワ紝鍦ㄦ鎴戞潵緇堢粨涓嬭繖綃囨枃绔犵殑涓嶈凍鐨勫湴鏂癸紙濂藉浜哄湪鎷嶇爾鍝堬級(jí)
瀵瑰鏉傜殑閫昏緫榪欐牱寮鍜屽叧澶啋澶變簡(jiǎn)銆?br />聽 鍦ㄦ櫘閫氱殑閫昏緫鏂規(guī)硶涓?session閮藉彔鍔犵殑鐢ㄤ笂2嬈′互涓?榪欏氨瑕佸涓綰х紦鍐茬殑綆$悊銆備簨鐗╃殑浣跨敤涔熸槸涓鏍楓?br />


涓嬭澆:

http://www.tkk7.com/Files/Good-Game/hibernateUtil.rar

綆鍗曠殑緇ф壙鍖?:聽
聽聽聽聽聽聽緇ф壙 DBManage 綾宦?鎶娐犳暟鎹寘瑁呯被 綆$悊 (聽濉厖 ,聽set 絳?) 鍚庡氨鐩存帴鏀懼埌閲?insert , update , delete 鏂規(guī)硶涓?br />聽聽聽聽聽聽聽聽 濉厖鍙湅鐪嬭繖涓?寰堟湁瀹炵敤浠峰?----> (聽褰?Form -> Bean聽 鎴柭燘ean ->Bean 聽) 渚嬪瓙寰堢畝鍗曚絾鎯蟲硶琛ㄦ槑聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽緇ф壙 DBSelect 綾?涓?select聽 榪欏彧鏄?鎶?Query 緇欏嚭 綆鍗曠敤娉?鏄?br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Query聽qu =聽聽聽dao.getQueryBegin(聽 "聽HQL聽"聽);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽qu.add .....
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽List li = dao.getQuertyEnd(聽qu聽);
聽聽聽聽聽鎵╁睍:聽 Criteria聽 cr , SQLQuery , 絳夌瓑 鎶婂ス鍖呰鎴?鍚勪釜 dao 涓?涓嶆槸 寰?鍋鋒噿聽聽聽聽 : )


Eg :聽聽聽 聽extendsDBBasic 綾?/font>
聽聽聽聽public聽List聽select(String聽id,String聽time){

聽聽聽聽聽聽聽聽List聽list聽
=new聽ArrayList();
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 Query qu =
getQueryBegin("HSQL");聽聽 //緇ф壙甯︽潵鐨勫ソ澶?br />聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 .add..........

聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽 List list = getQuertyEnd(qu);
聽聽 //緇ф壙緇撴潫
聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽
return聽聽list;
聽聽聽聽}

緙虹偣: 鏈濂戒笉瑕乻tatic聽鍦ㄤ竴鏃墮棿鐐逛笂 姝ょ被涓嶈兘鏀寔澶氱嚎紼嬄?br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽basicBegin聽 basicEnd 聽鏂規(guī)硶 (聽鎴戞壘涓嶅埌濂界殑鏂規(guī)硶 瑕佺殑璇濇潵涓?榪炴帴姹犱粈涔堢殑 澶у璁ㄨ瑗胯タ鍚?!聽聽)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 sess 涓哄叏灞鐨?session 瑕佹槸 鍐欐垚聽baseicBegin 榪斿洖鐨勮瘽 鎰熻涓嬮潰鐨勮繕鏈夊緢澶氳鍐?浠涔?close 鍟?..
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽sess 涔熸槸緙虹偣鐨勬墍鍦?褰撶劧澶у 鏈変粈涔堟兂娉?鏀逛笅 鎴栬嚜宸辨潵涓?灝忓寘瑁?鎴戝彧鏄?聽聽鎶涚爾寮曠帀
聽聽聽
聽聽聽聽private聽Map聽transmap聽=new聽HashMap();

聽聽聽聽
protected聽Session聽basicBegin(){
聽聽聽聽聽聽聽聽Session聽sess聽
=聽SessionManager.currentSession();
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽transmap.put(聽sess,聽sess.beginTransaction()聽);
聽聽聽聽聽聽聽聽}
catch(Exception聽e){System.out.println(e);}
聽聽聽聽聽聽聽聽
return聽sess聽;
聽聽聽聽}
聽聽聽聽
聽聽聽聽
protectedvoid聽basicEnd(Session聽sess){
聽聽聽聽聽聽聽聽
try聽{
聽聽聽聽聽聽聽聽聽聽聽聽((Transaction)transmap.remove(sess)).commit();
聽聽聽聽聽聽聽聽}
catch(Exception聽e){System.out.println(e);}
聽聽聽聽聽聽聽聽
finally{
聽聽聽聽聽聽聽聽聽聽聽聽sess聽
=null聽;
聽聽聽聽聽聽聽聽聽聽聽聽SessionManager.closeSession();
聽聽聽聽聽聽聽聽}
聽聽聽聽}

蹇?jī)寰? 澶氬寘瑁呭蹇箰

G_G 2007-07-04 16:49 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 久久久久亚洲av成人无码电影| 国产成人va亚洲电影| 青青视频观看免费99| 亚洲手机中文字幕| 成人黄色免费网站| 亚洲成人一级电影| 曰曰鲁夜夜免费播放视频| 久久久久亚洲精品无码蜜桃 | 亚洲xxxx视频| 最近中文字幕无吗免费高清| 亚洲最大的成人网| 日本黄页网站免费| 羞羞漫画登录页面免费| 亚洲精品偷拍视频免费观看| 一个人免费观看视频在线中文| 伊人亚洲综合青草青草久热| 中国毛片免费观看| 亚洲av无码国产精品色午夜字幕 | 国产精品怡红院永久免费| 亚洲国产精品线观看不卡| 日韩精品福利片午夜免费观着| 亚洲精品宾馆在线精品酒店 | 九九视频高清视频免费观看| 久久精品国产亚洲Aⅴ蜜臀色欲| 中文字幕在线成人免费看| 亚洲av中文无码乱人伦在线播放| 四虎影视在线影院在线观看免费视频 | 无套内射无矿码免费看黄| 国产日产亚洲系列最新| 免费黄网站在线观看| 亚洲网站在线播放| 亚洲欧洲免费无码| 色九月亚洲综合网| 永久亚洲成a人片777777| 特级无码毛片免费视频尤物| 亚洲成a人片在线看| 国产色婷婷精品免费视频| 国产99久久久久久免费看| 亚洲一区二区在线免费观看| 中文字幕无码视频手机免费看| 亚洲AV无码一区二区三区电影|