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();
} }
一对多,多对弈,结合起来就是一对多双向外键