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; }}
-- - - - - -
测试:
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); ArrayListlist = new ArrayList(); list.add("广东"); list.add("湖南"); list.add("北京"); list.add("上海"); p.setMy_list(list); HashMap map = new HashMap (); map.put("fkjava","www.fkjava.org"); map.put("facejava","www.facejava.org"); p.setMy_map(map); HashSet set = new HashSet (); 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(); }}