美文网首页
hibernate详解(十四)组件映射

hibernate详解(十四)组件映射

作者: 秀逼 | 来源:发表于2017-11-07 21:16 被阅读0次

    组件映射

    • 组建映射是指在一个实体类中有一个属性为自定义的类,但是自定义的类中并没有oid,在数据库中也没有对应这个类的表。
      表结构:
    /* 组件映射 */
    CREATE TABLE t_customer(
        id NUMBER(10) PRIMARY KEY,
        name VARCHAR2(20),
        province VARCHAR2(20),
        city VARCHAR2(20),
        street VARCHAR2(20)
    );
    

    Customner类

    package com.iotek.basic.component.pojo;
    
    import java.io.Serializable;
    
    public class Customer implements Serializable {
    
        private static final long serialVersionUID = 5660598920023039135L;
        private Long id;
        private String name;
        private Address address;
        
        public Customer() {}
    
        public Customer(Long id, String name, Address address) {
            super();
            this.id = id;
            this.name = name;
            this.address = address;
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
        }
        
        
    }
    
    

    组件类 Address.java

    package com.iotek.basic.component.pojo;
    
    import java.io.Serializable;
    
    public class Address implements Serializable {
    
        private static final long serialVersionUID = 1921592338080264702L;
        private String province;
        private String city;
        private String street;
        private Customer customer;
        
        public Address() {}
    
        public Address(String province, String city, String street, Customer customer) {
            super();
            this.province = province;
            this.city = city;
            this.street = street;
            this.customer = customer;
        }
    
        public String getProvince() {
            return province;
        }
    
        public void setProvince(String province) {
            this.province = province;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getStreet() {
            return street;
        }
    
        public void setStreet(String street) {
            this.street = street;
        }
    
        public Customer getCustomer() {
            return customer;
        }
    
        public void setCustomer(Customer customer) {
            this.customer = customer;
        }
    
        @Override
        public String toString() {
            return "Address [province=" + province + ", city=" + city + ", street=" + street + ", customer=" + customer
                    + "]";
        }
        
        
    }
    
    

    映射文件:

    <?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.iotek.basic.component.pojo">
        <class name="Customer" table="T_CUSTOMER">
            <id name="id" column="ID" type="long">
                <generator class="sequence">
                    <param name="sequence">t_customer_seq</param>
                </generator>
            </id>
            
            <property name="name" type="string" column="NAME"/>
            
            <component name="address" class="Address">
                <parent name="customer"/>
                <property name="province" />
                <property name="city" />
                <property name="street" />
            </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>
            <!-- property 元素用于配置Hibernate中的属性 键:值 -->
            
              <!-- hibernate.connection.driver_class : 连接数据库的驱动  -->
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            
              <!-- hibernate.connection.url : 连接数据库的地址,路径 -->
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
            
              <!-- hibernate.connection.username : 连接数据库的用户名 -->
            <property name="connection.username">guest</property>
            
              <!-- hibernate.connection.password : 连接数据库的密码 -->
            <property name="connection.password">guest</property>
            
              <!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->
            <property name="show_sql">true</property>
            
              <!-- 数据库方言配置 org.hibernate.dialect.Oracle10gDialect (选择最短的)-->
            <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
            
            <!-- 配置连接池最大连接数-->
            <property name="hibernate.c3p0.max_size">20</property>
            
            <!-- 配置连接池最小连接数-->
            <property name="hibernate.c3p0.min_size">2</property>
            
            <!-- 配置连接池超时时间-->
            <property name="hibernate.c3p0.timeout">5000</property>
            
            <!-- 配置连接池最大数量-->!
    
            <property name="hibernate.c3p0.max_statements">100</property>
            
            <!-- 配置连接池空闲检索时间-->
            <property name="hibernate.c3p0.idle_test_period">150</property>
            
            <!-- 配置连接池自增数量-->
            <property name="hibernate.c3p0.acquire_increment">2</property>
            
            <!-- 配置连接池验证-->
            <property name="hibernate.c3p0.validate">false</property>
            
            <mapping resource="com/iotek/basic/pojo/Student.hbm.xml"/>
            <mapping resource="com/iotek/basic/inheritance/pojo/concrete.hbm.xml"/>
            <mapping resource="com/iotek/basic/component/pojo/Customer.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
    

    看代码就可以理解了。
    总结:


    1.png

    相关文章

      网友评论

          本文标题:hibernate详解(十四)组件映射

          本文链接:https://www.haomeiwen.com/subject/kpqsmxtx.html