hibernate基础13:关联映射之组件映射

1、Java实体bean类

package com.project.pojo;

public class Link {
    private String telephone;
    private String qq;
    private String email;
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getQq() {
        return qq;
    }
    public void setQq(String qq) {
        this.qq = qq;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
}
package com.project.pojo;

public class Employee {
    private int id;
    private String name;
    private int age;
    private Link links;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Link getLinks() {
        return links;
    }
    public void setLinks(Link links) {
        this.links = links;
    }
    
    
}

2、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="com.project.pojo">
    <class name="Employee" table="t_employee">
        <id name="id" column="id" type="int">
            <generator class="native"></generator>
        </id>
        <property name="name" column="name" type="java.lang.String" />
        <property name="age" column="age" type="int" />
        <component name="links" class="Link">
            <property name="telephone" />
            <property name="qq" />
            <property name="email" />
        </component>
    </class>

</hibernate-mapping>

3、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>
        <!-- 1、数据库连接信息 -->
        <!-- 指定数据方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://192.168.1.59:3306/hibernate?characterEncoding=UTF8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>
        
        <!-- 2、通用配置信息 -->
        <!-- 打印sql语句 -->
        <property name="show_sql">true</property>
        <!-- 格式化sql语句 -->
        <property name="format_sql">true</property>
        
        <!-- 映射文件信息 -->
        <mapping resource="com/project/pojo/Employee.hbm.xml" />
    </session-factory >
</hibernate-configuration>

4、测试

package com.project.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.project.pojo.Employee;
import com.project.pojo.Link;
import com.project.util.HibernateUtil;

public class HibernateTest {
    Session session = null;
    Transaction ts = null;
    
    @Before
    public void setUp(){
        session = HibernateUtil.getSession();
        ts = session.beginTransaction();
    }
    
    @After
    public void tearDown(){
        HibernateUtil.closeSession();
    }
    
    @Test
    public void testCreateDB(){
        Configuration cfg = new Configuration().configure();
        //使得hibernate映射信息转换为数据库识别的dll语言
        SchemaExport se = new SchemaExport(cfg);
        //第一个参数:是否打印dll语句
        //第二个参数:是否将dll到数据库中执行
        se.create(true, true);
    }
    
    @Test
    public void testInit(){
        try {
            Link link = new Link();
            link.setTelephone("13022222222");
            link.setQq("111111111");
            link.setEmail("123@qq.com");
            Employee e = new Employee();
            e.setName("张三");
            e.setAge(23);
            e.setLinks(link);
            session.save(e);
            ts.commit();
        } catch (Exception e) {
            if(ts!=null)ts.rollback();
            e.printStackTrace();
        }
    }
    @Test
    public void testSelect(){
        Employee e = (Employee) session.get(Employee.class, 1);
        System.out.println(e.getName()+"\t"+e.getAge());
        System.out.println(e.getLinks().getTelephone()+"\t"+e.getLinks().getQq()+"\t"+e.getLinks().getEmail());
        
    }
    
}

 

hibernate基础13:关联映射之组件映射

上一篇:springboot情操陶冶-SpringApplication(二)


下一篇:判断APP是否已安装