hibernate集合映射
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>
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!