hibernate详解(十五)集合映射Set

2017-11-07  本文已影响0人  秀逼

set集合的映射:
SQL语句:

/* set */
CREATE TABLE items_set(
    id NUMBER(3) PRIMARY KEY,
    name VARCHAR2(20)
);

CREATE TABLE images_set(
    filename VARCHAR2(20),
    items_id NUMBER(10) REFERENCES items_set(id),
    PRIMARY KEY(items_id,filename)
);

ItemsSet类:

package com.iotek.basic.collection.set;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class ItemsSet implements Serializable{
    private Long id;
    private String name;
    private Set<String> images = new HashSet<String>();
    
    public ItemsSet() {}

    public ItemsSet(Long id, String name, Set<String> images) {
        super();
        this.id = id;
        this.name = name;
        this.images = images;
    }

    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 Set<String> getImages() {
        return images;
    }

    public void setImages(Set<String> images) {
        this.images = images;
    }

    @Override
    public String toString() {
        return "ItemsSet [id=" + id + ", name=" + name + ", images=" + images.toString() + "]";
    }
    
    
    
}

ItemsSet.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.iotek.basic.collection.set">
    <class name="ItemsSet" table="ITEMS_SET">
        <id name="id" column="ID" type="long">
            <generator class="sequence">
                <param name="sequence">t_itemsset_seq</param>
            </generator>
        </id>
        
        <property name="name" type="string" column="NAME"/>
        <set name="images" table="IMAGES_SET" cascade="all">
            <key column="ITEMS_ID" />
            <element column="FILENAME" type="string" not-null="true"></element>
        </set>
    </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"/>
        <mapping resource="com/iotek/basic/collection/set/ItemsSet.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

测试类: ItemTest.java

package com.iotek.basic.collection.set;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.iotek.basic.common.HibernateSessionFactory;

public class ItemsTest {

    public static void main(String[] args) {
        ItemsSet itemsSet = new ItemsSet();
        itemsSet.setName("zhangsan");
        
        Set<String> images = new HashSet<String>();
        images.add("pic/a.jpg");
        images.add("pic/b.jpg");
        images.add("pic/c.jpg");
        
        itemsSet.setImages(images);
        Session session = HibernateSessionFactory.getSession();
        Transaction transaction = null;
        
        try {
            
            transaction = session.beginTransaction();
            // session.save(itemsSet);
            ItemsSet item = (ItemsSet)session.get(ItemsSet.class, 63L);
            System.out.println(item);
            transaction.commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        }
    }

}

直接看代码就可以理解了,这里耀说明的是,这样配置的是set中存放的是hibernate内置的值类型,如果set中存放的是实例类型,则按照以前讲的one-to-many进行配置,而不是现在所使用的element进行配置。

2.png 3.png
上一篇 下一篇

猜你喜欢

热点阅读