![](http://static.blog.csdn.net/images/category_icon.jpg)
作者同类文章
X
版权声明:本文为博主原创文章,未经博主允许不得转载。
映射关联大致分为三种 :
一对一 ,一对多, 多对多
一.一对一
drop table if exists husband;
create table husband(
id int not null primary key ,
hname varchar(20) not null collate 'utf8_bin'
);
drop table if exists wife;
create table wife(
id int not null primary key ,
wname varchar(20) not null collate 'utf8_bin',
constraint h_w_id foreign key(id)references husband(id)
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
package com.wyy.dao;
import com.wyy.po.Husband;
import com.wyy.po.Wife;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
* Created by Noa on 2017/7/21.
*/
public class HusDAO {
private Configuration cfg = null;
private SessionFactory sf = null;
public HusDAO(){
cfg = new Configuration().configure("hibernate.cfg.xml");
sf = cfg.buildSessionFactory();
}
@Test
public void add(){
Session session = null;
Transaction tran =null;
try{
session = sf.openSession();
tran = session.getTransaction();
tran.begin();
Husband husband = new Husband();
husband.setId(1);
husband.setHname("hus1");
Wife wife = new Wife();
wife.setId(1);
wife.setWname("wif1");
wife.setHunband(husband);
husband.setWife(wife);
session.save(husband);
tran.commit();
}catch (HibernateException e){
tran.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
@Test
public void delete(){
Session session = sf.openSession();
Transaction tran = session.getTransaction();
tran.begin();
Husband husband = session.load(Husband.class, 1);
session.delete(husband);
tran.commit();
session.close();
}
@Test
public void update(){
Session session = sf.openSession();
Transaction tran = session.getTransaction();
tran.begin();
Husband husband = session.load(Husband.class , 1);
husband.setHname("jack");
husband.getWife().setWname("rose");
session.update(husband);
tran.commit();
session.close();
}
@Test
public void query(){
Session session = sf.openSession();
Husband husband = session.load(Husband.class , 1);
System.out.printf("%s %s",husband.getHname(),husband.getWife().getWname());
session.close();
}
}
想要实现对主表操作,从表也跟着操作 可以设置hus实体
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id", referencedColumnName = "id", nullable = false)
public Wif getWif() {
return wif;
}
public void setWif(Wif wif) {
this.wif = wif;
}
}
- 9
二.一对多
drop table if exists father;
create table father(
fid int not null primary key ,
fname varchar(20) not null collate 'utf8_bin'
);
drop table if exists son;
create table son(
sid int not null primary key ,
sname varchar(20) not null collate 'utf8_bin',
fid int not null,
constraint f_s_id foreign key(fid)references father(fid)
)
package com.wyy.dao;
import com.wyy.po.Father;
import com.wyy.po.Son;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import java.util.Iterator;
import java.util.Set;
/**
* Created by Noa on 2017/7/22.
*/
public class FatherDal {
private Configuration cfg=null;
private SessionFactory sf=null;
public FatherDal() {
cfg=new Configuration().configure("hibernate.cfg.xml");
sf=cfg.buildSessionFactory();
}
@Test
public void add() {
Session session=sf.openSession();
Transaction tran=session.getTransaction();
tran.begin();
Father f1=new Father();
f1.setFid(7);
f1.setFname("f7");
Son s1=new Son();
s1.setSid(5);
s1.setSname("s5");
s1.setFather(f1);
Son s2=new Son();
s2.setSid(6);
s2.setSname("s6");
s2.setFather(f1);
f1.getSons().add(s1);
f1.getSons().add(s2);
session.save(f1);
tran.commit();
session.close();
}
@Test
public void delete() {
Session session=sf.openSession();
Transaction tran=session.getTransaction();
tran.begin();
Father f=session.load(Father.class,7);
session.delete(f);
tran.commit();
session.close();
}
@Test
public void update() {
Session session = sf.openSession();
Transaction tran = session.getTransaction();
tran.begin();
Father father = session.load(Father.class,1);
session.update(father);
tran.commit();
session.close();
}
@Test
public void query() {
Session session=sf.openSession();
Father f=session.load(Father.class,7);
Set<Son> sons=f.getSons();
Iterator<Son> it=sons.iterator();
while(it.hasNext()){
Son son=it.next();
System.out.println(son.getSname());
}
session.close();
}
}
实现主表操作,连动从表,设置father实体类中
@OneToMany(mappedBy = "father",cascade = CascadeType.ALL)
public Set<Son> getSons() {
return sons;
}
public void setSons(Set<Son> sons) {
this.sons = sons;
三.多对多
drop table if exists teacher;
create table teacher(
tid int not null primary key ,
tname varchar(20) not null collate 'utf8_bin'
);
drop table if exists student;
create table student(
sid int not null primary key ,
sname varchar(20) not null collate 'utf8_bin'
);
create table teacherstudent(
tid int not null,
sid int not null,
primary key(tid,sid),
constraint t_s_1 foreign key(tid) references teacher(tid),
constraint t_s_2 foreign key(sid) references student(sid)
)
- 2
- 1
package com.lyf.dao;
import com.lyf.po.Students;
import com.lyf.po.Teachers;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
* Created by fangjiejie on 2017/4/17.
*/
public class StudentsDao implements IStudentsDao {
private Configuration cfg=null;
private SessionFactory sf=null;
public StudentsDao() {
cfg=new Configuration().configure("hibernate.cfg.xml");
sf=cfg.buildSessionFactory();
}
@Test
public void add() {
Session session=sf.openSession();
Transaction tran=session.getTransaction();
tran.begin();
Teachers tea1=new Teachers();
tea1.settId(1);
tea1.settName("t1");
Teachers tea2=new Teachers();
tea2.settId(2);
tea2.settName("t2");
Students stu1=new Students();
stu1.setsId(1);
stu1.setsName("s1");
Students stu2=new Students();
stu2.setsId(2);
stu2.setsName("s2");
tea1.getStudents().add(stu1);
tea1.getStudents().add(stu2);
tea2.getStudents().add(stu1);
tea2.getStudents().add(stu2);
stu1.getTeachers().add(tea1);
stu1.getTeachers().add(tea2);
stu2.getTeachers().add(tea1);
stu2.getTeachers().add(tea2);
session.save(stu1);
session.save(stu2);
tran.commit();
session.close();
}
@Test
public void delete() {
Session session=sf.openSession();
Transaction tran=session.getTransaction();
tran.begin();
Students s=session.load(Students.class,1);
session.delete(s);
tran.commit();
session.close();
}
public void update() {
}
public void query() {
}
}
值得注意的是,如果在删除主表数据时,我们并不想,让从表数据一起删除。我们可以设置主表
@ManyToMany(cascade = CascadeType.PERSIST)//增加连动,删除不联动
@JoinTable(name = "teastu", catalog = "bz", schema = "bz", joinColumns = @JoinColumn(name = "s_id", referencedColumnName = "s_id", nullable = false), inverseJoinColumns = @JoinColumn(name = "t_id", referencedColumnName = "t_id", nullable = false))
public Set<Teachers> getTeachers() {
return teachers;
}
- 1