Hibernate关联关系映射实例速查
│
├─单向关联
│├─一对一外键单向关联
│├─一对一主键单向关联
│├─一对一连接表单向关联
│├─一对多外键单向关联(在多方做外键)
│├─一对多连接表单向关联
│├─多对一外键单向关联
│├─多对一连接表单向关联
│└─多对多单向关联
└─双向关联
├─一对一外键双向关联
├─一对一主键双向关联
├─一对一连接表双向关联
├─一对多外键双向关联
├─一对多连接表双向关联
└─ 多对多双向关联<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"[url]http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd[/url]"><session-factory>
<!--指定连接数据库驱动-->
<propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--指定连接数据库的url,hibernate连接的数据库名-->
<propertyname="connection.url">jdbc:mysql://localhost:3306/hbstudy</property>
<!--指定连接数据库的用户名-->
<propertyname="connection.username">root</property>
<!--指定连接数据库的用户密码-->
<propertyname="connection.password">leizhimin</property>
<!--指定连接池的大小-->
<propertyname="connection.pool_size">5</property>
<!--指定数据库的方言-->
<propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--根据需要自动创建数据库,测试环境用-->
<propertyname="hbm2ddl.auto">create</property>
<!--在控制台显示执行的SQL语句-->
<propertyname="show_sql">true</property>
<!--EnableHibernate'sautomaticsessioncontextmanagement-->
<property name="current_session_context_class">thread</property><!--单向关联-->
<mappingresource="com/lavasoft/dx/_n_1_fk/Addressn1fk.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_n_1_fk/Personn1fk.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_n_1_tab/Addressn1tab.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_n_1_tab/Personn1tab.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_1_fk/Address11fk.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_1_fk/Person11fk.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_1_tab/Address11tab.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_1_tab/Person11tab.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_1_pk/Address11pk.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_1_pk/Person11pk.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_n_fk/Address1nfk.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_n_fk/Person1nfk.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_n_tab/Address1ntab.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_1_n_tab/Person1ntab.hbm.xml"/>
<mappingresource="com/lavasoft/dx/_n_n/Addressnn.hbm.xml"/>
<mapping resource="com/lavasoft/dx/_n_n/Personnn.hbm.xml"/><mappingresource="com/lavasoft/sx/_1_n_fk/Address1nfk_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_1_n_fk/Person1nfk_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_1_n_tab/Address1ntab_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_1_n_tab/Person1ntab_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_n_n/Addressnn_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_n_n/Personnn_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_1_1_fk/Address11fk_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_1_1_fk/Person11fk_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_1_1_pk/Address11pk_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_1_1_pk/Person11pk_sx.hbm.xml"/>
<mappingresource="com/lavasoft/sx/_1_1_tab/Address11tab_sx.hbm.xml"/>
<mapping resource="com/lavasoft/sx/_1_1_tab/Person11tab_sx.hbm.xml"/>try{
//CreatetheSessionFactoryfromhibernate.cfg.xml
sessionFactory=newConfiguration().configure().buildSessionFactory();
}catch(Throwableex){
//Makesureyoulogtheexception,asitmightbeswallowed
System.err.println("初始化SessionFactory失败!"+ex);
thrownewExceptionInInitializerError(ex);
}
}Sessions=(Session)session.get();
//当原Session为空或已关闭时,打开一个新的Session
if(s==null||!s.isOpen()){
s=sessionFactory.openSession();
session.set(s);
}
returns;
}Sessions=(Session)session.get();
session.set(null);
if(s!=null){
s.close();
}
}
}
本文出自 中山大学新华学院论坛,转载请与作者联系!