• hibernate组件映射


    类组合关系的映射,也叫做组件映射!

    注意:组件类和被包含的组件类,共同映射到一张表!

    需求: 汽车与车轮

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <!-- 
        组件映射
     -->
    <hibernate-mapping package="loaderman.d_component">
        
        <class name="Car" table="t_car">
            <id name="id">
                <generator class="native"></generator>
            </id>    
            <property name="name" length="20"></property>
            
            <!-- 组件映射 -->
            <component name="wheel">
                <property name="size"></property>
                <property name="count"></property>
            </component>
            
                         
        </class>
        
    
    </hibernate-mapping>
    package loaderman.d_component;
    
    import org.hibernate.Hibernate;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.classic.Session;
    import org.junit.Test;
    
    public class App {
    
        private static SessionFactory sf;
        static {
            sf = new Configuration()
                    .configure()
                    .addClass(Car.class)
                    .buildSessionFactory();
        }
    
        @Test
        public void getSave() {
    
            Session session = sf.openSession();
            session.beginTransaction();
    
            // 轮子
            Wheel wheel = new Wheel();
            wheel.setSize(38);
            wheel.setCount(4);
            // 汽车
            Car car = new Car();
            car.setName("BMW");
            car.setWheel(wheel);
    
            // 保存
            session.save(car);
    
            session.getTransaction().commit();
            session.close();
    
        }
    
    }
    package loaderman.d_component;
    
    public class Car {
    
        private int id;
        private String name;
        // 车轮
        private Wheel wheel;
        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 Wheel getWheel() {
            return wheel;
        }
        public void setWheel(Wheel wheel) {
            this.wheel = wheel;
        }
        
    }
    package loaderman.d_component;
    // 车轮
    public class Wheel {
    
        private int count;
        private int size;
        public int getCount() {
            return count;
        }
        public void setCount(int count) {
            this.count = count;
        }
        public int getSize() {
            return size;
        }
        public void setSize(int size) {
            this.size = size;
        }
        
        
    }

  • 相关阅读:
    Leetcode Best Time to Buy and Sell Stock II
    Leetcode Shortest Word Distance III
    Leetcode Shortest Word Distance II
    Leetcode Shortest Word Distance
    牛客多校第四场 J Free 最短路
    牛客多校第四场 A meeting 树的半径
    hdu多校第二场1008(hdu6598) Harmonious Army 最小割
    hdu多校第二场1011 (hdu6601) Keen On Everything But Triangle 主席树
    hdu多校第二场 1010 (hdu6600)Just Skip This Problem
    hdu多校第二场 1005 (hdu6595) Everything Is Generated In Equal Probability
  • 原文地址:https://www.cnblogs.com/loaderman/p/10038001.html
Copyright © 2020-2023  润新知