基本的需求是:用户购买东西,有多个地址。
这篇文章关注的是String类型的Set、List和Map数据如何与数据库中的表建立映射关系,在下一篇文章中,会String类型进一步修改为用户自定义的类型。这篇文章的重点是学习.hbm.xml中映射的写法。
User.java
package com.rk.hibernate.f_collections; import java.util.List; import java.util.Map; import java.util.Set; public class User { private int uid; private String username; private Set<String> addressSet; private List<String> addressList; private Map<String, String> addressMap; 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 Set<String> getAddressSet() { return addressSet; } public void setAddressSet(Set<String> addressSet) { this.addressSet = addressSet; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
在User.java中,有Set、List和Map三种类型的数据,它的基础类型是String
private Set<String> addressSet; private List<String> addressList; private Map<String, String> addressMap;
User.hbm.xml,这里是学习的重点:Set、List和Map三种类型是如何进行映射的
<?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.rk.hibernate.f_collections" auto-import="true"> <class name="User" table="T_Users"> <id name="uid" column="id"> <generator class="native"></generator> </id> <property name="username" column="name" type="string"></property> <!-- set集合属性的映射 name 指定要映射的set集合的属性 table 集合属性要映射到的表 key 指定集合表(t_address)的外键字段 element 指定集合表的其他字段 type 元素类型,一定要指定 --> <set name="addressSet" table="T_addressSet"> <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>
App.java,这里主要是写代码进行测试
package com.rk.hibernate.f_collections; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; 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() { Session session = sf.openSession(); session.beginTransaction(); // 用户对象 User u = new User(); u.setUsername("小刚"); // 添加地址 Set<String> addressSet = new HashSet<String>(); addressSet.add("北京"); addressSet.add("上海"); u.setAddressSet(addressSet); // 保存 session.save(u); session.getTransaction().commit(); session.close(); System.out.println("执行结束!"); } // 保存list @Test public void testSaveList() { Session session = sf.openSession(); session.beginTransaction(); // 用户对象 User u = new User(); u.setUsername("小蓝"); // 添加地址 List<String> list = new ArrayList<String>(); list.add("石家庄"); list.add("保定"); u.setAddressList(list); // 保存 session.save(u); session.getTransaction().commit(); session.close(); System.out.println("执行结束!"); } // 保存map @Test public void testSaveMap() { Session session = sf.openSession(); session.beginTransaction(); // 用户对象 User u = new User(); u.setUsername("小花"); // 添加地址 Map<String, String> map = new HashMap<String, String>(); map.put("京", "北京"); map.put("冀", "河北"); u.setAddressMap(map); // 保存 session.save(u); session.getTransaction().commit(); session.close(); System.out.println("执行结束!"); } // 获取 @Test public void testGetList() { Session session = sf.openSession(); session.beginTransaction(); // 获取 User u = (User) session.get(User.class, 5);// 及时加载 System.out.println(u.getUid()); System.out.println(u.getUsername()); // 当查询用户,同时可以获取用户关联的list集合的数据 (因为有正确映射) // 当使用到集合数据的使用,才向数据库发送执行的sql语句 (懒加载) System.out.println(u.getAddressList()); session.getTransaction().commit(); session.close(); System.out.println("执行结束!"); } }
时间: 2024-10-12 10:37:33