• [原创]java WEB学习笔记84:Hibernate学习之路-- -映射 一对一关系 ,基外键的方式实现


    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

    内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

    本人互联网技术爱好者,互联网技术发烧友

    微博:伊直都在0221

    QQ:951226918

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1.映射一对一关联关系

      1)域模型

          

      2)关系数据模型:

        

    2.基于外键映射的 1-1

      1)对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加many-to-one元素。为many-to-one元素增加unique=“true” 属性来表示为1-1关联

    1  <!-- 使用 many-to-one 的方式来映射 1-1 关联关系 -->
    2         <many-to-one name="mgr" class="Manager">
    3             <column name="MGR_ID" unique="true"></column>
    4         </many-to-one>

       2)另一端需要使用one-to-one元素,该元素使用 property-ref 属性指定使用被关联实体主键以外的字段作为关联字段

    1  <!-- 映射 1-1 的关联关系:在对应的数据表中已经有外键了,当前持久化类使用 one-to-one 进行映射
    2                 需要使用one-to-one元素,该元素使用 property-ref 属性指定使用被关联实体主键以外的字段作为关联字段
    3          -->
    4         <one-to-one name="dept" class="Department" property-ref="mgr"></one-to-one>

      

                       

     

    Manager
     1 package com.jason.hibernate.entities.foreign.one2one;
     2 
     3 public class Manager {
     4 
     5     private Integer mgrId;
     6     private String mgrName;
     7     private Department dept;
     8 
     9     public Integer getMgrId() {
    10         return mgrId;
    11     }
    12 
    13     public void setMgrId(Integer mgrId) {
    14         this.mgrId = mgrId;
    15     }
    16 
    17     public String getMgrName() {
    18         return mgrName;
    19     }
    20 
    21     public void setMgrName(String mgrName) {
    22         this.mgrName = mgrName;
    23     }
    24 
    25     public Department getDept() {
    26         return dept;
    27     }
    28 
    29     public void setDept(Department dept) {
    30         this.dept = dept;
    31     }
    32 
    33 }
    Department 
     1 package com.jason.hibernate.entities.foreign.one2one;
     2 
     3 public class Department {
     4     private Integer deptId;
     5     private String deptName;
     6 
     7     private Manager mgr;
     8 
     9     public Integer getDeptId() {
    10         return deptId;
    11     }
    12 
    13     public void setDeptId(Integer deptId) {
    14         this.deptId = deptId;
    15     }
    16 
    17     public String getDeptName() {
    18         return deptName;
    19     }
    20 
    21     public void setDeptName(String deptName) {
    22         this.deptName = deptName;
    23     }
    24 
    25     public Manager getMgr() {
    26         return mgr;
    27     }
    28 
    29     public void setMgr(Manager mgr) {
    30         this.mgr = mgr;
    31     }
    32 
    33 }

    Department.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 <!-- Generated 2016-10-5 22:31:35 by Hibernate Tools 3.4.0.CR1 -->
     5 <hibernate-mapping package="com.jason.hibernate.entities.foreign.one2one">
     6     <class name="Department" table="DEPARTMENT">
     7     
     8         <id name="deptId" type="java.lang.Integer">
     9             <column name="DEPT_ID" />
    10             <generator class="native" />
    11         </id>
    12         
    13         <property name="deptName" type="java.lang.String">
    14             <column name="DEPT_NAME" />
    15         </property>
    16         
    17         <!-- 使用 many-to-one 的方式来映射 1-1 关联关系 -->
    18         <many-to-one name="mgr" class="Manager">
    19             <column name="MGR_ID" unique="true"></column>
    20         </many-to-one>
    21         
    22     </class>
    23     
    24 </hibernate-mapping>

    Manager.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 <!-- Generated 2016-10-5 22:31:35 by Hibernate Tools 3.4.0.CR1 -->
     5 <hibernate-mapping package="com.jason.hibernate.entities.foreign.one2one">
     6     <class name="Manager" table="MANAGER">
     7     
     8         <id name="mgrId" type="java.lang.Integer">
     9             <column name="MGR_ID" />
    10             <generator class="native" />
    11         </id>
    12         
    13         <property name="mgrName" type="java.lang.String">
    14             <column name="MGR_NAME" />
    15         </property>
    16         
    17         
    18         <!-- 映射 1-1 的关联关系:在对应的数据表中已经有外键了,当前持久化类使用 one-to-one 进行映射
    19                 需要使用one-to-one元素,该元素使用 property-ref 属性指定使用被关联实体主键以外的字段作为关联字段
    20          -->
    21         <one-to-one name="dept" class="Department" property-ref="mgr"></one-to-one>
    22     </class>
    23     
    24 </hibernate-mapping>

    hibernate.cfg.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 <hibernate-configuration>
     6     <session-factory>
     7         <!-- hibernate 连接数据库的基本信息 -->
     8         <property name="connection.username">root</property>
     9         <property name="connection.password">zhangzhen</property>
    10         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    11         <property name="connection.url">jdbc:mysql:///hibernate</property>
    12         
    13         
    14         <!-- 配置hibernate 的节本信息 -->
    15         <!-- hibernate 所使用的数据库方言 -->
    16         <!--<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>-->
    17    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    18         <!-- 执行操作时是否在控制台打印SQL  -->
    19         <property name="show_sql">true</property>
    20         
    21         <!-- 是否都SQL 进行格式化 -->
    22         <property name="format_sql">true</property>
    23         
    24         
    25         <!-- 指定自动生成数据表的策略 -->
    26         <property name="hbm2ddl.auto">update</property>
    27         
    28         <!-- 设置hibernate 的事务隔离级别 -->
    29         <property name="connection.isolation">2</property>
    30         
    31         
    32         <!-- 配置c3p0 -->
    33         <property name="hibernate.c3p0.max_size">10</property>
    34         <property name="hibernate.c3p0.min_size">5</property>
    35         <property name="c3p0.acquire_increment">2</property>
    36         <property name="c3p0.idle_test_period">2000</property>
    37         <property name="c3p0.timeout">2000</property>
    38         <property name="c3p0.max_statements">10</property>
    39     <!-- 1-1 映射 -->
    40         
    41         <mapping resource="com/jason/hibernate/entities/foreign/one2one/Manager.hbm.xml"/>
    42         <mapping resource="com/jason/hibernate/entities/foreign/one2one/Department.hbm.xml"/>
    43         
    44         
    45     </session-factory>
    46     
    47 </hibernate-configuration>

    HibernateTest

      1 package com.jason.hibernate.entities.foreign.one2one;
      2 
      3 import hibernate.helloworld.News;
      4 
      5 import java.util.Date;
      6 
      7 import org.hibernate.Hibernate;
      8 import org.hibernate.Session;
      9 import org.hibernate.SessionFactory;
     10 import org.hibernate.Transaction;
     11 import org.hibernate.cfg.Configuration;
     12 import org.hibernate.service.ServiceRegistry;
     13 import org.hibernate.service.ServiceRegistryBuilder;
     14 import org.junit.After;
     15 import org.junit.Before;
     16 import org.junit.Test;
     17 
     18 public class HibernateTest {
     19 
     20     private SessionFactory sessionFactory;
     21     private Session session;
     22     private Transaction transaction;
     23     @Test
     24     public void test() {
     25 
     26         // 1. 创建一个SessionFatory 对象
     27         SessionFactory sessionFactory = null;
     28 
     29         // 1) 创建Configuration 对象:对应hibernate 的基本配置信息 和 对象关系映射信息
     30         Configuration configuration = new Configuration().configure();
     31 
     32         // 2) 创建一个ServiceRegistry 对象:hibernate 4.x 新天添加的对象。
     33         // hibernate 的任何配置 和 服务都需要在该对象中注册后才有效
     34         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
     35                 .applySettings(configuration.getProperties())
     36                 .buildServiceRegistry();
     37 
     38         // sessionFactory = configuration.buildSessionFactory();
     39         sessionFactory = configuration.buildSessionFactory(serviceRegistry);
     40 
     41         // 2. 创建一个session 对象
     42         Session session = sessionFactory.openSession();
     43 
     44         // 3. 开启事物
     45         Transaction transaction = session.beginTransaction();
     46 
     47         // 4.执行保存操作
     48         News news = new News("java", "jason", new Date(
     49                 new java.util.Date().getTime()));
     50         session.save(news);
     51 
     52         // 5.提交事物
     53         transaction.commit();
     54         // 6.关闭session
     55         session.close();
     56         // 7.关闭SessionFactory 对象
     57         sessionFactory.close();
     58     }
     59 
     60     // 创建上述三个对象
     61     @Before
     62     public void init() {
     63         Configuration configuration = new Configuration().configure();
     64         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
     65                 .applySettings(configuration.getProperties())
     66                 .buildServiceRegistry();
     67 
     68         sessionFactory = configuration.buildSessionFactory(serviceRegistry);
     69 
     70         session = sessionFactory.openSession();
     71 
     72         transaction = session.beginTransaction();
     73     }
     74 
     75     // 关闭上述三个对象
     76     @After
     77     public void destroy() {
     78         transaction.commit();
     79         session.close();
     80         sessionFactory.close();
     81     }
     82 
     83     
     84     @Test
     85     public void testSave(){
     86         
     87         Department department = new Department();
     88         department.setDeptName("DEPT-AA");
     89         
     90         Manager manager = new Manager();
     91         manager.setMgrName("MGR-AA");
     92         
     93         
     94         //设定关联关系
     95         department.setMgr(manager);
     96         manager.setDept(department);
     97         
     98         //保存
     99         //建议先保存没有外键列的那个对象,这样会减少update 语句
    100         session.save(manager);
    101         session.save(department);
    102         
    103         
    104     }
    105     
    106     @Test
    107     public void testGet(){
    108         
    109         //1.默认情况下对关联属性使用懒加载
    110         Department dept = (Department) session.get(Department.class, 1);
    111         System.out.println(dept.getDeptName());
    112         
    113         
    114         //2.查询Manager 对象的连接条件应该是dept.manager_id = mgr.manager_id
    115         //而不因该是  manager0_.MGR_ID=department1_.DEPT_ID 
    116         Manager mgr = dept.getMgr();
    117         System.out.println(mgr);
    118     }
    119     
    120     
    121     @Test
    122     public void testGet2(){
    123         
    124         //在查询没有外键的实体对象,使用的是左外连接查询,一并查询出其关联的对象,并已经进行初始化
    125         Manager manager = (Manager) session.get(Manager.class, 1);
    126         System.out.println(manager);
    127         
    128         System.out.println(manager.getDept().getDeptName());
    129         
    130     }
    131     
    132     
    133     
    134     
    135 }
  • 相关阅读:
    python中线程 进程 协程
    python 部署lvs
    python中函数
    python监控cpu 硬盘 内存
    python文件操作
    python中字典
    零基础逆向工程34_Win32_08_线程控制_CONTEXT结构
    零基础逆向工程33_Win32_07_创建线程
    零基础逆向工程32_Win32_06_通用控件_VM_NOTIFY
    零基础逆向工程31_Win32_05_提取图标_修改标题
  • 原文地址:https://www.cnblogs.com/jasonHome/p/5933130.html
Copyright © 2020-2023  润新知