??xml version="1.0" encoding="utf-8" standalone="yes"?>日韩综合一区二区,香蕉久久a毛片,羞羞答答一区二区http://www.aygfsteel.com/Good-Game/category/23018.htmlzh-cnThu, 29 May 2008 08:57:08 GMTThu, 29 May 2008 08:57:08 GMT60数据库表地址数据Q中国地区)http://www.aygfsteel.com/Good-Game/archive/2008/05/29/203747.htmlG_GG_GThu, 29 May 2008 02:51:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2008/05/29/203747.htmlhttp://www.aygfsteel.com/Good-Game/comments/203747.htmlhttp://www.aygfsteel.com/Good-Game/archive/2008/05/29/203747.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/203747.htmlhttp://www.aygfsteel.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.aygfsteel.com/Good-Game/archive/2008/05/09/199477.htmlG_GG_GFri, 09 May 2008 05:47:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2008/05/09/199477.htmlhttp://www.aygfsteel.com/Good-Game/comments/199477.htmlhttp://www.aygfsteel.com/Good-Game/archive/2008/05/09/199477.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/199477.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/199477.htmlhttp://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch09.html

先从配置文g开始:
源码Q?a href="/Files/Good-Game/springAop.rar">springAop.rar

需要jar
<?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 与事务联p aopBean<->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>

    
<!-- 事务原子 具体Ҏq行什么事务?/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 c(接口Q?br />
package dao.imp;

import java.util.List;

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

import bean.UnitBean;

import dao.BeanDao;

public class BeanDaoImp extends HibernateDaoSupport implements BeanDao{
    
public void addBean(UnitBean unitBean) {
        
this.getHibernateTemplate().save(unitBean);
    }

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

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

Main c?br />
package unit;

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

import dao.BeanDao;
import bean.UnitBean;

public class Main {
    
public static void 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());
           
    }
}
l果Q?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 数据l构Q?树、多对多 l构表示 Q?/title><link>http://www.aygfsteel.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.aygfsteel.com/Good-Game/archive/2008/04/26/196293.html</guid><wfw:comment>http://www.aygfsteel.com/Good-Game/comments/196293.html</wfw:comment><comments>http://www.aygfsteel.com/Good-Game/archive/2008/04/26/196293.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Good-Game/comments/commentRss/196293.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Good-Game/services/trackbacks/196293.html</trackback:ping><description><![CDATA[     摘要: level c: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.aygfsteel.com/Good-Game/archive/2008/04/26/196293.html'>阅读全文</a><img src ="http://www.aygfsteel.com/Good-Game/aggbug/196293.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Good-Game/" target="_blank">G_G</a> 2008-04-26 18:30 <a href="http://www.aygfsteel.com/Good-Game/archive/2008/04/26/196293.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate U联d删除http://www.aygfsteel.com/Good-Game/archive/2008/04/02/190283.htmlG_GG_GWed, 02 Apr 2008 02:07:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2008/04/02/190283.htmlhttp://www.aygfsteel.com/Good-Game/comments/190283.htmlhttp://www.aygfsteel.com/Good-Game/archive/2008/04/02/190283.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/190283.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/190283.html 效果-?br />
    /**
     * d 问题 和 选项
     * 
@throws Exception
     
*/@Test
    
public void testQu() throws Exception {
        Session session 
= HibernateUtil.currentSession();
        Transaction tr 
= session.beginTransaction();
        
        
//U联d
        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)


        
        
//U联删除
        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();
    }

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

Problems c?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.aygfsteel.com/Good-Game/archive/2008/01/28/178122.htmlG_GG_GMon, 28 Jan 2008 01:53:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2008/01/28/178122.htmlhttp://www.aygfsteel.com/Good-Game/comments/178122.htmlhttp://www.aygfsteel.com/Good-Game/archive/2008/01/28/178122.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/178122.htmlhttp://www.aygfsteel.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.代码
        //插入
        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());
        InputStream in 
= lo2.getContent().getBinaryStream() ;
        
byte[] bbr = new byte[in.available()];
        in.read(bbr);
        System.out.println(
new String(bbr));
        HibernateSessionFactory.closeSession();



G_G 2008-01-28 09:53 发表评论
]]>
hbn l承关系映射_1http://www.aygfsteel.com/Good-Game/archive/2007/12/26/170536.htmlG_GG_GWed, 26 Dec 2007 03:41:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/12/26/170536.htmlhttp://www.aygfsteel.com/Good-Game/comments/170536.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/12/26/170536.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/170536.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/170536.html阅读全文

