Hibernate映射--集合篇

实体类
package cn.itcast.e_hbm_collection;

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

public class User {
private Integer id;
private String name;

//特别注意:使用集合属性时,一定要使用接口,不能申明为具体实现类,
//因为hibernate经过Session操作后,就变成了hibernate自己的实现类

private Set<String> addressSet = new HashSet<String>(); // Set集合
private List<String> addressList = new ArrayList<String>(); // List集合
private String[] addressArray; // 数组
private Map<String, String> addressMap = new HashMap<String, String>();
private List<String> addressBag = new ArrayList<String>();

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<String> getAddressSet() {
return addressSet;
}

public void setAddressSet(Set<String> addressSet) {
this.addressSet = addressSet;
}

public List<String> getAddressList() {
return addressList;
}

public void setAddressList(List<String> addressList) {
this.addressList = addressList;
}

public String[] getAddressArray() {
return addressArray;
}

public void setAddressArray(String[] addressArray) {
this.addressArray = addressArray;
}

public Map<String, String> getAddressMap() {
return addressMap;
}

public void setAddressMap(Map<String, String> addressMap) {
this.addressMap = addressMap;
}

public List<String> getAddressBag() {
return addressBag;
}

public void setAddressBag(List<String> addressBag) {
this.addressBag = addressBag;
}


}

*****************************************************************
2 操作类
package cn.itcast.e_hbm_collection;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

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

public class App {

private static SessionFactory sessionFactory = new Configuration()//
//.configure()//
//.addClass(User.class)// 添加Hibernate实体类(加载对应的映射文件)
//.buildSessionFactory();

cfg.configure("hibernate.cfg.xml"); // 读取指定的主配置文件
sessionFactory = cfg.buildSessionFactory(); // 根据生成Session工厂

@Test
public void testSave() throws Exception {
Session session = sessionFactory.openSession();
session.beginTransaction();
// --------------------------------------------

// 构建对象
User user = new User();
user.setName("张天");
// >> Set集合
user.setAddressSet(new TreeSet<String>()); // 当设置了sort属性时,就要使用SortedSet类型
user.getAddressSet().add("2御富科贸园");
user.getAddressSet().add("1棠东东路");
// >> List集合
user.getAddressList().add("御富科贸园");
user.getAddressList().add("棠东东路");
user.getAddressList().add("棠东东路");
// >> 数组
user.setAddressArray(new String[] { "御富科贸园", "棠东东路" });
// >> Map集合
user.getAddressMap().put("公司", "御富科贸园");
user.getAddressMap().put("家庭", "棠东东路");
// >> Bag集合
user.getAddressBag().add("御富科贸园");
user.getAddressBag().add("棠东东路");
user.getAddressBag().add("棠东东路");

// 保存
session.save(user);

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

@Test
public void testGet() throws Exception {
Session session = sessionFactory.openSession();
session.beginTransaction();
// --------------------------------------------

// 获取数据
User user = (User) session.get(User.class, 9);

// 显示信息
System.out.println(user.getName());
System.out.println(user.getAddressSet());
System.out.println(user.getAddressList());
System.out.println(Arrays.toString(user.getAddressArray()));
System.out.println(user.getAddressMap());
System.out.println(user.getAddressBag());

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


*******************************************
3 映射文件配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.e_hbm_collection">

<class name="User" table="user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"/>

<!-- addressSet属性,Set集合
table属性:集合表的名称
key子元素:集合外键的列名
element子元素:存放集合元素的列的信息
sort属性:"unsorted|natural|comparatorClass"
默认为:unsorted
order-by属性:写的是order by 子句,是SQL语句,是操作的集合表。
这是在查询数据时指定orderby子句。
-->
<set name="addressSet" table="user_addressSet" order-by="address ASC">
<key column="userId"></key>
<element type="string" column="address"></element>
</set>

<!-- addressList属性,List集合
list-index:用于存放索引的列
-->
<list name="addressList" table="user_addressList">
<key column="userId"></key>
<list-index column="idx"></list-index>
<element type="string" column="address"></element>
</list>

<!-- addressArray属性,数组。与List的映射基本一致 -->
<array name="addressArray" table="user_addressArray">
<key column="userId"></key>
<list-index column="idx"></list-index>
<element type="string" column="address"></element>
</array>

<!-- addressMap属性,Map集合 -->
<map name="addressMap" table="user_addressMap">
<key column="userId"></key>
<map-key type="string" column="key_"></map-key>
<element type="string" column="address"></element>
</map>

<!-- addressBag属性,Bag集合:无序,可重复。与Set集合的映射基本一致 -->
<bag name="addressBag" table="user_addressBag">
<key column="userId"></key>
<element type="string" column="address"></element>
</bag>

</class>

</hibernate-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值