其去掉foreign key 的SQL为:
CREATE TABLE `ADDRESS` (
`ID` int(4) NOT NULL auto_increment,
`CITY` varchar(40) default NULL,
`STREET` varchar(100) default NULL,
`PERSON_ID` int(4) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312;
CREATE TABLE `PERSON` (
`ID` int(4) NOT NULL auto_increment,
`NAME` char(20) default NULL,
`PHONE` char(20) default NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312;
一对多单向关联关系和一对多双向关联关系数据库表相同,不同的只是映射的JAVA类及映射文件的配置。DAO代码和测试代码省略。
一对多单向关联关系:
实体类:
package com.onetomanysingle.model; /** * Address entity */ public class Address implements java.io.Serializable { // Fields private Integer id; private String city; private String street; private Person person; // Constructors /** default constructor */ public Address() { } /** minimal constructor */ public Address(Person person) { this.person = person; } /** full constructor */ public Address(String city, String street, Person person) { this.city = city; this.street = street; this.person = person; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } public String getStreet() { return this.street; } public void setStreet(String street) { this.street = street; } public Person getPerson() { return this.person; } public void setPerson(Person person) { this.person = person; } }
package com.onetomanysingle.model; /** * Person entity */ public class Person implements java.io.Serializable { // Fields private Integer id; private String name; private String phone; // Constructors /** default constructor */ public Person() { } /** full constructor */ public Person(String name, String phone) { this.name = name; this.phone = phone; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } }
映射文件:
Address.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.onetomanysingle.model.Address" table="address" catalog="test"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="city" type="java.lang.String"> <column name="CITY" length="40" /> </property> <property name="street" type="java.lang.String"> <column name="STREET" length="100" /> </property> <!-- 映射person到address的一对多单向关联即address到person的多对一单向关联 具体意义是:通过查询地址可以查询到此地址所住的人(一个)。 --> <many-to-one name="person" column="PERSON_ID" class="com.onetomanysingle.model.Person" lazy="false" not-null="true"/> <!-- 以上分别说明映射字段在类中的属性,表中的字段,对应的类,立即加载,非空 --> </class> </hibernate-mapping>
Person.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.onetomanysingle.model.Person" table="person" catalog="test"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="20" /> </property> <property name="phone" type="java.lang.String"> <column name="PHONE" length="20" /> </property> </class> </hibernate-mapping>
一对多单向关联关系,可以从多的一方查询到一的一方,但从一的一方查询不到多的一方。
一对多双向关联关系映射:
实体类:
package com.onetomanydouble.model; /** * Address entity */ public class Address implements java.io.Serializable { // Fields private Integer id; private String city; private String street; private Person person; // Constructors public Address() { super(); } public Address(Integer id, String city, String street, Person person) { super(); this.id = id; this.city = city; this.street = street; this.person = person; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } public String getStreet() { return this.street; } public void setStreet(String street) { this.street = street; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
package com.onetomanydouble.model; import java.util.HashSet; import java.util.Set; /** * Person entity */ public class Person implements java.io.Serializable { // Fields private Integer id; private String name; private String phone; private Set address = new HashSet(); // Constructors /** default constructor */ public Person() { } public Person(Integer id, String name, String phone, Set address) { super(); this.id = id; this.name = name; this.phone = phone; this.address = address; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public Set getAddress() { return address; } public void setAddress(Set address) { this.address = address; } }
映射文件:
Address.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.onetomanydouble.model.Address" table="address" catalog="test"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="city" type="java.lang.String"> <column name="CITY" length="40" /> </property> <property name="street" type="java.lang.String"> <column name="STREET" length="100" /> </property> <!-- 多对一映射 --> <many-to-one name="person" column="PERSON_ID" class="com.onetomanydouble.model.Person" lazy="false" not-null="true"/> </class> </hibernate-mapping>
Person.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.onetomanydouble.model.Person" table="person" catalog="test"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="20" /> </property> <property name="phone" type="java.lang.String"> <column name="PHONE" length="20" /> </property> <!-- 一对多映射 inverse说明这是主控方,true由被控方(多的一方)维护外键关系,一般来说,都应该由多的一方来维护 --> <set name="address" cascade="all" inverse="true" lazy="false"> <key column="person_id"/> <one-to-many class="com.onetomanydouble.model.Address"/> </set> </class> </hibernate-mapping>
一对多双向关联关系,双方均可查询到对方。
从上面可以看出,一对多的单向和双向,只不过是在类中是否有对方作为属性,一的一方有个多的一方的set集合,多的一方有个一的一方做为属性。在配置文件中,单双向多的一方的配置均相同,只有一的一方不同。