标签:
关系映射的双向关联的多对一
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; } } ---------------------------------------------------------------------- package org.fkjava.bean; /** * 一对多 外键 双向 关联关系 配置 * * @author hanfeili www.fkjava.org */ public class Address { private Integer id; /** * 邮政编码 */ private String codes; /** * 具体地址 */ private String des; //因为是 双向 关联 ,所以需要在address类 //中也要有person private Person person; @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 Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
<?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"/> <!-- 通过Set配置一个人对应的多个地址的关联关系 inverse=true表示在Person端不维护关系 --> -<set name="addressSet" inverse="true" cascade="all"> <!-- 指定adddrssSet集合中的数据对应t_person的的一个外键 --> <key column="p_id"/> <!-- 指定Person 关联的实例类型 --> <one-to-many class="Address"/> </set> </class> -<class table="t_address" name="Address"> -<id name="id"> <generator class="identity"/> </id> <property name="codes"/> <property name="des"/> <!-- not-null="true" 指定外键不能为 null --> <many-to-one name="person" column="p_id" not-null="true"/> </class> </hibernate-mapping>
标签:
原文地址:http://www.cnblogs.com/aicpcode/p/4201200.html