Hibernate集合的配置
Hibernate集合的配置
package org.fkjava.bean; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; /** * Hibernate 中 集合和数组类型数据操作 * * @author hanfeili www.fkjava.org */ public class Person { private Integer id; private String name; private int password; private Date birthday; //定义数组和其他常用集合,并提供get/set方法 private String[] my_array; private List my_list; private Map my_map; private Set my_set; public Person() { } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", password=" + password + ", birthday=" + birthday + "]"; } public String[] getMy_array() { return my_array; } public void setMy_array(String[] my_array) { this.my_array = my_array; } public List getMy_list() { return my_list; } public void setMy_list(List my_list) { this.my_list = my_list; } public Map getMy_map() { return my_map; } public void setMy_map(Map my_map) { this.my_map = my_map; } public Set getMy_set() { return my_set; } public void setMy_set(Set my_set) { this.my_set = my_set; } 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 int getPassword() { return password; } public void setPassword(int password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- default-access="field" 属性的访问策略 field 表示持久化类属性通过反射方式进行操作 property 表示持久化类属性通过getting/setting方法进行操作 (默认值) --> -<hibernate-mapping package="bean"> -<class table="t_person" name="Person"> -<id name="id"> <generator class="identity"/> </id> <property name="name" column="t_name"/> <property name="password" column="t_pass"/> <property name="birthday" type="timestamp"/> <!-- 效率底下,通常不建议使用 --> -<array table="t_array" name="my_array"> <key column="id"/> <!-- 指定t_array表的数据与与t_person表的数据的关系 --> <list-index column="indexs"/> <!-- 因为array是顺序的,使用需要保存数据的顺序值 --> <element column="value" type="java.lang.String"/> <!-- 指定数组中的元素类型和存储在什么列中 --> </array> -<list name="my_list"> <key column="id"/> <list-index column="indexs"/> <element column="value" type="java.lang.String"/> </list> -<map name="my_map"> <key column="id"/> <!-- 指定什么列存储map的key数据 --> <map-key column="key_id" type="java.lang.String"/> <element column="value" type="java.lang.String"/> </map> <!-- 因为set是没有顺序的所以不需要list—index属性 --> -<set name="my_set"> <key column="id"/> <element column="value" type="java.lang.String"/> </set> </class> </hibernate-mapping>
测试:
package org.fkjava.test; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import org.fkjava.bean.Person; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * 使用Junit4 进行Hibernate 操作 * * @author hanfeili * www.fkjava.org */ public class HibernateTest { SessionFactory sessionFactory =null; Session session =null; Transaction tx = null; /** * 初始化测试数据 * @throws Exception */ @Before public void setUp() throws Exception { System.out.println("------setUp---初始化测试资源-----"); Configuration config = new Configuration().configure(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); sessionFactory = config.buildSessionFactory(sr); session = sessionFactory.openSession(); } @Test public void testAdd(){ Person p = new Person(); p.setName("tom0"); p.setPassword(123456); p.setBirthday(new java.util.Date()); String[] array = {"A","B","C","D"}; p.setMy_array(array); ArrayList<String> list = new ArrayList(); list.add("广东"); list.add("湖南"); list.add("北京"); list.add("上海"); p.setMy_list(list); HashMap<String,String> map = new HashMap<String,String>(); map.put("fkjava","www.fkjava.org"); map.put("facejava","www.facejava.org"); p.setMy_map(map); HashSet<String> set = new HashSet<String>(); set.add("帅哥"); set.add("大帅哥"); set.add("帅哥2"); set.add("美女"); p.setMy_set(set); tx = session.beginTransaction(); session.persist(p); tx.commit(); } @Test public void testGet() { Person p = (Person)session.get(Person.class, 1); System.out.println(p); } /** * 释放测试数据 * @throws Exception */ @After public void tearDown() throws Exception { System.out.println("------tearDown---释放测试数据---"); session.close(); sessionFactory.close(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。