首页 > 代码库 > hibernate之List&Set批量插入

hibernate之List&Set批量插入

技术分享
package com.chris.pojo;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class User {
    private Integer userid;
    private String name;
    private List<String> address_list;
    private Set<String> address_set;
    
    
    public Set<String> getAddress_set() {
        return address_set;
    }
    public void setAddress_set(Set<String> address_set) {
        this.address_set = address_set;
    }
    public List<String> getAddress_list() {
        return address_list;
    }
    public void setAddress_list(List<String> address_list) {
        this.address_list = address_list;
    }
    public Integer getUserid() {
        return userid;
    }
    public void setUserid(Integer userid) {
        this.userid = userid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
User.java
技术分享
<?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="com.chris.pojo">
    <class name="User" table="user_table">
        <id name="userid">
            <generator class="native"></generator>
        </id>
        <!-- set表示需要映射的属性是一个set集合 table:表示集合对应的表明 key:表示外键列,它会自动关联到test_user表中的主键 
            element:表示集合表中的元素 -->
            
        <set name="address_set" table="address_table4" > 
            <key column="user_id"></key> 
            <element type="string" column="address"></element> </set> 
    <!--     -->
        <list name="address_list" table="address_list">
            <key column="user_id"></key>
            <list-index column="idx" />
            <element type="string" column="address"></element>
        </list>
            
        <!-- map表示需要映射的属性是一个Map集合 table:表示集合对应的表明 key:表示外键列,它会自动关联到user2表中的主键 map-key 
            map集合的key element:map集合的value -->
    </class>
</hibernate-mapping>
User.hbm.xml
技术分享
package com.chris.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.map.HashedMap;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.chris.pojo.User;
import com.chris.util.HibernateUtil;

public class TestCollection {
    @Test
    public void testListAdd(){
        User user=new User();
        List<String> address=new ArrayList<String>();
        address.add("忠孝东路EEQQAA");
        address.add("剩男天桥EEQQAA");
        address.add("华育网科技EEQQAA");
        user.setAddress_list(address);
        Session session=HibernateUtil.getSession();
        Transaction tr=session.beginTransaction();
        session.save(user);
        tr.commit();
        session.close();
    }
    @Test
    public void testSetAdd(){
        User user=new User();
        Set<String> address2=new HashSet<String>();
        address2.add("忠孝东路EEQQAA");
        address2.add("剩男天桥EEQQAA");
        address2.add("华育网科技EEQQAA");
        user.setAddress_set(address2);
        Session session=HibernateUtil.getSession();
        Transaction tr=session.beginTransaction();
        session.save(user);
        tr.commit();
        session.close();
    }
}
TestCollection.java
技术分享
package com.chris.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    public static SessionFactory sf=null;
    static{
        Configuration  cf=new Configuration().configure();
        sf=cf.buildSessionFactory();
    }
    
    public static Session getSession(){
        return sf.openSession();
    }

}
HibernateUtil.java
技术分享
<!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 name="foo">
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql:///hibernate_20120328</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="hibernate.connection.password">hyy</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">false</property>
        <!-- 
            create:先删除,再创建
            update:如果表不存在就创建,不一样就更新,一样就什么都不做。
            create-drop:初始化时创建表,SessionFactory执行close()时删除表。
            validate:验证表结构是否一致,如果不一致,就抛异常。
         -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 
            设置默认的事务隔离级别:
            隔离级别        对应的整数表示
            READ UNCOMMITED    1
            READ COMMITED    2
            REPEATABLE READ    4
            SERIALIZEABLE    8
         -->
        <property name="connection.isolation">2</property>
    
    <mapping resource="com/chris/pojo/User.hbm.xml" />


</session-factory>
</hibernate-configuration>
hibernate.cfg.xml

 

hibernate之List&Set批量插入