Hibernate进阶之映射集合关系

来源:互联网 发布:计算机c语言基础知识 编辑:程序博客网 时间:2024/04/27 22:05

上一篇介绍了Hibernate的hql,接下来认识Hibernate进阶之映射集合关系。

下面先对实例做一个总结:

映射集合关系:

(1)List<String>集合/Set<Integer>集合/Map<String,Integer>集合

 (2)保存用户
  (3)更新用户

    每种集合(List/Set/Map)对应一张表,通过外健关联

第一步:定义一个实体类User:

package example.list_map;import java.util.LinkedHashMap;import java.util.LinkedHashSet;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;public class User {private Integer id;private String name;//用过的号码private Set<String> telSet=new LinkedHashSet<String>();//去过的城市private List<String> cityList=new LinkedList<String>();//号码所对应的城市private Map<String,String> map=new LinkedHashMap<String, String>();public User() {}public User(String name) {super();this.name = name;}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> getTelSet() {return telSet;}public void setTelSet(Set<String> telSet) {this.telSet = telSet;}public List<String> getCityList() {return cityList;}public void setCityList(List<String> cityList) {this.cityList = cityList;}public Map<String, String> getMap() {return map;}public void setMap(Map<String, String> map) {this.map = map;}}

第二步:写配置文件User.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="example.list_map"><class name="User" table="USERS"><id name="id" column="id" type="int"><generator class="native"></generator></id><property name="name" column="name" type="string"></property><!-- set集合映射 --> <set name="telSet" table="TELS"> <key column="users_id"></key> <!-- 这里的type必须写类型,否则会报错 --> <element column="TEL" type="string"></element> </set> <!-- list集合的映射 --> <list name="cityList" table="CITYS"> <key column="users_id"></key> <list-index column="IDX"></list-index> <element column="city" type="string"></element> </list> <map name="map" table="tel_city"> <key column="user_id"></key> <map-key type="string" column="tel"></map-key> <element type="string" column="city"></element> </map></class></hibernate-mapping>

第三步:写测试类UserDAo:

package example.list_map;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import example.utils.HibernateUtils;public class UserDao {/** *  */@Testpublic void test01(){User user=new User("小明");user.getTelSet().add("123456");user.getTelSet().add("854321");user.getTelSet().add("764343");user.getCityList().add("广州");user.getCityList().add("深圳");user.getCityList().add("珠海");user.getMap().put("123456", "广州");user.getMap().put("854321", "深圳");user.getMap().put("764343", "珠海");Session session=HibernateUtils.getSession();Transaction t= session.getTransaction();try{t.begin();session.save(user);t.commit();}catch (Exception e) {// TODO: handle exceptiont.rollback();e.printStackTrace();}finally{HibernateUtils.closeSession();}}}



0 0
原创粉丝点击