G_G 2007-12-26 11:41 发表评论
]]>
hibernate + Myeclipes 敏捷(例)http://www.aygfsteel.com/Good-Game/archive/2007/12/11/167003.htmlG_GG_GTue, 11 Dec 2007 09:51:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/12/11/167003.htmlhttp://www.aygfsteel.com/Good-Game/comments/167003.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/12/11/167003.html#Feedback1http://www.aygfsteel.com/Good-Game/comments/commentRss/167003.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/167003.html
开发过E描qͼ

1.使用 MyEclipes -> uml  创徏cd
2.?Generate java Code Ҏcd生成 java文g
3.使用 Xdoclet d Hbn 标签
4.配置myEclipes -> XDoclet 自动生成 mapping.hbn.xml
5.使用myEclipes 把项目{化成 hibernate  目
6.使用 org.hibernate.tool.hbm2ddl.SchemaExport

开发过E好处:

1Q完全是面向对象Q不需要写xml配置文g(XDoclet);
2)目后期修改Ҏ面对uml
3Q用myEclipes q些都不用去找,直接拿来?uml,XDoclet,hibernate ..)

下面来?例?
1.MyEclipes 使用 uml 参?>MyEclipse 5.5 UML 入门视频 (作者:BeanSoft)


2.由uml生成cL?


3.先?eclipes?快键Ҏ?get/set ҎQ?cL件文件添?hbn XDoclet的注?br />
package bean;

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

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

}

4.用myEclipes 生成 XDoclet
在项目点右键-> properties -> MyEclipse-XDoclet ->
在Configuration I白初点右键 ?add standard -> ... hbn 后面不太好描q?可以查下很简单的 。配|好了运行后可以看?多了 ?T1oo.hbm.xml 文g;

5.myEclipes + hbn ׃多说?br />6. hbn2java:
    public void 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(
truetrue); 
    }


sqlQ?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 )


效果Q?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.aygfsteel.com/Good-Game/archive/2007/12/10/166625.htmlG_GG_GMon, 10 Dec 2007 04:52:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/12/10/166625.htmlhttp://www.aygfsteel.com/Good-Game/comments/166625.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/12/10/166625.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/166625.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/166625.html
1.单条select延迟加蝲
        Iterator it = session.createQuery("from T1oo ").iterate();
        
while(it.hasNext()){
            T1oo t1 
= (T1oo)it.next();
            t1.getName();
        }
