`
gg19861207
  • 浏览: 179947 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JPA多对一关(单向和双向)联映射实验

阅读更多

背景: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;
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics