Hibernate中配置集合映射(1)

来源:互联网 发布:网络映射什么意思 编辑:程序博客网 时间:2024/06/06 09:06

Hibernate中配置集合映射

1.Set映射:
        <set name="Set
属性名" table="表名">
            <key column="
与主键关联字段名
" />
            <element type="
数据类型" column="字段名
" />
        </set>
2.List
映射:

        <list name="List
属性名" table="表名">
            <key column="
与主键关联字段名
" />
            <list-index column="
索引字段名
" />
            <element type="
数据类型" column="字段名
" />
        </list>
3.Map
映射:

        <map name="Map
属性名" table="表名">
            <key column="
与主键关联字段名
" />
            <map-key type="
数据类型" column="KEY字段名
" />
            <element type="
数据类型" column="VALUE字段名
" />
        </map>
4.Array
映射:

        <array name="Array
属性名" table="表名">
            <key column="
与主键关联字段名
"/>
            <list-index column="
索引字段名
" />
            <element type="
数据类型" column="字段名
" />
        </array>


hibernate.cfg.xml:

view plaincopy to clipboardprint?

<?xml version='1.0' encoding='UTF-8'?>  

<!DOCTYPE hibernate-configuration PUBLIC   

           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  

  <!-- Generated by MyEclipse Hibernate Tools.-->  

<hibernate-configuration>    

<session-factory>  

    <property name="connection.username">scott</property>  

  <property name="connection.url">  

        jdbc:oracle:thin:@127.0.0.1:1521:MGC   

    </property>  

    <property name="dialect">  

        org.hibernate.dialect.Oracle9Dialect   

    </property>  

    <property name="myeclipse.connection.profile">MGC</property>  

    <property name="connection.password">tiger</property>  

    <property name="connection.driver_class">  

        oracle.jdbc.driver.OracleDriver   

    </property>  

  

    <property name="show_sql">true</property>  

    <mapping  

        resource="cn/edu/ahau/mgc/hibernate/pojo/Collection.hbm.xml" />  

  

</session-factory>  

  

</hibernate-configuration>  

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<!-- Generated by MyEclipse Hibernate Tools.                   -->

<hibernate-configuration>

 

<session-factory>

    <property name="connection.username">scott</property>

    <property name="connection.url">

        jdbc:oracle:thin:@127.0.0.1:1521:MGC

    </property>

    <property name="dialect">

        org.hibernate.dialect.Oracle9Dialect

    </property>

    <property name="myeclipse.connection.profile">MGC</property>

    <property name="connection.password">tiger</property>

    <property name="connection.driver_class">

        oracle.jdbc.driver.OracleDriver

    </property>

 

    <property name="show_sql">true</property>

    <mapping

        resource="cn/edu/ahau/mgc/hibernate/pojo/Collection.hbm.xml" />

 

</session-factory>

 

</hibernate-configuration>



Collection.java:

view plaincopy to clipboardprint?

package cn.edu.ahau.mgc.hibernate.pojo;   

  

import java.util.List;   

import java.util.Map;   

import java.util.Set;   

  

public class Collection {   

  

    private long id;   

    private String name;   

    private Set setValues;   

    private List listValues;   

    private Map mapValues;   

    private String[] arrayValues;   

  

    public long getId() {   

        return id;   

    }   

  

    public void setId(long 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 Map getMapValues() {   

        return mapValues;   

    }   

  

    public void setMapValues(Map mapValues) {   

        this.mapValues = mapValues;   

    }   

  

    public String[] getArrayValues() {   

        return arrayValues;   

    }   

  

    public void setArrayValues(String[] arrayValues) {   

        this.arrayValues = arrayValues;   

    }   

}  

package cn.edu.ahau.mgc.hibernate.pojo;

 

import java.util.List;

import java.util.Map;

import java.util.Set;

 

public class Collection {

 

    private long id;

    private String name;

    private Set setValues;

    private List listValues;

    private Map mapValues;

    private String[] arrayValues;

 

    public long getId() {

        return id;

    }

 

    public void setId(long 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 Map getMapValues() {

        return mapValues;

    }

 

    public void setMapValues(Map mapValues) {

        this.mapValues = mapValues;

    }

 

    public String[] getArrayValues() {

        return arrayValues;

    }

 

    public void setArrayValues(String[] arrayValues) {

        this.arrayValues = arrayValues;

    }

}