/*q行语句 n+1
q就只加?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.U连查询Q?br />1)set排序  <set ... order-by="avg desc" ...> 从大到小
2Qbatch-size="10" 用法?
    select * from t2oo  where aid in (?,?,?....)
3)

如果惛_略gq,q有一定逻辑全部加蝲Q这有两中解军_法:
1).内连
mysql> select   *
    
-> from t1oo t1oo0_ inner join 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() );
            }
        }
l果是:
Hibernate: select t1oo0_.id as id0_, t2ooset1_.id as id1_, t1oo0_.name as name0_0_, t2ooset1_.version as version1_1_, t2ooset1_.avg as avg1_1_, t2ooset1_.aid as aid1_1_, t2ooset1_.aid as aid0__, t2ooset1_.id as id0__ from t1oo t1oo0_ inner join 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.aygfsteel.com/Good-Game/archive/2007/12/07/166061.htmlG_GG_GFri, 07 Dec 2007 07:36:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/12/07/166061.htmlhttp://www.aygfsteel.com/Good-Game/comments/166061.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/12/07/166061.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/166061.htmlhttp://www.aygfsteel.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" />

在src根目录下 ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    
<diskStore path="java.io.tmpdir"/>
    
<defaultCache
        
maxElementsInMemory="10000" //最大缓存数?/font>
        eternal
="false"Q?-- ~存是否持久 --Q?/font>
        timeToIdleSeconds
="120" Q?-- 当缓存闲|nU后销?--Q?/font>
        timeToLiveSeconds
="120"Q?-- 当缓存存znU后销?-Q?/font>
        overflowToDisk
="true"Q?-- 是否保存到磁盘,当系l当机时--Q?/font>
        diskPersistent
="false"
        diskExpiryThreadIntervalSeconds
="120"/>

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

</ehcache>

试Q?br />
    public void 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_.avg as 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_.avg as 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 多服务器数据同步问题Q支持gq同步)http://www.aygfsteel.com/Good-Game/archive/2007/12/03/164813.htmlG_GG_GMon, 03 Dec 2007 03:29:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/12/03/164813.htmlhttp://www.aygfsteel.com/Good-Game/comments/164813.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/12/03/164813.html#Feedback10http://www.aygfsteel.com/Good-Game/comments/commentRss/164813.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/164813.html
所用到知识点:
一般jdk
|-U有属性反?br />|-序列?br />|-正则表达使用
|-多线E?br />|-dom4j的xmld
|+hibernate
   |-拦截?br />   |-一个Session工厂同时q接不同数据库(本文关键Q?br />   |-oracle Blob 存取

{?.......

需求功能介l:
 为性能考虑Q单一服务器改成集(每太服务器数据允许在一定时间内保持相步Q,l出的修Ҏ间短Q不q代码持久层比较l一Qhibernate 感谢天还好是她! Q。网l连接不E_Q铁路内|!Q?br />
完成后效果:
 
当网l连接成功时Q多数据库的同步?br />   ?/font>
|络q接p|Ӟ本地应用E序q用hibernate拦截器拦截正操作对象q记录下操作动作Q序列化到本地时局?z_jcyy_tb 表中。表数据属性ؓQid,inputdate(记录旉),objectQ序列对象),action(操作动作)。ƈ安一定时间测试连接。如果成功,d z_jcyy_tb 表中数据 反序列化 再同步到 其他数据库中?br />
代码说明Q?br />1.新Session 建立
  
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()
    private static String url_b = null ;
    
private static String use_b = null ;
    
private static String pass_b = null ;
    
private static String dirver_b = null ;
    
static {try {
        //取得hibernate.cfg.xml逻辑路径Q和原来E序兌上?
        Field field   
=   SessionManager.class.getDeclaredField("CONFIG_FILE_LOCATION");
        field.setAccessible( 
true );
        String path  
=  (String) field.get(SessionManager. class );

        //通过 dom4j 加蝲 配置文g   

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

        //正则+xpathd 在hbn文g中加入的
<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的SessionFactory得到 openSession(Connection); 打开异地数据库连接?
    //利用U有反射得到 加蝲完成的SessionFactory
    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(
"--没有q接到L?openSessionb)--");
            
return null ;
        }
    }
 
2.异地数据同步p|后动作?TBDao->save() ȝ状态到数据?br />
    public void 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,p|后又成功q接?Q线E实玎ͼ TBDao->action()
    public int 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 ;
    }

    
public boolean action(){
        
int isSql = 0 ;
        ObjectConvert oc 
= new ObjectConvert();
        Session session 
= null ;
        Session session_b 
= null ;
        
        
try {
            //有失败连接动作后试 q程数据库?
            
if( (isSql=isSql())>0 ){
                session 
= SessionManager.currentSession(null,null);
                //q程数据库连接?
                //如果成功q接Qz_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();
                }
            }    
return true ;
        } 
catch (Exception e) {
            System.out.println(
"--没有q接到L?action)--");
        }
finally{
            
if(session_b!=null&&session_b.isOpen())session_b.close();
            
if(session!=null&& session.isOpen() )session.close();
            SessionManager.closeSession();    
        }
        
return false ;
    }

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;

public class TestInterceptor implements Interceptor,Serializable{
    
    
static private boolean isConn_b = false ;
    
static private TBDao tb = new TBDao();
    //U程 一分钟 连接失?同步
    
static{
        
new Thread(new Runnable(){
            
public void run() {
                
while(true){
                    isConn_b 
= tb.action();
                    
try {
                        Thread.sleep(
60*1000);
                    } 
catch (InterruptedException e) {e.printStackTrace();}
                }
            }
        }).start();
    }

    
public boolean 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();
// 拦截器中 l对不能有这?->  SessionManager.closeSession();
        }
        
return false;
    }

    
public boolean 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();
        }
        
return false;
    }

    
public void 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();
        }
    }

  ................................


}


后记Q?br />׃一些原因代码写的有些简陋,但功能实玎ͼ表达的的意思也q可以(自己有点 ‘买瓜了’哈哈!Q。我写出来的目的希望大家能共同进步,q等大家拍砖?Q)



        




G_G 2007-12-03 11:29 发表评论
]]>
hbn 拦截?/title><link>http://www.aygfsteel.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.aygfsteel.com/Good-Game/archive/2007/11/29/163922.html</guid><wfw:comment>http://www.aygfsteel.com/Good-Game/comments/163922.html</wfw:comment><comments>http://www.aygfsteel.com/Good-Game/archive/2007/11/29/163922.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Good-Game/comments/commentRss/163922.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.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);"> Interceptor,Serializable{<br /><br />    </span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 0);"> Set inserts </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);"> HashSet();<br />    </span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 0);"> Set updates </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);"> HashSet();<br />    <br />    </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">Session初化一个持久对?如果q方法中改变了对象属性就q回true 否则null</span> <span style="color: rgb(0, 128, 0);"> </span> <span style="color: rgb(0, 0, 0);"> </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">boolean</span> <span style="color: rgb(0, 0, 0);"> onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {<br />    </span> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">false</span> <span style="color: rgb(0, 0, 0);">;<br />    }<br /></span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">    //</span> <span style="color: rgb(0, 128, 0);">Session flush()中检查到脏数据是调用 如:tr.commit() ....</span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">boolean</span> <span style="color: rgb(0, 0, 0);"> onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {<br />        updates.add(entity);<br />        </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">false</span> <span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">Session Save() 当修改了对象属性返回true </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">boolean</span> <span style="color: rgb(0, 0, 0);"> onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {<br />        <br />        inserts.add(entity);<br />        </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">false</span> <span style="color: rgb(0, 0, 0);">;<br />    }<br />     </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">delete </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);"> onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {</span> <span style="color: rgb(0, 0, 0);"> <br />    }<br /></span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">   //flush() 之前调用</span> <span style="color: rgb(0, 128, 0);"> </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);"> preFlush(Iterator entities) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {</span> <span style="color: rgb(0, 0, 0);"> <br />    }<br />     </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">flush() 执行SQL语句之后调用 </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);"> postFlush(Iterator entities) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {<br />        <br />        </span> <span style="color: rgb(0, 0, 255);">try</span> <span style="color: rgb(0, 0, 0);"> {<br />            </span> <span style="color: rgb(0, 0, 255);">for</span> <span style="color: rgb(0, 0, 0);">(Iterator it </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> updates.iterator();it.hasNext();){<br />                System.out.println(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">update=</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">+</span> <span style="color: rgb(0, 0, 0);"> it.next() );    <br />            }<br />            </span> <span style="color: rgb(0, 0, 255);">for</span> <span style="color: rgb(0, 0, 0);">(Iterator it </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> inserts.iterator();it.hasNext();){<br />                System.out.println(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">insert</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">+</span> <span style="color: rgb(0, 0, 0);"> it.next() );    <br />            }<br />            <br />        } </span> <span style="color: rgb(0, 0, 255);">catch</span> <span style="color: rgb(0, 0, 0);"> (Exception e) {<br />            e.printStackTrace();<br />        }<br />        <br />    }<br /><br />    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> Boolean isTransient(Object entity) {<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />    }<br /></span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">  //军_Session中那些对象是脏数?如果null Session使用默认处理脏数?/span> <span style="color: rgb(0, 128, 0);"> </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">int</span> <span style="color: rgb(0, 0, 0);">[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {</span> <span style="color: rgb(0, 128, 0);"> </span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />     </span> <font> <font color="#ffa500"> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 128, 0);">// 当Session构造实体类对象前调?/span> <span style="color: rgb(0, 128, 0);"> </span> </font> </font> <br /> <span style="color: rgb(0, 0, 0);">    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> Object instantiate(String entityName, EntityMode entityMode, Serializable id) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {</span> <span style="color: rgb(0, 128, 0);"> </span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> String getEntityName(Object object) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> Object getEntity(String entityName, Serializable id) </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> CallbackException {<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        </span> <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">null</span> <span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);"> afterTransactionBegin(Transaction tx) {<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        <br />    }<br /><br />    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);"> beforeTransactionCompletion(Transaction tx) {<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        <br />    }<br /><br />    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);"> afterTransactionCompletion(Transaction tx) {<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);"> TODO Auto-generated method stub</span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        <br />    }<br /><br />}<br /></span> </div> <b> <br />试</b> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"> <!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <span style="color: rgb(0, 0, 255);">package</span> <span style="color: rgb(0, 0, 0);"> hbn.test.supper.Interceptor;<br /><br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);"> java.lang.reflect.Field;<br /><br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);"> org.hibernate.Session;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);"> org.hibernate.SessionFactory;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);"> org.hibernate.Transaction;<br /><br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);"> hbn.HibernateSessionFactory;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);"> hbn.bean.T2oo;<br /></span> <span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);"> junit.framework.TestCase;<br /><br /></span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">class</span> <span style="color: rgb(0, 0, 0);"> TestIC </span> <span style="color: rgb(0, 0, 255);">extends</span> <span style="color: rgb(0, 0, 0);"> TestCase {<br />    </span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 0);"> SessionFactory sessionFactory;<br />    </span> <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);"> setUp() </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> Exception {<br />        </span> <span style="color: rgb(0, 0, 255);">super</span> <span style="color: rgb(0, 0, 0);">.setUp();<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">利用java反射得到 HibernateSessionFactory -><br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">private  static org.hibernate.SessionFactory sessionFactory;<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">要模拟 ƈ发 要 HibernateSessionFactory 得出的 有 threadLocal 不行 </span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        HibernateSessionFactory.currentSession();<br />        HibernateSessionFactory.closeSession();<br />        Field field </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> HibernateSessionFactory.</span> <span style="color: rgb(0, 0, 255);">class</span> <span style="color: rgb(0, 0, 0);">.getDeclaredField(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">sessionFactory</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">);<br />        field.setAccessible(</span> <span style="color: rgb(0, 0, 255);">true</span> <span style="color: rgb(0, 0, 0);">);<br />        sessionFactory </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> (SessionFactory) field.get(HibernateSessionFactory.</span> <span style="color: rgb(0, 0, 255);">class</span> <span style="color: rgb(0, 0, 0);">);<br />    }<br />    <br />    </span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);"> testInc() </span> <span style="color: rgb(0, 0, 255);">throws</span> <span style="color: rgb(0, 0, 0);"> Exception {<br />        TestInterceptor intx </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);"> TestInterceptor();<br />        </span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">加蝲拦截?/span> <span style="color: rgb(0, 128, 0);"> <br /> </span> <span style="color: rgb(0, 0, 0);">        Session session </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> sessionFactory.openSession(intx);<br />        <br />        Transaction tr </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> session.beginTransaction();<br />        T2oo t2 </span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);"> </span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);"> T2oo(</span> <span style="color: rgb(0, 0, 0);">23</span> <span style="color: rgb(0, 0, 0);">);<br />        session.save(t2);<br />        t2.setAvg(</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);"> Integer(</span> <span style="color: rgb(0, 0, 0);">99</span> <span style="color: rgb(0, 0, 0);">));<br />        tr.commit();<br />    }<br />}<br /></span> </div>l果<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.aygfsteel.com/Good-Game/aggbug/163922.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Good-Game/" target="_blank">G_G</a> 2007-11-29 10:13 <a href="http://www.aygfsteel.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.aygfsteel.com/Good-Game/archive/2007/11/26/163310.htmlG_GG_GMon, 26 Nov 2007 10:57:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/11/26/163310.htmlhttp://www.aygfsteel.com/Good-Game/comments/163310.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/11/26/163310.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/163310.htmlhttp://www.aygfsteel.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();
    }

悲观?
     /**  悲观锁问题 线E模拟 ƈ发?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();
        Integer testId 
=  testUse.getId();
        
        //q发模拟
        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' " );
        
        //加悲观锁
        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( 
" ********解锁准备********* "  );
                            Thread.sleep(
10 * 1000 );
                            T1oo t1oo1 
=  (T1oo)bean1;
                            t1oo1.setName(
" run2 " );
                            tr1.commit();
                            System.out.println( 
" ********解锁成功t********* "  );
                        } 
catch  (InterruptedException e) {e.printStackTrace();}
                    }
                }
        ).start();

       
/ /q里?{待十秒?tr1解锁 /
        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  |
     *    +---------+---------+------+
     *  注意Q要在数据库中多加一列?br />     *  mysql> alter table T2OO add version int;
     
*/
    
