Hibernate深入浅出(十八)-----读深入浅出hibernate有感
单向一对多关联:
主控方映射配置如下:
被动方(TAddress)的记录由Hibernate负责读取,之后存放在主控方(TUser)指定的Collection类型属性中。对于one-to-many
关联关系,我们可以采用java.util.Set类型的Collection,表现在XML映射文件中就是<set>节点。单向一对多存在一个问题
,由于是单向关联,为了保持关联关系,我们只能通过主控方对被动方进行级联更新。如果被关联方的关联字段为"NOT
NULL",当Hibernate创建或者更新关联关系时,可能出现约束违例。
由于关联方向是单向的,关联关系由TUser对象维持,而被关联的addr对象本身并不知道自己与哪个TUser对象相关联,也就
是说,addr对象本身并不知道自己的user_id应该设为什么数值。在使用one-to-many进行单向关联时,由于Hibernate实现机
制中,采用了2条SQL语句进行一次数据插入操作,相对单条insert操作,几乎是2倍的性能开销,效率较低,因此,对于性能
敏感的系统而言,这样的解决方案所带来的开销可能难以承受。
如果addr对象知道如何获取user_id字段的内容,那么执行insert语句的时候直接将数据植入即可。这样不但绕开了约束违例
的可能,而且还节省了一条update语句的开销,大幅度提高了性能。双向一对多关系的出现解决了这个问题。它除了避免约
束和提高性能的好处之外,还带来另外一个优点,由于建立了双向关联,我们可以在关联双方中任意一方,访问关联的另一
方,这提供了更丰富灵活的控制手段。
双向一对多关联,实际上是"一对多"与"多对一"关联的组合。也就是说我们必须在主控方配置单向一对多关系的基础上,在
被控方配置与其对应的多对一关系。
上代码:
package
com.wyq.demo.common.reference.onetomany;
import java.io.Serializable;
import java.util.Set;
/**
* @author 作者
* @version 创建时间:2008-11-28 上午10:10:23
* 类说明 在one-to-many关系中,将many一方设为主控方(inserse=false)将有助于性能的改善。
*/
public class TUser implements Serializable {
private Integer id;
private Integer age;
private String name;
private Set addresses;
public Set getAddresses() {
return addresses;
}
public void setAddresses(Set addresses) {
this .addresses = addresses;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this .age = age;
}
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;
}
}
对应的映射文件:
import java.io.Serializable;
import java.util.Set;
/**
* @author 作者
* @version 创建时间:2008-11-28 上午10:10:23
* 类说明 在one-to-many关系中,将many一方设为主控方(inserse=false)将有助于性能的改善。
*/
public class TUser implements Serializable {
private Integer id;
private Integer age;
private String name;
private Set addresses;
public Set getAddresses() {
return addresses;
}
public void setAddresses(Set addresses) {
this .addresses = addresses;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this .age = age;
}
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;
}
}
<?
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.wyq.demo.common.reference.onetomany.TUser" table ="t_user" dynamic-insert ="true" dynamic-update ="true" catalog ="sample" >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="native" />
</ id >
< property name ="name" type ="java.lang.String" >
< column name ="name" ></ column >
</ property >
< property name ="age" type ="java.lang.Integer" >
< column name ="age" ></ column >
</ property >
<!-- 控制方向反转,Tuser不再作为主控方,而是将关联关系的维护工作
交给关联对象Taddress来完成,这样Taddress对象在持久化过程中
,就可以主动获取其关联的TUser对象的id,并将其作为自己的user_id,
之后执行一次insert操作就可以完成全部工作。
-->
< set name ="addresses" table ="t_address" inverse ="true" cascade ="all" order-by ="zipcode asc" >
< key column ="user_id" ></ key >
< one-to-many class ="com.wyq.demo.common.reference.onetomany.TAddress" />
</ set >
</ class >
</ hibernate-mapping >
<! 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.wyq.demo.common.reference.onetomany.TUser" table ="t_user" dynamic-insert ="true" dynamic-update ="true" catalog ="sample" >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="native" />
</ id >
< property name ="name" type ="java.lang.String" >
< column name ="name" ></ column >
</ property >
< property name ="age" type ="java.lang.Integer" >
< column name ="age" ></ column >
</ property >
<!-- 控制方向反转,Tuser不再作为主控方,而是将关联关系的维护工作
交给关联对象Taddress来完成,这样Taddress对象在持久化过程中
,就可以主动获取其关联的TUser对象的id,并将其作为自己的user_id,
之后执行一次insert操作就可以完成全部工作。
-->
< set name ="addresses" table ="t_address" inverse ="true" cascade ="all" order-by ="zipcode asc" >
< key column ="user_id" ></ key >
< one-to-many class ="com.wyq.demo.common.reference.onetomany.TAddress" />
</ set >
</ class >
</ hibernate-mapping >
关联对象:
package
com.wyq.demo.common.reference.onetomany;
import java.io.Serializable;
/**
* @author 作者
* @version 创建时间:2008-11-28 上午10:11:01
* 类说明 双向一对多关系除了避免约束违例和提高性能的好处之外,还带来了另外一个优点
* 由于建立双向关联,我们可以在关联双方中任意一方,访问关联的另一方。
*/
public class TAddress implements Serializable {
private Integer id;
private String address;
private String zipcode;
private String tel;
private String type;
private Integer userId;
private TUser user;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this .address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this .tel = tel;
}
public String getType() {
return type;
}
public void setType(String type) {
this .type = type;
}
public TUser getUser() {
return user;
}
public void setUser(TUser user) {
this .user = user;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this .userId = userId;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this .zipcode = zipcode;
}
}
关联对象的映射文件:
import java.io.Serializable;
/**
* @author 作者
* @version 创建时间:2008-11-28 上午10:11:01
* 类说明 双向一对多关系除了避免约束违例和提高性能的好处之外,还带来了另外一个优点
* 由于建立双向关联,我们可以在关联双方中任意一方,访问关联的另一方。
*/
public class TAddress implements Serializable {
private Integer id;
private String address;
private String zipcode;
private String tel;
private String type;
private Integer userId;
private TUser user;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this .address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this .tel = tel;
}
public String getType() {
return type;
}
public void setType(String type) {
this .type = type;
}
public TUser getUser() {
return user;
}
public void setUser(TUser user) {
this .user = user;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this .userId = userId;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this .zipcode = zipcode;
}
}
<?
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.wyq.demo.common.reference.onetomany.TAddress" table ="t_address" dynamic-insert ="false" dynamic-update ="false" catalog ="sample" >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="native" />
</ id >
< property name ="address" type ="java.lang.String" column ="address" ></ property >
< property name ="zipcode" type ="java.lang.String" column ="zipcode" ></ property >
< property name ="tel" type ="java.lang.String" column ="tel" ></ property >
< property name ="type" type ="java.lang.String" column ="type" ></ property >
< many-to-one name ="user" class ="com.wyq.demo.common.reference.onetomany.TUser" cascade ="none" outer-join ="auto" column ="user_id" not-null ="true" ></ many-to-one >
</ class >
</ hibernate-mapping >
Inverse指的是关联关系的控制方向,而cascade指的是层级之间的连锁操作。在one-to-many关系中,将many一方设为主控方(inverse=false)将有助性能的改善。
<! 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.wyq.demo.common.reference.onetomany.TAddress" table ="t_address" dynamic-insert ="false" dynamic-update ="false" catalog ="sample" >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="native" />
</ id >
< property name ="address" type ="java.lang.String" column ="address" ></ property >
< property name ="zipcode" type ="java.lang.String" column ="zipcode" ></ property >
< property name ="tel" type ="java.lang.String" column ="tel" ></ property >
< property name ="type" type ="java.lang.String" column ="type" ></ property >
< many-to-one name ="user" class ="com.wyq.demo.common.reference.onetomany.TUser" cascade ="none" outer-join ="auto" column ="user_id" not-null ="true" ></ many-to-one >
</ class >
</ hibernate-mapping >