双向关联现实一对多,注解现实

来源:互联网 发布:申请淘宝达人后怎么做 编辑:程序博客网 时间:2024/05/21 09:13

有两个实体:GoodsType -商品类别,Goods-商品信息

========================================  一方

package org.cgh.ssh.pojo;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.GenerationType;
import javax.persistence.OneToMany;
import javax.persistence.Table;


import org.hibernate.annotations.GenericGenerator;
/**
 * 商品类别
 * @author cgh
 *
 */
@Entity
public class GoodsType {
@Id
@GeneratedValue(generator="hib_uuid")
@GenericGenerator(name="hib_uuid", strategy = "uuid")
@Column(length=32)
private String gtId;//商品类别编号
@Column(length=100,nullable=false,unique=true)
private String gtName;//类别名称
@Column(length=200,nullable=false)
private String gtImg;//图文描述
//商品类别对商品,一对多
@OneToMany(cascade = { CascadeType.ALL}, fetch = FetchType.EAGER ,targetEntity = Goods.class,mappedBy="goodsType")
private Set<Goods> goods;


public Set<Goods> getGoods() {
return goods;
}
public void setGoods(Set<Goods> goods) {
this.goods = goods;
}
public String getGtId() {
return gtId;
}
public void setGtId(String gtId) {
this.gtId = gtId;
}
public String getGtName() {
return gtName;
}
public void setGtName(String gtName) {
this.gtName = gtName;
}
public String getGtImg() {
return gtImg;
}
public void setGtImg(String gtImg) {
this.gtImg = gtImg;
}
}



========================================  多方

package org.cgh.ssh.pojo;
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.GenericGenerator;
import org.springframework.context.annotation.Lazy;
/**
 * 商品信息
 * @author cgh
 *
 */
@Entity
public class Goods {
@Id
@GeneratedValue(generator="hib_uuid")
@GenericGenerator(name="hib_uuid", strategy = "uuid")
@Column(length=32)
private String goodsId;//商品编号
@Column(length=100,nullable=false)
private String goodsName;//商品名称
@Column(length=200,nullable=false)
private String goodsImg;//商品图片
@Column(length=500,nullable=false)
private String describe;//描述
//商品对类型,多对一
@ManyToOne
@JoinColumn(name="gtId",nullable=false)
private GoodsType goodsType;//商品类型对象

public GoodsType getGoodsType() {
return goodsType;
}
public void setGoodsType(GoodsType goodsType) {
this.goodsType = goodsType;
}
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getGoodsImg() {
return goodsImg;
}
public void setGoodsImg(String goodsImg) {
this.goodsImg = goodsImg;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
}

0 0