本人初学 hibernate , 望大家多指点

笔记下
xml 代码
xml 代码
- <property name="show_sql">true<!---->property> //控制台 运行 输出 sql
- <property name="hbm2ddl.auto">update<!---->property> // DB 中没表 就 由 mapping create 表
- <set name="fkSet" inverse="true" cascade="all"> //inverse =true 有主控 可以 update,delete,.... ; cascade='all' 可以级连 更新
- constranid 反向生成 DB
java 代码 CONFIG_FILE_LOCATION 为 /../hibernate.cfg.xml 路径
- static public void main(String[]args){
- Configuration cf = new Configuration().configure(CONFIG_FILE_LOCATION);
- System.out.println("Creating tables...");
- SchemaExport schemaExport = new SchemaExport(cf);
- schemaExport.create(true, true);
- } //由 hibernate.xml 建表
xml 代码
- <xml></xml> version='1.0' encoding='UTF-8'?>
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
- <session-factory>
- <property name="myeclipse.connection.profile">my<!---->property>
- <property name="connection.url">jdbc:mysql://localhost/hibernate<!---->property>
- <property name="connection.username">root<!---->property>
- <property name="connection.password">123456<!---->property>
- <property name="connection.driver_class">com.mysql.jdbc.Driver<!---->property>
- <property name="dialect">org.hibernate.dialect.MySQLDialect<!---->property>
- <property name="show_sql">true<!---->property>
- <property name="hbm2ddl.auto">update<!---->property>
- <mapping resource="bean/Fk.hbm.xml" />
- <mapping resource="bean/Hql.hbm.xml" />
- <!----><session-factory></session-factory>>
- <!----><hibernate-configuration></hibernate-configuration>>
Hql.hbm.xml
xml 代码
- <xml></xml> version="1.0" encoding='UTF-8'?>
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
-
-
-
- <hibernate-mapping package="bean">
- <class name="Hql" table="hql">
- <id name="id" column="id" type="integer">
- <generator class="increment"/>
- <!----><id></id>>
- <property name="name" column="name" type="string" not-null="true" />
- <set name="fkSet" inverse="true" cascade="all">
- <key column="hid"/>
- <one-to-many class="Fk"/>
- <<!---->set>
- <<!---->class>
- <!----><hibernate-mapping></hibernate-mapping>>
Fk.hbm.xml
xml 代码
- xml version="1.0" encoding='UTF-8'?>
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
-
-
-
- <hibernate-mapping package="bean">
- <class name="Fk" table="fk">
- <id name="id" column="id" type="integer">
- <generator class="increment"/>
- <!---->id>
- <property name="name" column="name" type="string" not-null="true" />
- <many-to-one name="hql" column="hid" class="Hql" not-null="true" />
- <!---->class>
- <!---->hibernate-mapping>
test
java 代码
- public class testHQL extends TestCase {
- //没有DB 会自动加载 hbm2ddl.auto 为 update
- public void testSave(){
- Session se = HibernateSessionFactory.currentSession() ;
- Transaction tr = se.beginTransaction() ;
- Hql qq = new Hql();
- qq.setName("liukaiyi");
- Fk f1 = new Fk();
- f1.setName("oo");
- f1.setHql(qq);
- Set set = new HashSet();
- set.add(f1);
- qq.setFkSet(set);
- se.save(qq); //hql有 inverse="true" 主控 所以 save hql 也会 save fk
- tr.commit();
- }
-
- public void testHql(){
- Session se = HibernateSessionFactory.currentSession() ;
- Transaction tr = se.beginTransaction() ;
- Query q = se.createQuery("from Hql h where h.fkSet.hql = h.id ");
- //组合查询 同 select * from hql h , fk f where h.id = f.hid
- for( Iterator it = q.list().iterator() ;it.hasNext();){
- System.out.println( ((Hql)it.next()).getName() );
- }
- tr.commit();
- }
- }