组件映射和集合映射

来源:互联网 发布:json嵌套json数据 编辑:程序博客网 时间:2024/05/02 02:28

组件映射:

组件映射:

在实体类中:

private Set<String> hobbies;

在映射文件中:

<set name="hobbies" table="student_hobby">

    <key column="student_id"/>   用来指定外键列

    <element type="string" column="hobbies_name"></element> 用来指定元素

</set>

在配置文件中:

<mapping resource="com/hbsi/domain/Student.hbm.xml" />

在测试类中:

try{

           session=HibrnateUtil.getSession();

           tx=session.beginTransaction();

           Student s=new Student();

           s.setName("tom");

           Set<String> hobbies=new HashSet<String>();

           hobbies.add("football");

           hobbies.add("dance");

           s.setHobbies(hobbies);

           session.save(s);

           tx.commit();

       }finally{

           if(session!=null){

              session.close();

           }

       }

 

 

集合映射(set, list, array,bag, map)

·这些集合类都是Hibernate实现的类和JAVA中的集合类不完全一样,set,list,map分别和JAVA中的Set,List,Map接口对应,bag映射成JAVA的List;这些集合的使用和JAVA集合中对应的接口基本一致;在JAVA的实体类中集合只能定义成接口不能定义成具体类, 因为集合会在运行时被替换成Hibernate的实现。

·集合的简单使用原则:大部分情况下用set,需要保证集合中的顺序用list,想用java.util.List又不需要保证顺序用bag。

注:在类中运用的时候,均用接口。

一、             set集合映射:

在实体类中定义属性:

//private Set<String> hobbies;//爱好----集合对象

       在映射文件中:

       <set name="hobbies" table="student_hobby">

       <key column="student_id"/>    用来指定外键列

    <element type="string" column="hobbies_name"></element>   用来指定元素

</set>

       在配置文件中:

       <mapping resource="com/hbsi/domain/Student.hbm.xml" />

    在测试类中:

       try{

           session=HibrnateUtil.getSession();

           tx=session.beginTransaction();

           Student s=new Student();

           s.setName("tom");

           Set<String> hobbies=new HashSet<String>();

           hobbies.add("football");

           hobbies.add("dance");

           s.setHobbies(hobbies);

           session.save(s);

           tx.commit();

       }finally{

           if(session!=null){

              session.close();

           }

       }

二、   list映射:list集合类型在有些一对多关系中可能会很有用,例如,在论坛版面管理中,将会涉及到版面的上移/下移等操作,这就需要在版面集合中维护顺序,用list集合就可以很好地解决这个问题!

在实体类中定义属性:

private List<String> hobbies;

在映射文件中:

<list name="hobbies">

        <key column="student_id"/>

       <list-index column="order_index"/> 用来指定某一列维护添加顺序  

       <element type="string" column="hobbies_name"></element>

</list>

在配置文件中:

       <mapping resource="com/hbsi/domain/Student.hbm.xml" />

在测试类中:

session=HibrnateUtil.getSession();

           tx=session.beginTransaction();

           Student s=new Student();

           s.setName("tom");

           List<String> list=new ArrayList<String>();

           list.add("football");

           list.add("dance");

           s.setHobbies(list);

           session.save(s);

           tx.commit();

       }finally{

           if(session!=null){

              session.close();

           }

    }

注:list和set的区别:list集合有一个顺序的要求。

三、             bag映射

四、             map映射:

在实体类中:

private Map<Long,String>hobbies;

在映射文件中:

<map name="hobbies">

       <key column="student_id"/>

       <map-key type="long" column="name_key"/> <!-- 指定键值对中的键  -->

       <element type="string" column="hobbies_name"></element> <!-- 指定键值对中的值  -->

    </map>

    在配置文件中:

    <mapping resource="com/hbsi/domain/Student.hbm.xml" />

    在测试类中:

           session=HibrnateUtil.getSession();

           tx=session.beginTransaction();

           Student s=new Student();

           s.setName("tom");

           Map<Long,String> m=new HashMap<Long, String>();

           m.put(new Long(4),"dudu");

           m.put(new Long(5), "huhu");

           s.setHobbies(m);

           session.save(s);

           tx.commit();

       }finally{

           if(session!=null){

              session.close();

           }

       }

  注:map集合是键值对。