public   void  testOpLock() throws  Exception{
        //d试数据
        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  Integer( 23 ));    
        t2oo.setT1oo(t1oo);
        t1oo.getT2ooSet().add(t2oo);
        session.save(t2oo);
        trI.commit();
        session.clear();
        session.close();

        //q发模拟
        Session se1 
=  sessionFactory.openSession();
        Session se2 
=  sessionFactory.openSession();
        
        Transaction tr1 
=  se1.beginTransaction();
        Transaction tr2 
=  se2.beginTransaction();
       
        T1oo obj1 
=  (T1oo)se1.load(T1oo. class , new  Integer( 1 ));
        T1oo obj2 
=  (T1oo)se2.load(T1oo. class , new  Integer( 1 ));
        
        ((T2oo)obj1.getT2ooSet().iterator().next()).setAvg(
new  Integer( 9 ));
        ((T2oo)obj2.getT2ooSet().iterator().next()).setAvg(
new  Integer( 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和cascade)http://www.aygfsteel.com/Good-Game/archive/2007/11/16/161100.htmlG_GG_GFri, 16 Nov 2007 11:20:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/11/16/161100.htmlhttp://www.aygfsteel.com/Good-Game/comments/161100.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/11/16/161100.html#Feedback2http://www.aygfsteel.com/Good-Game/comments/commentRss/161100.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/161100.html inverse 使用
说明U定-如:
1.表属?:T1oo->id,name
          T2oo->id,avg,aid(
外键 ),version

2.代码q程是:Save->t1oo对象(T1oo 外键 T2oo)
        T1oo t1oo = new T1oo();
        t1oo.setName("xx");       
        t1oo.setT2ooSet(new HashSet());
        T2oo t2oo2 = new T2oo(24);
        //t2oo2.setT1oo(t1oo); (在下面的本用例表中的W?属?)
        t1oo.getT2ooSet().add(t2oo2);
(在下面的本用例表中的W?属?)
        session.save(t1oo);

3.本例表用:
T1oo.hbm.xml ->
        <set name="t2ooSet" inverse="true" cascade = "all"  >
(在下面的本用例表中的W?Q?属?)
            <key column="aid"/>
            <one-to-many class="T2oo"/>
        </set>
T2oo.hbm.xml ->
        <many-to-one name="t1oo" column="aid"  class="T1oo" cascade="all" />
(在下面的本用例表中的W?Q?属?)
l合上面说明得表为:
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo  | true  | all   |          |          |
+-------+-------+-------+  true    |  false   |
| t2oo  |       | all   |          |          |
+-------+-------+-------+----------+----------+


4.执行 Hibernate语句 Q?/b>
    Hibernate: insert into t1oo (name, id) values (?, ?)
    Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
5.l果为:
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; //
因ؓ T1ooL可以 Save 下面׃再提?/font>
+----+------+
| id | name |
+----+------+
|  2 | xx   |
+----+------+
1 row in set (0.00 sec)

可改:

+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo  | 可改4 | 可改2  |          |          |
+-------+-------+-------+           |   可改1  |
| t2oo  |       | 可改3 |           |          |
+-------+-------+-------+----------+----------+



L:
+-------+-------+-------+----------+----------+
|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.aygfsteel.com/Good-Game/archive/2007/11/12/160000.htmlG_GG_GMon, 12 Nov 2007 08:29:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/11/12/160000.htmlhttp://www.aygfsteel.com/Good-Game/comments/160000.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/11/12/160000.html#Feedback1http://www.aygfsteel.com/Good-Game/comments/commentRss/160000.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/160000.html ~存问题Q?br />
     /**  使用 Query.executeUpdate() ~存中数据不同步 解决办法
     *  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 成ؓ 持久状态 一U缓存中 加蝲
        session.saveOrUpdate(t1oo); 
        
// 直接一条语句删除T1ooQ缓存无法同?br />         // 一U缓存中q有 t1
        Query qu  =  session.createQuery( " delete T1oo " );
        
try  {
            qu.executeUpdate();
        } 
catch  (Exception e) {    
            System.out.println(
" //err: 有联 单使用 delete T1oo q要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();
        
// q直接通过一U缓存中加蝲t2Q但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 中数据库中没有 t1oo // " );
        }
        

        System.out.println(
"  一U缓存清I前  " +  session.get(T1oo. class ,t1oo.getId()) );
        session.evict(t1oo);
        System.out.println(
"  一U缓存清I后  " +  session.get(T1oo. class ,t1oo.getId()) );
        
        
// 不把t1oo id 为空Q否则当在saveOrUpdate时候就会以为是游离态 update?/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();
        
    }
l果是:
********************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 q要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 //
 一U缓存清I前 hbn.bean.T1oo@287
Hibernate: select t1oo0_.id as id0_, t1oo0_.name as name0_0_ from t1oo t1oo0_ where t1oo0_.id=?
 一U缓存清I后 null
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: delete from t1oo where id=?


Get Load 区别 Q?br /> 
    /** Get Load 区别 (在commit前要 session.flush())
     * Table -> T1oo(id,name)
     * 1.如果未能发现W合条g的记录,getҎq回nullQ而loadҎ会抛出异?br />     * 2.LoadҎ可返回实体的代理cd例,而getҎ永远直接q回实体cR?br />     * 3.loadҎ可以充分利用内部~存和二U缓存中的现有数据,而getҎ则仅仅在内部~存中进行数据查找,
     *             如没有发现对应数据,越q二U缓存,直接调用SQL完成数据d。?br />     
*/
    
public void 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);
        
