情况:
每一个人都会有不同的爱好、特长,而且有每个人的爱好和特长会有多个。那怎样表示呢?—用集合
区别:
Set:它的元素存放没有顺序且不允许重复
List:它需要在结合属性对应的数据库表中用一个额外的索引列保存每个元素的位置。
Array:同list
Map:它的元素以键/值对的形式保存,也是无序的
关系图:
具体实现
1、实体
/**
* 集合映射实体
*
* @author gxq
*
*/
@SuppressWarnings("unused")
public class CollectionMapping {
// 集合id
private int id;
// 集合名称
private String name;
// Set集合
private Set setValues = new HashSet();
// List集合
private List listValues = new ArrayList();
// 数组
private String[] arrayValues;
// Map
private Map mapValues;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getSetValues() {
return setValues;
}
public void setSetValues(Set setValues) {
this.setValues = setValues;
}
public List getListValues() {
return listValues;
}
public void setListValues(List listValues) {
this.listValues = listValues;
}
public String[] getArrayValues() {
return arrayValues;
}
public void setArrayValues(String[] arrayValues) {
this.arrayValues = arrayValues;
}
public Map getMapValues() {
return mapValues;
}
public void setMapValues(Map mapValues) {
this.mapValues = mapValues;
}
}
2、映射文件
<span style="font-size:18px;"><hibernate-mapping>
<class name="com.bjpowernode.collection.CollectionMapping" table="t_collection_mapping">
<!--集合表的主键 -->
<id name="id">
<generator class="native" />
</id>
<property name="name"></property>
<!-- set的id及其对应的值 -->
<set name="setValues" table="t_set_values">
<key column="set_id"></key>
<element type="string" column="set_values" not-null="true"></element>
</set>
<!-- list有序,使用 list-index进行设置 -->
<list name="listValues" table="t_list_values">
<key column="list_id"></key>
<list-index column="list_index"></list-index>
<element type="string" column="list_values"></element>
</list>
<!-- 同list -->
<array name="arrayValues" table="t_array_values">
<key column="array_id"></key>
<list-index column="array_index"></list-index>
<element type="string" column="array_values"></element>
</array>
<map name="mapValues" table="t_map_values">
<key column="map_id"></key>
<map-key type="string" column="map_key"></map-key>
<element type="string" column="map_values"></element>
</map>
</class>
</hibernate-mapping></span>
3、配置文件
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
<![CDATA[jdbc:mysql://localhost:3306/Hibernate?useUnicode=true&characterEncoding=utf8]]>
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/bjpowernode/collection/Collection.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、封装好的工具类:HibernateUtils
<span style="font-size:18px;">/**
* 工具类(封装开启session和事务)
* @classname HibernateUtils
* @author 高晓青
* @date 2015-4-16 下午2:56:42
* @version hibernate
*/
public class HibernateUtils {
private static SessionFactory factory;
static{
try {
//默认读取的是hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//建立sessionFactory
factory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
//开启session
public static Session getSession(){
return factory.openSession();
}
//关闭session
public static void closeSession(Session session){
//判断是否为空
if(session!=null){
//判断是否是打开状态再进行关闭
if(session.isOpen()){
session.close();
}
}
}
}</span>
5、添加
<span style="font-size:18px;">@SuppressWarnings("unchecked")
public void testSave1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
CollectionMapping c = new CollectionMapping();
c.setName("XXX");
//向set中添加数据
c.getSetValues().add("a");
c.getSetValues().add("b");
//向list中添加数据
c.getListValues().add("c");
c.getListValues().add("d");
//向s数组中添加数据
String[] arrayValues = new String[]{"e", "f"};
c.setArrayValues(arrayValues);
//向map中添加数据
Map mapValues = new HashMap();
mapValues.put("k1", "v1");
mapValues.put("k2", "v2");
c.setMapValues(mapValues);
session.save(c);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
</span>
6、查询
@SuppressWarnings("unchecked")
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//获取集合中的一条数据
CollectionMapping c = (CollectionMapping)session.get(CollectionMapping.class, 1);
//打印集合中的元素
System.out.println("name=" + c.getName());
System.out.println("setValue=" + c.getSetValues());
System.out.println("listValue=" + c.getListValues());
System.out.println("arrayValue=" + c.getArrayValues());
System.out.println("mapValue=" + c.getMapValues());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
注:array并未重写toString