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;
 }
 

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值