// Z么这会错Q?br />        //session.evict(t1oo);
        
//session.flush();
        t1.commit();
        session.evict(t1oo);
    
    Transaction t2 
= session.beginTransaction();
        System.out.println(
"一U缓存是否有t1oo(load)->"+session.contains(t1oo)); 
        
//q时候t1oo为 CGlib生成的代理类
        t1oo = (T1oo)session.load(T1oo.class,t1oo.getId());
        System.out.println(
" 延迟加蝲出现Qselect.. t1oo0_.id=? 表的其他属性加q?/span>");
        t1oo.setName(
"load list");
        
//后在 update
        t2.commit();
        session.evict(t1oo);
        
    Transaction t3 
= session.beginTransaction();
        System.out.println(
"一U缓存是否有t1oo(get)->"+session.contains(t1oo)); 
        
//q时候t1oo为 CGlib生成的代理类
        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();
        
    }
l果
********************Get<>Load************************
Hibernate: insert into t1oo (name, id) values (?, ?)
一U缓存是否有t1oo(load)->false
 延迟加蝲出现Qselect.. 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=?
一U缓存是否有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>Q?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 />     *     当 T1oo没有 inverse="true" d权的时候,要双关?br />     *     t1oo.getT2ooSet().add(t2oo1);
     *    t1oo.getT2ooSet().add(t2oo2);
     *    t2oo1.setT1oo(t1oo);
     *    t2oo2.setT1oo(t1oo);
     *  要不后sql为:
     *  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 |
     *    +----+-----+------+
     *    当 T1oo有 inverse="false"d权的时?br />     *  t1oo.getT2ooSet().add(t2oo1);
     *    t1oo.getT2ooSet().add(t2oo2);
     *  Sql语句为:
     *  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 |
     *    +----+-----+------+
    
