hibernate之双向一对多(注解方式)

一方:


package com.cwenhui.demo.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name="goods_type")
public class GoodsType implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5397949036775130583L;
	
	@Id
	@Column(name="goodsTypeId")
	@GeneratedValue
	private int goodsTypeId;
	
	@Column(name="goodsTypeName")
	private String goodsTypeName;
	
	@OneToMany(mappedBy="goodsType",cascade = CascadeType.MERGE, targetEntity = Goods.class, fetch=FetchType.EAGER)
	private Set<Goods> goods= new HashSet<Goods>();

	public GoodsType() {
//		super();
	}

	public GoodsType(String goodsTypeName) {
//		super();
		this.goodsTypeName = goodsTypeName;
	}

	public int getGoodsTypeId() {
		return goodsTypeId;
	}

	public void setGoodsTypeId(int goodsTypeId) {
		this.goodsTypeId = goodsTypeId;
	}

	public String getGoodsTypeName() {
		return goodsTypeName;
	}

	public void setGoodsTypeName(String goodsTypeName) {
		this.goodsTypeName = goodsTypeName;
	}

	public Set<Goods> getGoods() {
		return goods;
	}

	public void setGoods(Set<Goods> goods) {
		this.goods = goods;
	}

	@Override
	public String toString() {
		return "GoodsType [goodsTypeId=" + goodsTypeId + ", goodsTypeName="
				+ goodsTypeName + ", goods=" + goods + "]";
	}
	
	

}



多方:

package com.cwenhui.demo.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.Cascade;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity(name="goods")
@JsonIgnoreProperties(value={"goodsType"})				//忽略json结果中的goodsType字段,避免死循环
public class Goods implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3186260988079048370L;
	
	@Id
	@Column(name="goodsId")
	@GeneratedValue
	private int goodsId;
	
	@Column(name="goodsName")
	private String goodsName;
	
	@Column(name="goodsPrice")
	private int goodsPrice;
	
	@Column(name="goodsSales")
	private int goodsSales;
	
	@Column(name="goodsDesc")
	private String goodsDesc;
	
	@Column(name="goodsImgPath")
	private String goodsImgPath;
	
	@Column(name="goodsRemain")
	private int goodsRemain;
	
	@ManyToOne(targetEntity=GoodsType.class, cascade=CascadeType.ALL)
	@JoinColumn(name="goodsTypeId")		//外键
	private GoodsType goodsType;

	public Goods() {
//		super();
	}

	public Goods(String goodsName, int goodsPrice, String goodsDesc,
			String goodsImgPath, int goodsRemain) {
		super();
		this.goodsName = goodsName;
		this.goodsPrice = goodsPrice;
		this.goodsDesc = goodsDesc;
		this.goodsImgPath = goodsImgPath;
		this.goodsRemain = goodsRemain;
	}

	public int getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public String getGoodsName() {
		return goodsName;
	}

	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}

	public int getGoodsPrice() {
		return goodsPrice;
	}

	public void setGoodsPrice(int goodsPrice) {
		this.goodsPrice = goodsPrice;
	}

	public int getGoodsSales() {
		return goodsSales;
	}

	public void setGoodsSales(int goodsSales) {
		this.goodsSales = goodsSales;
	}

	public String getGoodsDesc() {
		return goodsDesc;
	}

	public void setGoodsDesc(String goodsDesc) {
		this.goodsDesc = goodsDesc;
	}

	public String getGoodsImgPath() {
		return goodsImgPath;
	}

	public void setGoodsImgPath(String goodsImgPath) {
		this.goodsImgPath = goodsImgPath;
	}

	public int getGoodsRemain() {
		return goodsRemain;
	}

	public void setGoodsRemain(int goodsRemain) {
		this.goodsRemain = goodsRemain;
	}

	public GoodsType getGoodsType() {
		return goodsType;
	}

	public void setGoodsType(GoodsType goodsType) {
		this.goodsType = goodsType;
	}

	@Override
	public String toString() {
		return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName
				+ ", goodsPrice=" + goodsPrice + ", goodsSales=" + goodsSales
				+ ", goodsDesc=" + goodsDesc + ", goodsImgPath=" + goodsImgPath
				+ ", goodsRemain=" + goodsRemain + ", goodsType=" + goodsType
				+ "]";
	}
	
	
}


OneToMany自然表示一对多

targetEntity 的参数, 该参数定义了目标实体名.通常不需要定义该参数, 因为在大部分情况下默认值(表示关联关系的属性类型)就可以很好的满足要求了.

也就是说hibernate能通过属性的类型来决定这个值,所以设不设该值问题不大。

cascade表示级联风格,ALL=save-update,delete

最主要的就是mappedBy因为没有映射文件中的 inverse,mappedBy来定义由谁来维护外键的。

在一对多的时候,多的一方是关系维护端,一的一方是关系被维护端。在关系被维护端里面有mappedBy 关系维护方对应的表中应该有外键

在本例中,GoodsType为关系被维护方,所以要在集合上声明mappedBy,表示外键由哪一方维护(这样有个好处,insert之后不会执行相应的update,就不会过于影响性能)。不设置的默认情况下,外键是由一的一方维护的(insert之后会执行相应的update

在多的一方,也就是Goods中应该会有一个属性goodsType指定它的类型(一方),同时对应的表中的外键列,即goodsTypeId

参考自关联一对多:http://guobin6125.iteye.com/blog/1597937,感谢

记录学习情况




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值