Hibernate 集合属性的操作

来源:互联网 发布:韩国美女直播秀软件 编辑:程序博客网 时间:2024/05/02 00:11

Hibernate对集合属性的操作


Set集合属性的操作

Set结合接口的配置

在*.hbm.xml文件中使用<set>标签

<set>元素用来映射java.util.Set类型的属性,常见的属性和子元素有:

name属性

table属性

<key>子元素

<element>子元素


实例:

entity/StudentSet.java

package entity;import java.io.Serializable;import java.util.Set;/** * Created by lenovo on 2016/3/29. */public class StudentSet implements Serializable{    private int id;    private String name;    private int age;    private Set<String> hobby;    public StudentSet() {    }    public Set<String> getHobby() {        return hobby;    }    public void setHobby(Set<String> hobby) {        this.hobby = hobby;    }    public StudentSet(String name){        this.name=name;    }    public StudentSet(String name, int age) {        this.name = name;        this.age = age;    }    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 int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}
entity/StudentSet.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC        "//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="entity.StudentSet" table="studentSet">        <id name="id" column="id" >            <generator class="native"/>        </id>        <property name="name" column="name" />        <property name="age" column="age"/>        <set name="hobby" table="hobby">        <key column="id"/>            <element type="java.lang.String" column="hobby"/>    </set>    </class></hibernate-mapping>
action/handleSet.java
package action;import entity.Student;import entity.StudentSet;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;import java.util.HashSet;import java.util.Set;/** * Created by lenovo on 2016/3/29. */public class HandleSet {    @Test    public void createTable(){        Configuration configuration=new Configuration().configure();        SchemaExport schemaExport=new SchemaExport(configuration);        schemaExport.create(true,true);    }    @Test    public void save(){        Session session=HandleSession.getSession();        Transaction transaction=session.beginTransaction();        try{            StudentSet student=new StudentSet();            student.setName("wo");            student.setAge(22);            Set<String> set=new HashSet<>();            set.add("football");            set.add("basketball");            student.setHobby(set);            session.save(student);            transaction.commit();        }catch (Exception e){            transaction.rollback();            e.printStackTrace();        }finally {            session.close();        }    }    @Test    public void get(){        Session session=HandleSession.getSession();        Transaction transaction=session.beginTransaction();        try{            StudentSet student=(StudentSet)session.get(StudentSet.class,1);            System.out.println(student.getName()+student.getAge());            System.out.println(student.getHobby());            transaction.commit();        }catch (Exception e){            transaction.rollback();            e.printStackTrace();        }finally {            session.close();        }    }}
对List集合的操作

<list>元素用来映射java.util.List类型的属性,常用的属性和子元素有:

name属性

table属性

<key>子元素

<list-index>子元素

<element>子元素


entity/StudentList.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC        "//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="entity.StudentList" table="studentList">        <id name="id" column="id" >            <generator class="native"/>        </id>        <property name="name" column="name" />        <property name="age" column="age"/>        <list name="hobby" table="hobby_list">        <key column="id"/>            <list-index column="position"/>            <element type="java.lang.String" column="hobby"/>    </list>    </class></hibernate-mapping>

Collection集合属性的操作

<bag>或<ibag>元素用来映射java.util.Collection类型的属性,常用的属性和子元素有:

name属性

table属性

<key>子元素

<collection-id>子元素

<element>子元素


StudentCollection.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC        "//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="entity.StudentCollection" table="studentCollection">        <id name="id" column="id" >            <generator class="native"/>        </id>        <property name="name" column="name" />        <property name="age" column="age"/>        <list name="hobby" table="hobby_collection">        <key column="id"/>            <element type="java.lang.String" column="hobby"/>    </list>    </class></hibernate-mapping>

Map集合属性的操作

<map>元素用来映射java.util.Map类型的属性,常用的属性和子元素有:

name属性

table属性

<key>子元素

<map-key>子元素

<element>子元素

<!DOCTYPE hibernate-mapping PUBLIC        "//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="entity.StudentMap" table="studentMap">        <id name="id" column="id" >            <generator class="native"/>        </id>        <property name="name" column="name" />        <property name="age" column="age"/>        <map name="hobby" table="hobby_map">            <key column="id"/>            <element type="java.lang.String" column="hobby"/>            <map-key type="java.lang.String" column="map-key"/>    </map>    </class></hibernate-mapping>



0 0
原创粉丝点击