*/
    
public void testSet(){
        System.out.println(
"\r\n\r\n********************Set************************");
        T1oo t1oo 
= new T1oo();
        t1oo.setName(
"list");
        
        T2oo t2oo1 
= new T2oo(); t2oo1.setAvg(new Integer(23));
        T2oo t2oo2 
= new T2oo(); t2oo2.setAvg(new Integer(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 Integer(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: 没有d权 cascade = 'all' 不可以联save t1oo   //");
        }
        session.close();
        HibernateSessionFactory.closeSession();
    }
   

l果是:
********************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: 没有d?cascade = 'all' 不可以联save t1oo   //




G_G 2007-11-12 16:29 发表评论
]]>
源码分析http://www.aygfsteel.com/Good-Game/archive/2007/10/23/155210.htmlG_GG_GTue, 23 Oct 2007 02:34:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/10/23/155210.htmlhttp://www.aygfsteel.com/Good-Game/comments/155210.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/10/23/155210.html#Feedback0http://www.aygfsteel.com/Good-Game/comments/commentRss/155210.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/155210.html
public final class CollectionHelper {
    
public static final List EMPTY_LIST = Collections.unmodifiableList( new ArrayList(0) );
    
public static final Collection EMPTY_COLLECTION = Collections.unmodifiableCollection( new ArrayList(0) );
    
public static final Map EMPTY_MAP = Collections.unmodifiableMap( new HashMap(0) );
    
private CollectionHelper() {}
}
    1.1 在Collections.unmodifiableList(...)是静态内部类个构造方?
       悟: 从上面看出是一个非常好的?适配? 
   //1.Collections ?new 出内部类
    public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
        return new UnmodifiableCollection<T>(c);
    }



    //2.
