• 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()+"	"+e.getAge());
            System.out.println(e.getLinks().getTelephone()+"	"+e.getLinks().getQq()+"	"+e.getLinks().getEmail());
            
        }
        
    }
  • 相关阅读:
    [luoguP1196] 银河英雄传说(并查集)
    [luoguP1111] 修复公路(并查集)
    [luoguP2387] 魔法森林(LCT + 并查集)
    [BZOJ2594] [Wc2006]水管局长数据加强版(LCT + kruskal + 离线)
    [luoguP2045] 方格取数加强版(最小费用最大流)
    [BZOJ2843] 极地旅行社(LCT)
    [luoguP3690] 【模板】Link Cut Tree
    [luoguP3203][HNOI2010]BOUNCE 弹飞绵羊(LCT)
    [luoguP1901] 发射站(单调栈)
    codeforces Round#381 div2
  • 原文地址:https://www.cnblogs.com/chai-blogs/p/12951551.html
Copyright © 2020-2023  润新知