第八章Hibernate映射多对一关联关系
1.表与表之间的关联可以分成一对一,一对多,多对一和多对多
2.网络商城中,一个大的商品分类下,又多个小的商品分类,一个小的商品分类下,又多个商品
Product类:
public class Product implements java.io.Serializable { private Integer id; private String name; private Double price; private String decription; public Product() { } 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 Double getPrice() { return this.price; } public void setPrice(Double price) { this.price = price; } public String getDecription() { return this.decription; } public void setDecription(String decription) { this.decription = decription; } }
Category类:
public class Category implements java.io.Serializable { private Integer id; private String name; private String description; private Set<Product> products = new HashSet<Product>(); public Category() { } public Category(String name, String description) { this.name = name; this.description = description; } 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 getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public Set<Product> getProducts() { return products; } public void setProducts(Set<Product> products) { this.products = products; } }
Category映射配置:
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.Category" table="CATEGORY" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="200" /> </property> <property name="description" type="java.lang.String"> <column name="DESCRIPTION" length="4000" /> </property> <set name="products"> <key column="category_id"></key> <one-to-many class="com.crazy.Product"/> </set> </class> </hibernate-mapping>
与Set映射有点儿像
Product映射配置:
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.Product" table="PRODUCT" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="200" /> </property> <property name="price" type="java.lang.Double"> <column name="PRICE" precision="6" /> </property> <property name="decription" type="java.lang.String"> <column name="DECRIPTION" length="2000" /> </property> </class> </hibernate-mapping>
Product与Category是多对一的关系,Product对象维护着Category对象的参考,如果由Category对象维护着多个Product对象的管理,就是一对多单向关联。
Product类:
public class Product implements java.io.Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private Double price; private String decription; private Category category; public Product() { } 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 Double getPrice() { return this.price; } public void setPrice(Double price) { this.price = price; } public String getDecription() { return this.decription; } public void setDecription(String decription) { this.decription = decription; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } }
Category类:
public class Category implements java.io.Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private String description; private Set<Product> products = new HashSet<Product>(); public Set<Product> getProducts() { return products; } public void setProducts(Set<Product> products) { this.products = products; } public Category() { } public Category(String name, String description) { this.name = name; this.description = description; } 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 getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } }
Category映射配置:
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.Category" table="CATEGORY" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="200" /> </property> <property name="description" type="java.lang.String"> <column name="DESCRIPTION" length="4000" /> </property> <set name="products"> <key column="category_id"></key> <one-to-many class="com.crazy.Product"/> </set> </class> </hibernate-mapping>
Product映射配置:
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.Product" table="PRODUCT" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="200" /> </property> <property name="price" type="java.lang.Double"> <column name="PRICE" precision="6" /> </property> <property name="decription" type="java.lang.String"> <column name="DECRIPTION" length="2000" /> </property> </class> </hibernate-mapping>
主动方对执行操作时,被关联对象(被动方)是否同步执行同一操作。
<set name="products" cascade="save-update"> <key column="category_id"></key> <one-to-many class="com.crazy.Product"/> </set>
注意:通常来说,在多对一关联和多对多的关联关系中使用级联没有任何意义。级联通常应用在一对多喝一对一的关联中,应用级联可以少写几行代码;否则需要维护级联的操作,需要多写几行代码。同时,cascade属性的save-update是最为常用的。
Category类:
public class Category implements java.io.Serializable { private Integer id; private String name; private String description; private Set<Product> products = new HashSet<Product>(); public Category() { } public Category(String name, String description, Set<Product> products) { this.name = name; this.description = description; this.products = products; } 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 getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public Set<Product> getProducts() { return this.products; } public void setProducts(Set<Product> products) { this.products = products; } }
Category映射配置:
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.Category" table="CATEGORY" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="200" /> </property> <property name="description" type="java.lang.String"> <column name="DESCRIPTION" length="4000" /> </property> <set name="products" inverse="true" cascade="save-update"> <key> <column name="CATEGORY_ID" precision="8" scale="0" /> </key> <one-to-many class="com.crazy.Product" /> </set> </class> </hibernate-mapping>
Product类:
public class Product implements java.io.Serializable { private Integer id; private Category category; private String name; private Double price; private String decription; public Product() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public Category getCategory() { return this.category; } public void setCategory(Category category) { this.category = category; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Double getPrice() { return this.price; } public void setPrice(Double price) { this.price = price; } public String getDecription() { return this.decription; } public void setDecription(String decription) { this.decription = decription; } }
Product映射配置:
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.crazy.Product" table="PRODUCT" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="200" /> </property> <property name="price" type="java.lang.Double"> <column name="PRICE" precision="6" /> </property> <property name="decription" type="java.lang.String"> <column name="DECRIPTION" length="2000" /> </property> <many-to-one name="category" class="com.crazy.Category" outer-join="true"> <column name="CATEGORY_ID" precision="8" scale="0" /> </many-to-one> </class> </hibernate-mapping>
在类的表现上是,我中有你,你中有我。
在Hibernate中的一对多的单向或者多向关联的情况下,我们可以将"一"方控制权交给"多"方,称为控制反转。
<set name="products" inverse="true"> <key> <column name="CATEGORY_ID" precision="8" scale="0" /> </key> <one-to-many class="com.crazy.Product" /> </set>