内部c?/font>也? Collection
    static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
    private static final long serialVersionUID = 1820017752578914078L;

    
final Collection<? extends E> c;
    //3.很好适配 Collection 通过他把 add remove {功?装
    UnmodifiableCollection(Collection
<? extends E> c) {
            
if (c==null)
                
throw new NullPointerException();
            
this.c = c;
        }
    ...............

2.大量使用内部cL??如:
Mappings.PropertyReference upr = (Mappings.PropertyReference) iter.next();
////////////////////////////////////////////////
Mappings?br />    static final class PropertyReference implements Serializable {
        String referencedClass;
        String propertyName;
        
boolean unique;
    }

//感觉是可以更好的代码~写






G_G 2007-10-23 10:34 发表评论
]]>
没有玩{?hibernate hql ?/title><link>http://www.aygfsteel.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.aygfsteel.com/Good-Game/archive/2007/08/24/138889.html</guid><wfw:comment>http://www.aygfsteel.com/Good-Game/comments/138889.html</wfw:comment><comments>http://www.aygfsteel.com/Good-Game/archive/2007/08/24/138889.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Good-Game/comments/commentRss/138889.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Good-Game/services/trackbacks/138889.html</trackback:ping><description><![CDATA[    1。文件的映射。由于?MyEclipseQ傻瓜版^_^Q?所?hibernate工具QextensionsQMiddlegenQtools{)一直没好好研究q就不献丑了?br />    2。Criteria <br />       1Q查询条仉过 Criteria.add d Expression 用于描述条g <br />                Expression.( and or like in le lt ..... )<br />                参见Q?<a accesskey="n" >Criteria查询</a><br />       2Qcriteria.setFirstResult(100);<br />          criteria.setMaxResults(20); <font color="#008000">//索范?br />          <font color="#000000">criteria.addOrder(Order.asc("name")); <font color="#006400">//排序</font></font></font><br />          .add( Expression.sql("lower($alias.name) like lower(?)", "Fritz%", Hibernate.STRING)   <font color="#006400">//直接嵌入SQL<br />       <font color="#000000">3Q?/font></font>Cat cat = new Cat();<br />          ....<br /><br />         List results = session.createCriteria(Cat.class).add( Example.create(cat) ).list(); <font color="#006400">//Ҏ对象查询</font><br />      <br />    3。HQL 完全面向对象的,具备l承、多态和兌{特性?br />       1Q参见: <a accesskey="n" >HQL</a><br />       2Q内q接Qinner join<tt class="literal"><br />          左外q接Qleft outer join</tt><tt class="literal"><br />          叛_q接Qright outer join</tt>  <br /><font color="#006400">          http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html<br />          代表性的语句</font>     <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><span style="color: rgb(0, 0, 0);"><font color="#0000ff">Hql.hbm.xml</font><br />        </span><span style="color: rgb(0, 0, 255);"><</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">//单的 l果Q?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">//l出对象 l果 : 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">//l出Object l果Qheha 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">//l出 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 />//报表语句  l果: 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 />//集合?l果 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 l定 <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Query q </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> s.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">from foo Foo as foo where foo.name=:name and foo.size=:size</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />q.setProperties(fooBean); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> fooBean包含ҎgetName()与getSize()</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">List foos </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> q.list();</span></div><br /></font><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img src ="http://www.aygfsteel.com/Good-Game/aggbug/138889.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Good-Game/" target="_blank">G_G</a> 2007-08-24 16:36 <a href="http://www.aygfsteel.com/Good-Game/archive/2007/08/24/138889.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hbn W记http://www.aygfsteel.com/Good-Game/archive/2007/08/09/128154.htmlG_GG_GThu, 09 Aug 2007 06:30:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/08/09/128154.htmlhttp://www.aygfsteel.com/Good-Game/comments/128154.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/08/09/128154.html#Feedback3http://www.aygfsteel.com/Good-Game/comments/commentRss/128154.htmlhttp://www.aygfsteel.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. 容器储存 Q从表没主键Q?/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. 列ؓ对象
<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.aygfsteel.com/Good-Game/archive/2007/07/04/128143.htmlG_GG_GWed, 04 Jul 2007 08:49:00 GMThttp://www.aygfsteel.com/Good-Game/archive/2007/07/04/128143.htmlhttp://www.aygfsteel.com/Good-Game/comments/128143.htmlhttp://www.aygfsteel.com/Good-Game/archive/2007/07/04/128143.html#Feedback5http://www.aygfsteel.com/Good-Game/comments/commentRss/128143.htmlhttp://www.aygfsteel.com/Good-Game/services/trackbacks/128143.html 07-11-9
׃我对hibernate理解的深入,在次我来l结下这文章的不的地方(好多人在拍砖哈)
对复杂的逻辑q样开和关太冒׃?br />  在普通的逻辑Ҏ?session都叠加的用上2ơ以?q就要对一U缓冲的理。事物的使用也是一栗?br />


下蝲:
 
http://www.aygfsteel.com/Files/Good-Game/hibernateUtil.rar

单的l承?: 
      l承 DBManage c?把 数据包装类 理 ( 填充 , set {?) 后就直接攑ֈ?insert , update , delete Ҏ?br />         填充可看看这?很有实用价?----> ( ?Form -> Bean  或 Bean ->Bean  ) 例子很简单但x表明  
                                                                           
      l承 DBSelect c??select  q只??Query l出 单用??br />                                                      Query qu =   dao.getQueryBegin(  " HQL " );
                                                              qu.add .....
                                                      List li = dao.getQuertyEnd( qu );
     扩展:  Criteria  cr , SQLQuery , {等 把她包装?各个 dao ?不是 ?h     : )


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

        List list 
= new ArrayList();
        
        Query qu =
getQueryBegin("HSQL");   //l承带来的好?br />        
        .add..........

             
       List list = getQuertyEnd(qu);
   //l承l束
        

        
return  list;
    }

~点: 最好不要static 在一旉点上 此类不能支持多线E?br />               basicBegin  basicEnd  Ҏ ( 我找不到好的Ҏ 要的话来?q接池什么的 大家讨论西西?!  )
               sess 为全局?session 要是 写成 baseicBegin q回的话 感觉下面的还有很多要?什?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 ;
    }
    
    
protected void basicEnd(Session sess){
        
try {
            ((Transaction)transmap.remove(sess)).commit();
        }
catch(Exception e){System.out.println(e);}
        
finally{
            sess 
= null ;
            SessionManager.closeSession();
        }
    }

心得: 多包装多快乐

G_G 2007-07-04 16:49 发表评论
]]>
վ֩ģ壺 | | Դ| | Զ| | | | Դ| ̶| ¤| ˳| | ʻ| | °| ˮ| | Ϫ| | Դ| | â| | ̨| | | ʯ| | | »| | | | ݶ| | կ| | ɳ| | |