67,513
社区成员
发帖
与我相关
我的任务
分享
@Entity
@Table(name="TEACHERS")
@SequenceGenerator(name="SEQ_TEACHERS",sequenceName="SEQ_TEACHERS")
public class Teacher {
private Long id;
private String code;
private String name;
private String type;
private List<Student> students;
@Id
@GeneratedValue(generator="SEQ_TEACHERS",strategy=GenerationType.SEQUENCE)
@Column(name="id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="CODE")
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="TYPE")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="TECH_ID",nullable=true,updatable=true)
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
@Entity
@Table(name="STUDENTS")
@SequenceGenerator(name="SEQ_STUDENTS",sequenceName="SEQ_STUDENTS")
public class Student {
private Long id;
private String code;
private String name;
// @Column(name="TECH_ID")
// private Long techId;
private Teacher teacher;
@Id
@GeneratedValue(generator="SEQ_STUDENTS",strategy=GenerationType.SEQUENCE)
@Column(name="id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="CODE")
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// public Long getTechId() {
// return techId;
// }
// public void setTechId(Long techId) {
// this.techId = techId;
// }
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="TECH_ID")
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
public class AssociationsTest {
/**
* @param args
*/
public static void main(String[] args) {
// SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction trans = session.beginTransaction();
Teacher t = new Teacher();
t.setCode("000001");
t.setName("xiaoxue");
t.setType("shuxue");
List<Student> list = new ArrayList<Student>();
Student s0 = new Student();
s0.setCode("000001");
s0.setName("xioali");
list.add(s0);
Student s1 = new Student();
s1.setCode("000002");
s1.setName("zhangsan");
list.add(s1);
t.setStudents(list);
session.save(t);
trans.commit();
}
}
package com.eifashion.brand.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import com.eifashion.portal.model.FuncClassify;
@Entity(name = "brand_goods")
public class Goods implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private Brand brand;
//商品品牌分类操作
private FuncClassify funClassify;
private List<BrandTagValue> tagValue;
private List<GoodsBarcode> barcodes;
public Goods(String id) {
super();
this.id = id;
}
public Goods(){}
public Goods(String id, String name, Brand brand, FuncClassify funcClassify) {
super();
this.id = id;
this.name = name;
this.brand = brand;
this.funClassify = funcClassify;
}
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH }, fetch = FetchType.LAZY)
@JoinColumn(name = "fun_id")
public FuncClassify getFunClassify() {
return funClassify;
}
public void setFunClassify(FuncClassify funClassify) {
this.funClassify = funClassify;
}
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH }, fetch = FetchType.LAZY)
@JoinTable(name = "brand_goods_tag", joinColumns = @JoinColumn(name = "goods_id"), inverseJoinColumns = @JoinColumn(name = "tag_id"))
public List<BrandTagValue> getTagValue() {
return tagValue;
}
public void setTagValue(List<BrandTagValue> tagValue) {
this.tagValue = tagValue;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy =
"good", targetEntity = GoodsBarcode.class)
public List<GoodsBarcode> getBarcodes() {
return barcodes;
}
public void setBarcodes(List<GoodsBarcode> barcodes) {
this.barcodes = barcodes;
}
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH }, fetch = FetchType.LAZY)
@JoinColumn(name = "brand_id")
public Brand getBrand() {
return brand;
}
public void setBrand(Brand brand) {
this.brand = brand;
}
public void addBrandTag(BrandTagValue brandTag){
if(tagValue==null)
tagValue = new ArrayList<BrandTagValue>();
tagValue.add(brandTag);
}
public void addGoodsBar(GoodsBarcode goodsBarcode){
if(goodsBarcode == null)
barcodes = new ArrayList<GoodsBarcode>();
barcodes.add(goodsBarcode);
}
}
package com.eifashion.portal.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import com.eifashion.brand.model.BrandFuncClassify;
import com.eifashion.brand.model.Goods;
import com.eifashion.common.util.PinYinUtils;
/**
* 商品功能分类
*
* @author tommyzhao
*
*/
@Entity(name = "portal_func_class")
public class FuncClassify implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public final static String DEFAULT_FUNC = "nanzhuang";
private String id;
private FuncClassify parent;
private String name;
private long sort;
private List<FuncClassify> children;
private Date createTime;
private String description;
private List<Goods> goods;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy =
"funClassify", targetEntity = Goods.class)
public List<Goods> getGoods() {
return goods;
}
public void addGoods(Goods goods)
{
if(this.goods == null)
{
this.goods = new ArrayList<Goods>();
}
this.goods.add(goods);
}
public void setGoods(List<Goods> goods) {
this.goods = goods;
}
public FuncClassify() {
}
public FuncClassify(String name) {
super();
this.name = name;
this.id = PinYinUtils.toHanyuPinyinString(name);
}
public FuncClassify(String name, String description, FuncClassify parent) {
super();
this.parent = parent;
this.name = name;
this.id = PinYinUtils.toHanyuPinyinString(name);
this.description = description;
this.createTime = new Date();
}
public FuncClassify(String name, String description) {
super();
this.name = name;
this.id = PinYinUtils.toHanyuPinyinString(name);
this.description = description;
this.createTime = new Date();
}
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH }, fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
public FuncClassify getParent() {
return parent;
}
public void setParent(FuncClassify parent) {
this.parent = parent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSort() {
return sort;
}
public void setSort(long sort) {
this.sort = sort;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent", targetEntity = FuncClassify.class)
public List<FuncClassify> getChildren() {
return children;
}
public void setChildren(List<FuncClassify> children) {
this.children = children;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void addChild(FuncClassify child) {
if (children == null) {
children = new ArrayList<FuncClassify>();
}
children.add(child);
}
}
@Test
public void addGoods()
{
Brand brand = new Brand();
FuncClassify funcclass = new FuncClassify();
Goods goods = new Goods();
goods.setId("aaarr");
goods.setName("bbbb");
brand.addGoods(goods);
funcclass.addGoods(goods);
goodsDAO.addGoods(goods);
}