JPA多对一关(单向和双向)联映射实验
2009-09-30 23:18
330 查看
背景:ProductInfo对Brand是多对一的关系,ProductInfo对ProductType是多对一的关系
一:1:首先测试单向关联:即只在ProductInfo里添加
@ManyToOne(cascade=CascadeType.REFRESH)
public Brand getBrand() {
return brand;
}
和
@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
//optional=false说明必须要有品牌属性,即这个属性必须有,不能为空
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
运行测试生成productinfo表后,属性brand,type生成对应的字段分别是brand_code和type_typeid(因为数据库里的表名是brand和producttype,brand和type是ProductInfo的属性,code,typeid分别是表brand,productytype的主键,所以默认的生成的外键字段应该是:属性名_主键名(外表的主键名)
2:做一些改变,改变如下:
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="brandid")
public Brand getBrand() {
return brand;
}
和
@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
@JoinColumn(name="typeid")
//optional=false说明必须要有品牌属性,即这个属性必须有,不能为空
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
运行生成表的测试,测试的结果是:属性brand,type生成对应的字段分别是brandid和typeid。
单向测试总结:只是在表productinfo里多了两个外键,分别参照表brand和表producttype,而在表brand和表producttype中并没有出现新增字段
二:测试双向关联
在ProductType里加入下面数据:
private Set<ProductInfo> productInfos= new HashSet<ProductInfo>();
//mappedBy="type"说明一对多的关系是靠ProductInfo里的ProductType类型的属性type来控制
//,cascade=CascadeType.REMOVE说明级联关系是删除,就是当把ProductType对象删除的时候,对应的有关系的ProductInfo对象们也被删除
@OneToMany(mappedBy="type",cascade=CascadeType.REMOVE)
public Set<ProductInfo> getProductInfos() {
return productInfos;
}
public void setProductInfos(Set<ProductInfo> productInfos) {
this.productInfos = productInfos;
}
运行测试:测试结果:表producttype没有发生任何变化,也没有添加新字段,这个是可以理解的,因为本来在ProductType里一对多的关系是由ProductInfo里的type属性来控制的。
总结可见:双线关联和单向关联在数据表层是没有区别的,只是在业务上单向关联只可以通过ProductInfo对象找到对应的ProductType对象;而双向
关联关系不仅可以通过ProductInfo对象找到对应的ProductType对象,还可以通过ProductType对象找到对应的ProductInfo对象
package com.itcast.bean.product;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.Cascade;
import com.itcast.util.Sex;
@Entity
public class ProductInfo {
private Integer id;
private Brand brand;
/**型号**/
/**产品类型**/
private ProductType type;
private Sex sexRequest = Sex.NONE;
@Id @GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(cascade=CascadeType.REFRESH)
public Brand getBrand() {
return brand;
}
public void setBrand(Brand brand) {
this.brand = brand;
}
@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
//optional=false说明必须要有品牌属性,即这个属性必须有,不能为空
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
@Enumerated(EnumType.STRING)@Column(length=5,nullable=false)
public Sex getSexRequest() {
return sexRequest;
}
public void setSexRequest(Sex sexRequest) {
this.sexRequest = sexRequest;
}
}
一:1:首先测试单向关联:即只在ProductInfo里添加
@ManyToOne(cascade=CascadeType.REFRESH)
public Brand getBrand() {
return brand;
}
和
@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
//optional=false说明必须要有品牌属性,即这个属性必须有,不能为空
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
运行测试生成productinfo表后,属性brand,type生成对应的字段分别是brand_code和type_typeid(因为数据库里的表名是brand和producttype,brand和type是ProductInfo的属性,code,typeid分别是表brand,productytype的主键,所以默认的生成的外键字段应该是:属性名_主键名(外表的主键名)
2:做一些改变,改变如下:
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="brandid")
public Brand getBrand() {
return brand;
}
和
@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
@JoinColumn(name="typeid")
//optional=false说明必须要有品牌属性,即这个属性必须有,不能为空
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
运行生成表的测试,测试的结果是:属性brand,type生成对应的字段分别是brandid和typeid。
单向测试总结:只是在表productinfo里多了两个外键,分别参照表brand和表producttype,而在表brand和表producttype中并没有出现新增字段
二:测试双向关联
在ProductType里加入下面数据:
private Set<ProductInfo> productInfos= new HashSet<ProductInfo>();
//mappedBy="type"说明一对多的关系是靠ProductInfo里的ProductType类型的属性type来控制
//,cascade=CascadeType.REMOVE说明级联关系是删除,就是当把ProductType对象删除的时候,对应的有关系的ProductInfo对象们也被删除
@OneToMany(mappedBy="type",cascade=CascadeType.REMOVE)
public Set<ProductInfo> getProductInfos() {
return productInfos;
}
public void setProductInfos(Set<ProductInfo> productInfos) {
this.productInfos = productInfos;
}
运行测试:测试结果:表producttype没有发生任何变化,也没有添加新字段,这个是可以理解的,因为本来在ProductType里一对多的关系是由ProductInfo里的type属性来控制的。
总结可见:双线关联和单向关联在数据表层是没有区别的,只是在业务上单向关联只可以通过ProductInfo对象找到对应的ProductType对象;而双向
关联关系不仅可以通过ProductInfo对象找到对应的ProductType对象,还可以通过ProductType对象找到对应的ProductInfo对象
package com.itcast.bean.product;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.Cascade;
import com.itcast.util.Sex;
@Entity
public class ProductInfo {
private Integer id;
private Brand brand;
/**型号**/
/**产品类型**/
private ProductType type;
private Sex sexRequest = Sex.NONE;
@Id @GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(cascade=CascadeType.REFRESH)
public Brand getBrand() {
return brand;
}
public void setBrand(Brand brand) {
this.brand = brand;
}
@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
//optional=false说明必须要有品牌属性,即这个属性必须有,不能为空
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
@Enumerated(EnumType.STRING)@Column(length=5,nullable=false)
public Sex getSexRequest() {
return sexRequest;
}
public void setSexRequest(Sex sexRequest) {
this.sexRequest = sexRequest;
}
}
相关文章推荐
- Hibernate映射详解(二)--多对一,一对多单向,一对多双向关联映射
- JPA的一对多映射(单向)
- Hibernate XML一对一外键关联映射 单向+双向
- JPA中one-to-one关系的单向映射示例
- Hibernate映射详解(二)--多对一,一对多单向,一对多双向关联映射
- jpa单向一对一关系外键映射
- Hibernate多对多单向关联和双向关联映射的方法
- jpa双向一对多关联映射
- Hibernate关联关系映射(单向一对多、单向多对一、双向一对多)
- cascade和inverse一对多双向映射实验(各种情况分析)
- Hibernate映射详解(二)--多对一,一对多单向,一对多双向关联映射
- ORM框架Hibernate (四) 一对一单向、双向关联映射
- ORM框架Hibernate (四) 一对一单向、双向关联映射
- 【JPA 级联保存/级联删除】@OneToMany 一对多(单向和双向)注解
- 自己总结一下关联关系映射中的OneToMany,ManyToOne,单向,双向注意的事项!ps:例子
- Hibernate一对多单向关联和双向关联映射方法及其优缺点
- hibernate的映射关系怎样判断用单向还是双向
- 单向一对一映射与双向一对一映射的区别
- 技术点-hibernate关系映射-多对多单向/双向
- JPA学习笔记(8)——映射双向一对多关联关系