Hibernate组件映射

北慕城南 提交于 2020-03-23 12:54:18

组件的映射

建立关系数据模型的一个重要原则是在不会导致数据冗余的前提下,尽可能减少数据库表中的数目及表之间的外键参照关系。以员工信息为例,员工信息中有员工的家庭地址信息,如果把地址信息单独放在一张表中,然后建立员工信息表和地址信息表之间的外键关系,当每次查询员工信息时,都需要建立这两个表的连接。建立表的连接是很耗时的操作,为了提高数据库运行性能,可以把这两张表的信息整合在一张员工信息表Empinfo中

提示:

 

HibernateUtil工具类(用于获取session和关闭session)

package cn.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    //初始化一个ThreadLocal对象,有get和set方法
    private static final ThreadLocal<Session> sessionTL=new ThreadLocal<Session>();
    
    private static Configuration configuration;
    
    private final static SessionFactory sessionFactory;
    static{
        
        configuration=new Configuration().configure();
        sessionFactory=configuration.buildSessionFactory();
    }
    //获得session对象
    public static Session currentSession() {
        //sessionTL的get方法根据当前线程返回其对应的线程内部变量,即Session对象,多线程情况下共享数据库连接是不安全的。
        //ThreadLocal保证了每个线程都有自己的session对象
        Session session=(Session)sessionTL.get();
        if (session==null) {
            session=sessionFactory.openSession();
            sessionTL.set(session);
        }
        
        return session;
    }
    //关闭session对象
    public static void closeSession() {
        Session session=(Session)sessionTL.get();
        sessionTL.set(null);
        session.close();
    }

}

EmpHomeAddress实体类:

package cn.entity;
/*
 * 地址信息类
 */
public class EmpHomeAddress {
    private String ehomestreet;
    private String ehomecity;
    private String ehomeprovince;
    private String ehomezipcode;
    private EmpInfo empinfo;
    public String getEhomestreet() {
        return ehomestreet;
    }
    public void setEhomestreet(String ehomestreet) {
        this.ehomestreet = ehomestreet;
    }
    public String getEhomecity() {
        return ehomecity;
    }
    public void setEhomecity(String ehomecity) {
        this.ehomecity = ehomecity;
    }
    public String getEhomeprovince() {
        return ehomeprovince;
    }
    public void setEhomeprovince(String ehomeprovince) {
        this.ehomeprovince = ehomeprovince;
    }
    public String getEhomezipcode() {
        return ehomezipcode;
    }
    public void setEhomezipcode(String ehomezipcode) {
        this.ehomezipcode = ehomezipcode;
    }
    public EmpInfo getEmpinfo() {
        return empinfo;
    }
    public void setEmpinfo(EmpInfo empinfo) {
        this.empinfo = empinfo;
    }

}

EmpInfo实体类:

package cn.entity;
/*
 * 员工类
 */
public class EmpInfo {
    private Integer eid;
    private String ename;
    private EmpHomeAddress ehome;
    public Integer getEid() {
        return eid;
    }
    public void setEid(Integer eid) {
        this.eid = eid;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public EmpHomeAddress getEhome() {
        return ehome;
    }
    public void setEhome(EmpHomeAddress ehome) {
        this.ehome = ehome;
    }


}

EmpInfo.hbm.xml映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.entity">
<class name="EmpInfo" table="empinfo">
<id name="eid" column="eid">
<generator class="native"></generator>
</id>
 <property name="ename" column="ENAME" type="string"></property>
<component name="ehome" class="EmpHomeAddress">
<parent name="empinfo"/>
<property name="ehomestreet" column="EHOMESTREET" type="string"></property>
<property name="ehomecity" column="EHOMECITY" type="string"></property>
<property name="ehomeprovince" column="EHOMEPROVINCE" type="string"></property>
<property name="ehomezipcode" column="EHOMEZIPCODE" type="string"></property>
</component>
</class>
</hibernate-mapping>

hibernate.cfg.xml大配置文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings 数据库连接设置-->
        <!-- 驱动类 -->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <!-- url地址 -->
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">system</property>
        <property name="connection.password">1</property>

        <!-- SQL dialect  (SQL 方言) -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>


        <!-- Echo all executed SQL to stdout 在控制台打印后台的SQL语句 -->
        <property name="show_sql">true</property>
        
        
        <!-- 格式化显示SQL -->
        <property name="format_sql">true</property>
        

        <!-- Drop and re-create the database schema on startup -->
          <property name="hbm2ddl.auto">update</property>  
          
         <!-- 支持getCurrentSession方法 -->
         <!--  <property name="current_session_context_class">thread</property> -->
        
              <!-- 关联小配置 -->
         <mapping resource="cn/entity/EmpInfo.hbm.xml" />
         
    
    </session-factory>
</hibernate-configuration>

 

测试类:

public class Tests {
    Session session;
    Transaction tx;
    

    @Before
    public void initDate(){
        session = HibernateUtil.getSession();
         tx= session.beginTransaction();
    }
    
    
    @After
     public void afterTest(){
         tx.commit();
         HibernateUtil.closeSession();
     }
    
    
    /**
     * 组件的映射测试
     */

    @Test
    public void getTest(){
        //创建一个员工对象
        EmpInfo emp=new EmpInfo();
        emp.setEname("火狐1");
        
        //创建一个员工地址对象
        EmpHomeAddress address=new EmpHomeAddress();
        address.setEhomecity("北京");
        address.setEhomeprovince("北京");
        address.setEhomestreet("五道口");
        address.setEhomezipcode("100000");
        address.setEmpinfo(emp);
        emp.setEhome(address);
        session.save(emp);
        System.out.println("ok");
    }
    
    /**
     * 查询
     */
    @Test
    public void selectTest(){
        EmpInfo emp=(EmpInfo)session.load(EmpInfo.class, 1);
        System.out.println(emp.getEname());
        
    }

}

 

结果:

数据库:

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!