论坛首页 入门技术论坛

hibernate 完整小例

浏览 1793 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-08-27  
本人初学 hibernate , 望大家多指点
笔记下
xml 代码
xml 代码
 
  1. <property name="show_sql">true<!---->property>   //控制台 运行 输出 sql  
  2. <property name="hbm2ddl.auto">update<!---->property>  // DB 中没表 就 由 mapping create 表
  3. <set name="fkSet" inverse="true" cascade="all">   //inverse =true 有主控 可以             update,delete,.... ;  cascade='all' 可以级连 更新
  4. constranid  反向生成 DB

java 代码 CONFIG_FILE_LOCATION 为  /../hibernate.cfg.xml  路径
 
 
  1. static public void main(String[]args){  
  2.     Configuration cf = new Configuration().configure(CONFIG_FILE_LOCATION);  
  3.     System.out.println("Creating tables...");   
  4.     SchemaExport schemaExport = new SchemaExport(cf);  
  5.     schemaExport.create(truetrue);  
  6. }   //由 hibernate.xml  建表


xml 代码
  1. <xml></xml> version='1.0' encoding='UTF-8'?>  
  2.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  4. <!---->  
  5. <hibernate-configuration> 
  6. <session-factory>  
  7.     <property name="myeclipse.connection.profile">my<!---->property>  
  8.     <property name="connection.url">jdbc:mysql://localhost/hibernate<!---->property>  
  9.     <property name="connection.username">root<!---->property>  
  10.     <property name="connection.password">123456<!---->property>  
  11.     <property name="connection.driver_class">com.mysql.jdbc.Driver<!---->property>  
  12.     <property name="dialect">org.hibernate.dialect.MySQLDialect<!---->property>  
  13.     <property name="show_sql">true<!---->property>  
  14.     <property name="hbm2ddl.auto">update<!---->property>  
  15.     <mapping resource="bean/Fk.hbm.xml" />  
  16.     <mapping resource="bean/Hql.hbm.xml" /> 
  17. <!----><session-factory></session-factory>> 
  18. <!----><hibernate-configuration></hibernate-configuration>>  

Hql.hbm.xml
xml 代码
  1. <xml></xml> version="1.0" encoding='UTF-8'?>  
  2.                             "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3.                             "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
  4. <!---->  
  5. <!---->  
  6. <!---->  
  7. <hibernate-mapping package="bean"> 
  8.     <class name="Hql" table="hql">  
  9.         <id name="id" column="id" type="integer">  
  10.             <generator class="increment"/>  
  11.         <!----><id></id>> 
  12.         <property name="name" column="name" type="string"  not-null="true" /> 
  13.         <set name="fkSet" inverse="true" cascade="all">  
  14.             <key column="hid"/>  
  15.             <one-to-many class="Fk"/>  
  16.         <<!---->set>  
  17.     <<!---->class> 
  18. <!----><hibernate-mapping></hibernate-mapping>>  

Fk.hbm.xml
xml 代码
 
  1. xml version="1.0" encoding='UTF-8'?>  
  2.                             "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3.                             "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
  4. <!---->  
  5. <!---->  
  6. <!---->  
  7. <hibernate-mapping package="bean">  
  8.     <class name="Fk" table="fk">  
  9.         <id name="id" column="id" type="integer">  
  10.             <generator class="increment"/>  
  11.         <!---->id>  
  12.         <property name="name" column="name" type="string"  not-null="true" />  
  13.         <many-to-one name="hql" column="hid" class="Hql"  not-null="true" />  
  14.     <!---->class>  
  15. <!---->hibernate-mapping>  

test
java 代码
 
  1. public class testHQL extends TestCase { 
  2. //没有DB 会自动加载 hbm2ddl.auto 为 update
  3.     public void testSave(){  
  4.         Session se = HibernateSessionFactory.currentSession() ;  
  5.         Transaction tr = se.beginTransaction() ; 
  6.         Hql qq = new Hql();  
  7.         qq.setName("liukaiyi"); 
  8.         Fk f1 = new Fk();  
  9.         f1.setName("oo");  
  10.         f1.setHql(qq); 
  11.         Set set = new HashSet();  
  12.         set.add(f1);  
  13.         qq.setFkSet(set); 
  14.         se.save(qq);  //hql有  inverse="true" 主控 所以 save hql 也会 save fk
  15.         tr.commit();  
  16.     }  
  17.   
  18.     public void testHql(){  
  19.         Session se = HibernateSessionFactory.currentSession() ;  
  20.         Transaction tr = se.beginTransaction() ; 
  21.         Query q = se.createQuery("from Hql h where h.fkSet.hql = h.id   "); 
  22.         //组合查询 同 select * from hql h , fk f where h.id = f.hid
  23.         for( Iterator it = q.list().iterator() ;it.hasNext();){  
  24.             System.out.println( ((Hql)it.next()).getName() );  
  25.         } 
  26.         tr.commit();  
  27.     }  
  28. }  


论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics