码迷,mamicode.com
首页 > Web开发 > 详细

hibernate集合映射

时间:2018-11-29 15:11:04      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:doctype   测试   string   外键   util   ppi   his   pen   configure   

package loaderman.a_collection;

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

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(User.class)   // 测试时候使用
                .buildSessionFactory();
    }

    // 保存set
    @Test
    public void testSaveSet() throws Exception {
        Session session = sf.openSession();
        session.beginTransaction();

        //-- 保存
        Set<String> addressSet = new HashSet<String>();
        addressSet.add("广州");
        addressSet.add("深圳");
        // 用户对象
        User user = new User();
        user.setUserName("Jack");
        user.setAddress(addressSet);

        // 保存
        session.save(user);

        session.getTransaction().commit();
        session.close();
    }

    // 保存list/map
    @Test
    public void testSaveList() throws Exception {
        Session session = sf.openSession();
        session.beginTransaction();
        User user = new User();
        user.setUserName("Tom");
//        // 用户对象  --  list
//        user.getAddressList().add("广州");
//        user.getAddressList().add("深圳");
//        // 保存
//        session.save(user);

        // 用户对象  --  Map
        user.getAddressMap().put("A0001", "广州");
        user.getAddressMap().put("A0002", "深圳");

        // 保存
        session.save(user);

        session.getTransaction().commit();
        session.close();
    }


    // 获取
    @Test
    public void testGet() throws Exception {
        Session session = sf.openSession();
        session.beginTransaction();

        // 获取
        User user = (User) session.get(User.class, 3); // 及时加载
        System.out.println(user.getUserId());
        System.out.println(user.getUserName());

        // 当查询用户,同时可以获取用户关联的list集合的数据 (因为有正确映射)
        // 当使用到集合数据的使用,才向数据库发送执行的sql语句  (懒加载)
        System.out.println(user.getAddressList());

        session.getTransaction().commit();
        session.close();
    }
}
package loaderman.a_collection;

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

/// javabean设计
public class User {

    private int userId;
    private String userName;
    // 一个用户,对应的多个地址
    private Set<String> address;
    private List<String> addressList = new ArrayList<String>();
    //private String[] addressArray; // 映射方式和list一样     <array name=""></array>
    private Map<String,String> addressMap = new HashMap<String, String>();



    public Map<String, String> getAddressMap() {
        return addressMap;
    }
    public void setAddressMap(Map<String, String> addressMap) {
        this.addressMap = addressMap;
    }
    public List<String> getAddressList() {
        return addressList;
    }
    public void setAddressList(List<String> addressList) {
        this.addressList = addressList;
    }
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public Set<String> getAddress() {
        return address;
    }
    public void setAddress(Set<String> address) {
        this.address = address;
    }


}
<?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.a_collection">
    
    <class name="User" table="t_user">
        <id name="userId" column="id">
            <generator class="native"></generator>
        </id>    
        <property name="userName"></property>
        
        <!-- 
            set集合属性的映射
                name 指定要映射的set集合的属性
                table 集合属性要映射到的表
                key  指定集合表(t_address)的外键字段
                element 指定集合表的其他字段
                    type 元素类型,一定要指定
         -->
         <set name="address" table="t_address">
             <key column="uid"></key>
             <element column="address" type="string"></element>
         </set>
         
         <!-- 
             list集合映射
                 list-index  指定的是排序列的名称 (因为要保证list集合的有序)
          -->
          <list name="addressList" table="t_addressList">
                <key column="uid"></key>
                <list-index column="idx"></list-index>
                <element column="address" type="string"></element>
          </list>
          
          <!-- 
              map集合的映射
                  key  指定外键字段
                  map-key 指定map的key 
                  element  指定map的value
           -->
          <map name="addressMap" table="t_addressMap">
              <key column="uid"></key>
              <map-key column="shortName" type="string" ></map-key>
              <element column="address" type="string" ></element>
          </map>
          
         
    </class>
    

</hibernate-mapping>

 

hibernate集合映射

标签:doctype   测试   string   外键   util   ppi   his   pen   configure   

原文地址:https://www.cnblogs.com/loaderman/p/10037284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!