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>
时间: 2024-11-05 11:42:39