码迷,mamicode.com
首页 > 其他好文 > 详细

关系映射的双向关联的多对多

时间:2015-01-04 16:57:56      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

关系映射的双向关联的多对多

package org.fkjava.bean;

import java.util.Set;

/**
 * 多对多 双向 关联关系 配置
 * 
 * @author hanfeili www.fkjava.org
 */
public class Address {
    private Integer id;
    /**
     * 邮政编码
     */
    private String codes;
    /**
     * 具体地址
     */
    private String des;

    private Set<Person> personSet;

    @Override
    public String toString() {
        return "Address [id=" + id + ", codes=" + codes + ", des=" + des + "]";
    }

    public String getCodes() {
        return codes;
    }

    public void setCodes(String codes) {
        this.codes = codes;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    

    public Set<Person> getPersonSet() {
        return personSet;
    }

    public void setPersonSet(Set<Person> personSet) {
        this.personSet = personSet;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}
-----------------------------------------------------------------------
package org.fkjava.bean;

import java.util.Date;
import java.util.Set;

/** 
 * 多对多   双向 关联关系 配置 
 * 
 * 
 * @author hanfeili
 *  www.fkjava.org
 */
public class Person {
    private Integer id;
    private String name;
    private int password;
    private Date birthday;
    
    //一个set集合保存多个地址引用
    private Set<Address> addressSet;
    
    public Person() {
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", password=" + password
                + ", birthday=" + birthday + "]";
    }

    
    

    public Set<Address> getAddressSet() {
        return addressSet;
    }

    public void setAddressSet(Set<Address> addressSet) {
        this.addressSet = addressSet;
    }

    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;
    }

}

配置文件

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- 多对多 双向 关联关系 配置 -->

-<hibernate-mapping package="bean">


-<class table="t_person" name="Person">


-<id name="id">

<generator class="identity"/>

</id>

<property name="name"/>

<property name="password"/>

<property name="birthday"/>

<!-- table 指定一个中间表名 多对多的配置中中间表名要一致-->



-<set table="join_person_addrss" name="addressSet" inverse="true">

<!-- 在中间表中使用p_id 关联_person数据 -->


<key not-null="true" column="p_id"/>

<!-- 指定t_pseron表与t_address 的关联关系使用a_id 维护 -->


<many-to-many class="Address" column="a_id"/>

</set>

</class>


-<class table="t_address" name="Address">


-<id name="id">

<generator class="identity"/>

</id>

<property name="codes"/>

<property name="des"/>


-<set table="join_person_addrss" name="personSet">

<key not-null="true" column="a_id"/>

<many-to-many class="Person" column="p_id"/>

</set>

</class>

</hibernate-mapping>

 

关系映射的双向关联的多对多

标签:

原文地址:http://www.cnblogs.com/aicpcode/p/4201265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!