读书人

Hibernate关联关系投射实例速查

发布时间: 2012-10-06 17:34:01 作者: rapoo

Hibernate关联关系映射实例速查
Hibernate关联关系映射实例速查????????Hibernate的映射关系很多,也比较复杂,也很容易忘记。这个基本上占据了Hibernate学习的七成时间。熟悉这些映射模型,需要大量的实践才能掌握。下面是我对Hibernate关联关系映射的一个总结,放到blog上一是自己查看方便,二来也可以和更多Hibernate开发人员交流分享。希望各位多多留言哦:)。??????? 本文主要参考夏昕翻译的“Hibernate参考文档 V3.12”,也在附件中给出了。
??????? 本文的模块较多,映射关系部分分为一下模块:?Hibernate关联关系映射目录

├─单向关联
│? ├─? 一对一外键单向关联
│? ├─? 一对一主键单向关联
│? ├─? 一对一连接表单向关联
│? ├─? 一对多外键单向关联??(在多方做外键)
│? ├─? 一对多连接表单向关联
│? ├─? 多对一外键单向关联
│? ├─? 多对一连接表单向关联
│? └─? 多对多单向关联
└─双向关联
??? ├─? 一对一外键双向关联
??? ├─? 一对一主键双向关联
??? ├─? 一对一连接表双向关联
??? ├─? 一对多外键双向关联
??? ├─? 一对多连接表双向关联
??? └─? 多对多双向关联??本系列实例的开发环境:?Windows XP Professional 简体中文版MySQL 5.0.45Hibernate 3.12Java SDK 1.5.06IntelliJ IDEA 5.12?系列实例中所用的Hibernate配置文件如下:?<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC
??????? "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
??????? "[url]http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd[/url]"><hibernate-configuration>
??? <session-factory>
??????? <!--指定连接数据库驱动-->
??????? <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
??????? <!--指定连接数据库的url,hibernate连接的数据库名-->
??????? <property name="connection.url">jdbc:mysql://localhost:3306/hbstudy</property>
??????? <!--指定连接数据库的用户名-->
??????? <property name="connection.username">root</property>
??????? <!--指定连接数据库的用户密码-->
??????? <property name="connection.password">leizhimin</property>
??????? <!--指定连接池的大小-->
??????? <property name="connection.pool_size">5</property>
??????? <!--指定数据库的方言-->
??????? <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
??????? <!--根据需要自动创建数据库,测试环境用-->
??????? <property name="hbm2ddl.auto">create</property>
??????? <!--在控制台显示执行的SQL语句-->
??????? <property name="show_sql">true</property>
??????? <!-- Enable Hibernate's automatic session context management -->
??????? <property name="current_session_context_class">thread</property>??????? <!--映射文件列表-->
??????? <!--单向关联-->
??????? <mapping resource="com/lavasoft/dx/_n_1_fk/Addressn1fk.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_n_1_fk/Personn1fk.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_n_1_tab/Addressn1tab.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_n_1_tab/Personn1tab.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_1_fk/Address11fk.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_1_fk/Person11fk.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_1_tab/Address11tab.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_1_tab/Person11tab.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_1_pk/Address11pk.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_1_pk/Person11pk.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_n_fk/Address1nfk.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_n_fk/Person1nfk.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_n_tab/Address1ntab.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_1_n_tab/Person1ntab.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_n_n/Addressnn.hbm.xml"/>
??????? <mapping resource="com/lavasoft/dx/_n_n/Personnn.hbm.xml"/>??????? <!--双向关联-->
??????? <mapping resource="com/lavasoft/sx/_1_n_fk/Address1nfk_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_n_fk/Person1nfk_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_n_tab/Address1ntab_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_n_tab/Person1ntab_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_n_n/Addressnn_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_n_n/Personnn_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_1_fk/Address11fk_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_1_fk/Person11fk_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_1_pk/Address11pk_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_1_pk/Person11pk_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_1_tab/Address11tab_sx.hbm.xml"/>
??????? <mapping resource="com/lavasoft/sx/_1_1_tab/Person11tab_sx.hbm.xml"/>??? </session-factory>
</hibernate-configuration>?系列实例中所用到Session工厂是:public class HibernateUtil {
??? private static final SessionFactory sessionFactory;??? static {
??????? try {
??????????? // Create the SessionFactory from hibernate.cfg.xml
??????????? sessionFactory = new Configuration().configure().buildSessionFactory();
??????? } catch (Throwable ex) {
??????????? // Make sure you log the exception, as it might be swallowed
??????????? System.err.println("初始化SessionFactory失败!" + ex);
??????????? throw new ExceptionInInitializerError(ex);
??????? }
??? }??? public static final ThreadLocal session = new ThreadLocal();??? public static Session getCurrentSession() throws HibernateException {
??????? Session s = (Session) session.get();
??????? //当原Session为空或已关闭时,打开一个新的Session
??????? if (s == null || !s.isOpen()) {
??????????? s = sessionFactory.openSession();
??????????? session.set(s);
??????? }
??????? return s;
??? }??? public static void closeSession() throws HibernateException {
??????? Session s = (Session) session.get();
??????? session.set(null);
??????? if (s != null) {
??????????? s.close();
??????? }
??? }
}
?????

本文出自 中山大学新华学院论坛,转载请与作者联系!

读书人网 >软件架构设计

热点推荐