博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate关联关系映射实例速查
阅读量:6893 次
发布时间:2019-06-27

本文共 4304 字,大约阅读时间需要 14 分钟。

   Hibernate的映射关系很多,也比较复杂,也很容易忘记。这个基本上占据了Hibernate学习的七成时间。熟悉这些映射模型,需要大量的实践才能掌握。下面是我对Hibernate关联关系映射的一个总结,放到blog上一是自己查看方便,二来也可以和更多Hibernate开发人员交流分享。希望各位多多留言哦:)。
        本文主要参考夏昕翻译的“Hibernate参考文档 V3.12”,也在附件中给出了。
        
本文的模块较多,映射关系部分分为一下模块:
 
Hibernate关联关系映射目录
│ 
├─单向关联
│  ├─   │  ├─  
│  ├─   │  ├─  
│  ├─  
│  ├─  
│  ├─  
│  └─  
└─双向关联
    ├─  
    ├─       ├─  
    ├─  
    ├─  
    └─  
 
 
本系列实例的开发环境:
 
Windows XP Professional 简体中文版
MySQL 5.0.45
Hibernate 3.12
Java SDK 1.5.06
IntelliJ IDEA 5.12
 
系列实例中所用的Hibernate配置文件如下:
 
<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        " ">
<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();
        }
    }
}
 
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/39275,如需转载请自行联系原作者
你可能感兴趣的文章
《c程序设计语言》读书笔记--统计换行数,空格数,tab数
查看>>
【14】代理模式(Proxy Pattern)
查看>>
Visual Studio 2013 为C#类文件添加版权信息
查看>>
hdu1025(nlon(n)最长上升子序列)
查看>>
将Linux文件清空的几种方法
查看>>
Centos7使用yum快速安装ansible
查看>>
java例程练习(网络编程[简单网络连接试验])
查看>>
使用 console.time() 计算js代码执行时间
查看>>
test2-1
查看>>
Linux 进程间通信 有名管道(fifo)
查看>>
Oracle中SQL查询表字段基本信息、主键、外键(转)
查看>>
Oracle 12C 新特性之 恢复表
查看>>
Cisco交换机里IP和MAC地址互查找出对应端口
查看>>
随机数
查看>>
201671010117 2016-2017-2 《JAVA程序设计》java第十六周学习心得
查看>>
vue数据更新UI不刷新显示解决方案
查看>>
周例会的作用
查看>>
深入理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
查看>>
泛型中的协变
查看>>
1-22
查看>>