hibernate之一对多注解

ClssRoom.java package com.otm;

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.JoinColumn; import javax.persistence.OneToMany;

import org.hibernate.annotations.GenericGenerator;

@Entity public class ClassRoom { @Id @GeneratedValue(generator="cid") @GenericGenerator(name="cid",strategy="assigned") @Column(length=5) private String cid; private String cname; @OneToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY) @JoinColumn(name="cid") private Set<Mao> st;

public Set<Mao> getSt() {
	return st;
}

public void setSt(Set<Mao> st) {
	this.st = st;
}

public ClassRoom(){
	
}

public ClassRoom(String cid, String cname) {
	super();
	this.cid = cid;
	this.cname = cname;
}

public String getCid() {
	return cid;
}

public void setCid(String cid) {
	this.cid = cid;
}

public String getCname() {
	return cname;
}

public void setCname(String cname) {
	this.cname = cname;
}

} 2 Mao.java package com.otm; import javax.persistence.Entity; import javax.persistence.Id;

@Entity public class Mao { private int sid; private String phone; private String post; public Mao(){ }

public Mao(int sid, String phone, String post) {
	this.sid = sid;
	this.phone = phone;
	this.post = post;
}
@Id
public int getSid() {
	return sid;
}

public void setSid(int sid) {
	this.sid = sid;
}

public String getPhone() {
	return phone;
}

public void setPhone(String phone) {
	this.phone = phone;
}

public String getPost() {
	return post;
}

public void setPost(String post) {
	this.post = post;
}

}

3 Test.java package com.otm;

import java.util.HashSet;

import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.mapping.Set; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.tool.hbm2ddl.SchemaExport;

import com.otm.Mao;

public class Test { @org.junit.Test public void test(){ Configuration config=new Configuration().configure(); ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); SchemaExport export=new SchemaExport(config); export.create(true,true); sessionFactory.close(); }

@org.junit.Test public void add(){ Configuration config=new Configuration().configure(); ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); Session session=sessionFactory.openSession(); Transaction tx=session.beginTransaction(); ClassRoom c2=new ClassRoom("0001","runagong"); ClassRoom c1=new ClassRoom("0002","jike"); Mao mao=new Mao(1,"33","43"); Mao mao1=new Mao(2,"33","43"); Mao mao3=new Mao(3,"33","43"); Mao mao2=new Mao(4,"34","rwe"); HashSet<Mao> st1=new HashSet<Mao>(); st1.add(mao); st1.add(mao1); c2.setSt(st1); HashSet<Mao> st2=new HashSet<Mao>(); st2.add(mao3); st2.add(mao2); c1.setSt(st2);

  session.save(mao);
  session.save(mao1);
  session.save(mao2);
  session.save(mao3);

  session.save(c2);
  session.save(c1);
 
 tx.commit();
 session.close();
 sessionFactory.close();

} }

一对多,多对弈,结合起来就是一对多双向外键

转载于:https://my.oschina.net/u/2511906/blog/842800

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值