标签:hsi cascade thread 1.0 字段 one 指定 rem master
1.many2many
bean:
1 package com.zhidi.entity.many2many; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.GenerationType; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.JoinTable; 12 import javax.persistence.ManyToMany; 13 import javax.persistence.Table; 14 15 @Entity 16 @Table(name="tb_courses") 17 public class Course { 18 19 @Id 20 @GeneratedValue(strategy=GenerationType.AUTO) 21 private Integer id; 22 private String name; 23 24 // 学生集合 25 @ManyToMany(mappedBy="courses")//指定关联实体哪个字段维护关系 26 private Set<Student> students = new HashSet<Student>(); 27 28 public Integer getId() { 29 return id; 30 } 31 32 public void setId(Integer id) { 33 this.id = id; 34 } 35 36 public String getName() { 37 return name; 38 } 39 40 public void setName(String name) { 41 this.name = name; 42 } 43 44 public Set<Student> getStudents() { 45 return students; 46 } 47 48 public void setStudents(Set<Student> students) { 49 this.students = students; 50 } 51 52 }
1 package com.zhidi.entity.many2many; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.GenerationType; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.JoinTable; 12 import javax.persistence.ManyToMany; 13 import javax.persistence.Table; 14 15 import org.hibernate.annotations.BatchSize; 16 import org.hibernate.annotations.Cascade; 17 import org.hibernate.annotations.Fetch; 18 19 @Entity 20 @Table(name="tb_students") 21 public class Student { 22 23 @Id 24 @GeneratedValue(strategy=GenerationType.AUTO) 25 private Integer id; 26 private String name; 27 private String sex; 28 29 //课程 30 @ManyToMany 31 //多对多维护关系的是一张表 32 @JoinTable(name="tb_students_courses", 33 joinColumns=@JoinColumn(name="stu_id"), 34 inverseJoinColumns=@JoinColumn(name="cou_id")) 35 private Set<Course> courses = new HashSet<Course>(); 36 37 public Integer getId() { 38 return id; 39 } 40 41 public void setId(Integer id) { 42 this.id = id; 43 } 44 45 public String getName() { 46 return name; 47 } 48 49 public void setName(String name) { 50 this.name = name; 51 } 52 53 public String getSex() { 54 return sex; 55 } 56 57 public void setSex(String sex) { 58 this.sex = sex; 59 } 60 61 public Set<Course> getCourses() { 62 return courses; 63 } 64 65 public void setCourses(Set<Course> courses) { 66 this.courses = courses; 67 } 68 69 70 71 }
test:
1 package com.zhidi.test; 2 3 import org.junit.Test; 4 5 import com.zhidi.entity.many2many.Course; 6 import com.zhidi.entity.many2many.Student; 7 8 public class TestMany2Many extends BaseTest{ 9 10 @Test 11 public void testSave() { 12 Student student = new Student(); 13 student.setName("金三胖"); 14 student.setSex("男"); 15 16 Course course = new Course(); 17 course.setName("音乐"); 18 19 student.getCourses().add(course); 20 21 22 session.save(course); 23 session.save(student); 24 25 } 26 27 @Test 28 public void test() { 29 30 } 31 }
2.many2one
bean:
1 package com.zhidi.entity.many2one; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.GenerationType; 6 import javax.persistence.Id; 7 import javax.persistence.Table; 8 9 @Entity 10 @Table(name="dept") 11 public class Dept { 12 13 @Id 14 @GeneratedValue(strategy=GenerationType.AUTO) 15 private Integer deptno; 16 private String dname; 17 private String loc; 18 19 public Integer getDeptno() { 20 return deptno; 21 } 22 23 public void setDeptno(Integer deptno) { 24 this.deptno = deptno; 25 } 26 27 public String getDname() { 28 return dname; 29 } 30 31 public void setDname(String dname) { 32 this.dname = dname; 33 } 34 35 public String getLoc() { 36 return loc; 37 } 38 39 public void setLoc(String loc) { 40 this.loc = loc; 41 } 42 43 }
1 package com.zhidi.entity.many2one; 2 3 import java.util.Date; 4 5 import javax.persistence.Entity; 6 import javax.persistence.FetchType; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.GenerationType; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.ManyToOne; 12 import javax.persistence.Table; 13 import javax.persistence.Temporal; 14 import javax.persistence.TemporalType; 15 16 import org.hibernate.annotations.Cascade; 17 import org.hibernate.annotations.CascadeType; 18 19 /** 20 * 多对一关单向关联关系中的->多方 21 * 22 * @author lx 23 * 24 */ 25 @Entity 26 @Table(name="emp") 27 public class Emp { 28 29 @Id 30 @GeneratedValue(strategy = GenerationType.AUTO) 31 private Integer empno; 32 private String ename; 33 private String job; 34 private Integer mgr; 35 private Double sal; 36 @Temporal(TemporalType.DATE) 37 private Date hiredate; 38 39 // 提供类型为Dept的成员变量 40 @ManyToOne(fetch=FetchType.LAZY) 41 @JoinColumn(name="deptno") 42 @Cascade({CascadeType.SAVE_UPDATE}) 43 private Dept dept; 44 45 public Integer getEmpno() { 46 return empno; 47 } 48 49 public void setEmpno(Integer empno) { 50 this.empno = empno; 51 } 52 53 public String getEname() { 54 return ename; 55 } 56 57 public void setEname(String ename) { 58 this.ename = ename; 59 } 60 61 public String getJob() { 62 return job; 63 } 64 65 public void setJob(String job) { 66 this.job = job; 67 } 68 69 public Integer getMgr() { 70 return mgr; 71 } 72 73 public void setMgr(Integer mgr) { 74 this.mgr = mgr; 75 } 76 77 public Double getSal() { 78 return sal; 79 } 80 81 public void setSal(Double sal) { 82 this.sal = sal; 83 } 84 85 public Date getHiredate() { 86 return hiredate; 87 } 88 89 public void setHiredate(Date hiredate) { 90 this.hiredate = hiredate; 91 } 92 93 public Dept getDept() { 94 return dept; 95 } 96 97 public void setDept(Dept dept) { 98 this.dept = dept; 99 } 100 101 }
test:
1 package com.zhidi.test; 2 3 import java.util.Date; 4 5 import javax.persistence.CascadeType; 6 7 import org.junit.Test; 8 9 import com.zhidi.entity.many2one.Dept; 10 import com.zhidi.entity.many2one.Emp; 11 12 public class TestMany2One extends BaseTest { 13 14 /** 15 * 没有设置级联保存 16 */ 17 @Test 18 public void testSave() { 19 Emp emp = new Emp(); 20 emp.setEname("隔壁老王"); 21 emp.setJob("好心人"); 22 emp.setSal(1000d); 23 emp.setHiredate(new Date()); 24 25 Dept dept = new Dept(); 26 dept.setDname("居委会"); 27 dept.setLoc("武汉"); 28 29 emp.setDept(dept); 30 31 session.save(dept); 32 session.save(emp); 33 34 } 35 36 /** 37 * 级联保存 38 */ 39 @Test 40 public void testSave2() { 41 Emp emp = new Emp(); 42 emp.setEname("隔壁老王"); 43 emp.setJob("好心人"); 44 emp.setSal(1000d); 45 emp.setHiredate(new Date()); 46 47 Dept dept = new Dept(); 48 dept.setDname("居委会"); 49 dept.setLoc("武汉"); 50 51 emp.setDept(dept); 52 //设置级联保存为JTA标准的CascadeType.PERSIST 53 // session.persist(emp); 54 55 //Hibernate包下的CascadeType.SAVE_UPDATE 56 session.save(emp); 57 58 } 59 60 /** 61 * 设置延迟加载 62 */ 63 @Test 64 public void testFetch() { 65 //如果采用默认的抓取策略(EAGER),相当于在配置文件中设置fetch=join, 66 //不会延迟加载关联属性,发送一个左外连接直接查询出关联属性 67 Emp emp = (Emp) session.get(Emp.class, 7987); 68 System.out.println(emp.getDept().getDname()); 69 } 70 }
bothway
1 package com.zhidi.test; 2 3 import java.util.Date; 4 5 import javax.persistence.CascadeType; 6 7 import org.junit.Test; 8 9 import com.zhidi.entity.many2one.Dept; 10 import com.zhidi.entity.many2one.Emp; 11 12 public class TestMany2One extends BaseTest { 13 14 /** 15 * 没有设置级联保存 16 */ 17 @Test 18 public void testSave() { 19 Emp emp = new Emp(); 20 emp.setEname("隔壁老王"); 21 emp.setJob("好心人"); 22 emp.setSal(1000d); 23 emp.setHiredate(new Date()); 24 25 Dept dept = new Dept(); 26 dept.setDname("居委会"); 27 dept.setLoc("武汉"); 28 29 emp.setDept(dept); 30 31 session.save(dept); 32 session.save(emp); 33 34 } 35 36 /** 37 * 级联保存 38 */ 39 @Test 40 public void testSave2() { 41 Emp emp = new Emp(); 42 emp.setEname("隔壁老王"); 43 emp.setJob("好心人"); 44 emp.setSal(1000d); 45 emp.setHiredate(new Date()); 46 47 Dept dept = new Dept(); 48 dept.setDname("居委会"); 49 dept.setLoc("武汉"); 50 51 emp.setDept(dept); 52 //设置级联保存为JTA标准的CascadeType.PERSIST 53 // session.persist(emp); 54 55 //Hibernate包下的CascadeType.SAVE_UPDATE 56 session.save(emp); 57 58 } 59 60 /** 61 * 设置延迟加载 62 */ 63 @Test 64 public void testFetch() { 65 //如果采用默认的抓取策略(EAGER),相当于在配置文件中设置fetch=join, 66 //不会延迟加载关联属性,发送一个左外连接直接查询出关联属性 67 Emp emp = (Emp) session.get(Emp.class, 7987); 68 System.out.println(emp.getDept().getDname()); 69 } 70 }
1 package com.zhidi.entity.many2one.bothway; 2 3 import java.util.Date; 4 5 import javax.persistence.Entity; 6 import javax.persistence.FetchType; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.GenerationType; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.ManyToOne; 12 import javax.persistence.Table; 13 import javax.persistence.Temporal; 14 import javax.persistence.TemporalType; 15 16 import org.hibernate.annotations.Cascade; 17 import org.hibernate.annotations.CascadeType; 18 19 /** 20 * 多对一关双向关联关系中的->多方 21 * 22 * @author lx 23 * 24 */ 25 @Entity 26 @Table(name = "emp") 27 public class Emp { 28 29 @Id 30 @GeneratedValue(strategy = GenerationType.AUTO) 31 private Integer empno; 32 private String ename; 33 private String job; 34 private Integer mgr; 35 private Double sal; 36 @Temporal(TemporalType.DATE) 37 private Date hiredate; 38 39 // 持有部门的引用 40 @ManyToOne 41 @JoinColumn(name="deptno") 42 //设置级联保存 43 @Cascade(CascadeType.ALL) 44 private Dept dept; 45 46 public Integer getEmpno() { 47 return empno; 48 } 49 50 public void setEmpno(Integer empno) { 51 this.empno = empno; 52 } 53 54 public String getEname() { 55 return ename; 56 } 57 58 public void setEname(String ename) { 59 this.ename = ename; 60 } 61 62 public String getJob() { 63 return job; 64 } 65 66 public void setJob(String job) { 67 this.job = job; 68 } 69 70 public Integer getMgr() { 71 return mgr; 72 } 73 74 public void setMgr(Integer mgr) { 75 this.mgr = mgr; 76 } 77 78 public Double getSal() { 79 return sal; 80 } 81 82 public void setSal(Double sal) { 83 this.sal = sal; 84 } 85 86 public Date getHiredate() { 87 return hiredate; 88 } 89 90 public void setHiredate(Date hiredate) { 91 this.hiredate = hiredate; 92 } 93 94 public Dept getDept() { 95 return dept; 96 } 97 98 public void setDept(Dept dept) { 99 this.dept = dept; 100 } 101 102 }
1 package com.zhidi.test; 2 3 import java.util.Date; 4 5 import org.junit.Test; 6 7 import com.zhidi.entity.many2one.bothway.Dept; 8 import com.zhidi.entity.many2one.bothway.Emp; 9 10 public class TestMany2OneBoth extends BaseTest { 11 12 @Test 13 public void testSave() { 14 Emp emp = new Emp(); 15 emp.setEname("李二狗2"); 16 emp.setHiredate(new Date()); 17 emp.setJob("打酱油的"); 18 emp.setSal(100d); 19 20 Dept dept = new Dept(); 21 dept.setDname("阿里巴巴2"); 22 dept.setLoc("武汉"); 23 24 //员工来维护关系 25 emp.setDept(dept); 26 27 //保存 28 // session.save(dept); 29 session.save(emp); 30 31 } 32 33 @Test 34 public void testDelete() { 35 Emp emp = (Emp) session.get(Emp.class, 7991); 36 session.delete(emp); 37 } 38 }
3.one2many
bean:
1 package com.zhidi.entity.one2many; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.GenerationType; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.OneToMany; 12 import javax.persistence.Table; 13 14 import org.hibernate.annotations.BatchSize; 15 import org.hibernate.annotations.Cascade; 16 import org.hibernate.annotations.CascadeType; 17 18 @Entity 19 @Table(name="dept") 20 public class Dept { 21 22 @Id 23 @GeneratedValue(strategy=GenerationType.AUTO) 24 private Integer deptno; 25 private String dname; 26 private String loc; 27 28 //一对多映射 29 @OneToMany //相当于<set><one-to-many/></set> 30 //指定对方表中的外键字段 31 @JoinColumn(name="deptno") 32 //设置级联保存 33 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE}) 34 //设置批量抓取关联数据 35 @BatchSize(size=2) 36 private Set<Emp> emps = new HashSet<>(); 37 38 public Integer getDeptno() { 39 return deptno; 40 } 41 42 public void setDeptno(Integer deptno) { 43 this.deptno = deptno; 44 } 45 46 public String getDname() { 47 return dname; 48 } 49 50 public void setDname(String dname) { 51 this.dname = dname; 52 } 53 54 public String getLoc() { 55 return loc; 56 } 57 58 public void setLoc(String loc) { 59 this.loc = loc; 60 } 61 62 public Set<Emp> getEmps() { 63 return emps; 64 } 65 66 public void setEmps(Set<Emp> emps) { 67 this.emps = emps; 68 } 69 70 71 72 }
1 package com.zhidi.entity.one2many; 2 3 import java.util.Date; 4 5 import javax.persistence.Entity; 6 import javax.persistence.FetchType; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.GenerationType; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.ManyToOne; 12 import javax.persistence.OneToMany; 13 import javax.persistence.Table; 14 import javax.persistence.Temporal; 15 import javax.persistence.TemporalType; 16 17 import org.hibernate.annotations.Cascade; 18 import org.hibernate.annotations.CascadeType; 19 20 @Entity 21 @Table(name="emp") 22 public class Emp { 23 24 @Id 25 @GeneratedValue(strategy = GenerationType.AUTO) 26 private Integer empno; 27 private String ename; 28 private String job; 29 private Integer mgr; 30 private Double sal; 31 @Temporal(TemporalType.DATE) 32 private Date hiredate; 33 34 public Integer getEmpno() { 35 return empno; 36 } 37 38 public void setEmpno(Integer empno) { 39 this.empno = empno; 40 } 41 42 public String getEname() { 43 return ename; 44 } 45 46 public void setEname(String ename) { 47 this.ename = ename; 48 } 49 50 public String getJob() { 51 return job; 52 } 53 54 public void setJob(String job) { 55 this.job = job; 56 } 57 58 public Integer getMgr() { 59 return mgr; 60 } 61 62 public void setMgr(Integer mgr) { 63 this.mgr = mgr; 64 } 65 66 public Double getSal() { 67 return sal; 68 } 69 70 public void setSal(Double sal) { 71 this.sal = sal; 72 } 73 74 public Date getHiredate() { 75 return hiredate; 76 } 77 78 public void setHiredate(Date hiredate) { 79 this.hiredate = hiredate; 80 } 81 82 }
test:
1 package com.zhidi.test; 2 3 import java.util.Date; 4 import java.util.List; 5 6 import org.junit.Test; 7 8 import com.zhidi.entity.one2many.Dept; 9 import com.zhidi.entity.one2many.Emp; 10 11 public class TestOne2Many extends BaseTest { 12 13 /** 14 * 没有设置级联保存 15 */ 16 @Test 17 public void testSave() { 18 Emp emp = new Emp(); 19 emp.setEname("赵特助"); 20 emp.setHiredate(new Date()); 21 emp.setJob("打酱油的"); 22 emp.setSal(100d); 23 24 Dept dept = new Dept(); 25 dept.setDname("智递科技"); 26 dept.setLoc("武汉"); 27 28 //dept管理关系 29 dept.getEmps().add(emp); 30 31 session.save(dept); 32 session.save(emp); 33 } 34 35 /** 36 * 设置级联保存 37 */ 38 @Test 39 public void testSaveCascadeBySaveUpdate() { 40 Emp emp = new Emp(); 41 emp.setEname("赵特助"); 42 emp.setHiredate(new Date()); 43 emp.setJob("打酱油的"); 44 emp.setSal(100d); 45 46 Dept dept = new Dept(); 47 dept.setDname("智递科技"); 48 dept.setLoc("武汉"); 49 50 //dept管理关系 51 dept.getEmps().add(emp); 52 53 session.save(dept); 54 } 55 56 /** 57 * 设置级联删除 58 */ 59 @Test 60 public void testDelete() { 61 Dept dept = (Dept) session.get(Dept.class, 107); 62 session.delete(dept); 63 } 64 65 /** 66 * 测试延迟加载 67 */ 68 @Test 69 public void testQuery() { 70 //@OnetoMany注解fetch默认值为LAZY,默认会延迟加载关联实体 71 Dept dept = (Dept) session.get(Dept.class, 106); 72 } 73 74 /** 75 * 测试批量抓取关联数据 76 */ 77 @Test 78 public void testBatch() { 79 List<Dept> depts = session.createQuery("from Dept").list(); 80 for (Dept dept : depts) { 81 dept.getEmps().iterator(); 82 } 83 } 84 }
4.one2one
bean:
1 package com.zhidi.entity.one2one; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.GenerationType; 6 import javax.persistence.Id; 7 import javax.persistence.JoinColumn; 8 import javax.persistence.OneToOne; 9 import javax.persistence.Table; 10 11 @Entity 12 @Table(name="tb_clazz") 13 public class Clazz { 14 15 @Id 16 @GeneratedValue(strategy = GenerationType.AUTO) 17 private Integer id; 18 private String clazzName; 19 20 @OneToOne 21 @JoinColumn(name="master_id", unique=true)//从表中的外键只能出现一次,所有是唯一的unique=true 22 private Master master; 23 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 public String getClazzName() { 33 return clazzName; 34 } 35 36 public void setClazzName(String clazzName) { 37 this.clazzName = clazzName; 38 } 39 40 public Master getMaster() { 41 return master; 42 } 43 44 public void setMaster(Master master) { 45 this.master = master; 46 } 47 48 }
1 package com.zhidi.entity.one2one; 2 3 import java.sql.Timestamp; 4 5 import javax.persistence.Entity; 6 import javax.persistence.GeneratedValue; 7 import javax.persistence.GenerationType; 8 import javax.persistence.Id; 9 import javax.persistence.OneToOne; 10 import javax.persistence.Table; 11 import javax.persistence.Version; 12 13 /** 14 * 一对一双向关联,当前实体为主表 15 * @author lx 16 * 17 */ 18 @Entity 19 @Table(name="tb_master") 20 public class Master { 21 22 @Id 23 @GeneratedValue(strategy=GenerationType.AUTO) 24 private Integer id; 25 private String masterName; 26 27 @Version 28 private Timestamp version; 29 30 @OneToOne(mappedBy="master") 31 private Clazz clz; 32 33 public Integer getId() { 34 return id; 35 } 36 37 public void setId(Integer id) { 38 this.id = id; 39 } 40 41 public String getMasterName() { 42 return masterName; 43 } 44 45 public void setMasterName(String masterName) { 46 this.masterName = masterName; 47 } 48 49 public Clazz getClz() { 50 return clz; 51 } 52 53 public void setClz(Clazz clz) { 54 this.clz = clz; 55 } 56 57 }
test:
1 package com.zhidi.test; 2 3 import org.junit.Test; 4 5 import com.zhidi.entity.one2one.Clazz; 6 import com.zhidi.entity.one2one.Master; 7 8 public class TestOne2One extends BaseTest { 9 10 @Test 11 public void testSave() { 12 Clazz clz = new Clazz(); 13 clz.setClazzName("研发一"); 14 15 Master master = new Master(); 16 master.setMasterName("老梅"); 17 18 clz.setMaster(master); 19 20 session.save(master); 21 session.save(clz); 22 } 23 24 @Test 25 public void testHappyLock() { 26 Master master = (Master) session.get(Master.class, 1); 27 master.setMasterName("德玛2"); 28 } 29 }
Student
1 package com.zhidi.entity; 2 3 import java.util.Date; 4 5 import javax.persistence.Basic; 6 import javax.persistence.Column; 7 import javax.persistence.Entity; 8 import javax.persistence.GeneratedValue; 9 import javax.persistence.GenerationType; 10 import javax.persistence.Id; 11 import javax.persistence.Table; 12 import javax.persistence.Temporal; 13 import javax.persistence.TemporalType; 14 15 //把实体标记为一个Bean 16 @Entity 17 //指定实体对应的数据库表 18 @Table(name="students") 19 public class Student { 20 21 //指定属性为主键字段 22 @Id 23 //指定主键生成策略,GenerationType.AUTO=native 24 @GeneratedValue(strategy=GenerationType.AUTO) 25 private Integer id; 26 //相当于xml中<property/>,可省略 27 @Basic 28 @Column(name="name") 29 private String name; 30 @Column 31 private String sex; 32 //如果字段名和数据库列名一致,可直接省略@Column注解 33 //指定时间类型 34 @Temporal(TemporalType.DATE) 35 private Date birthday; 36 37 public Integer getId() { 38 return id; 39 } 40 41 public void setId(Integer id) { 42 this.id = id; 43 } 44 45 public String getName() { 46 return name; 47 } 48 49 public void setName(String name) { 50 this.name = name; 51 } 52 53 public String getSex() { 54 return sex; 55 } 56 57 public void setSex(String sex) { 58 this.sex = sex; 59 } 60 61 public Date getBirthday() { 62 return birthday; 63 } 64 65 public void setBirthday(Date birthday) { 66 this.birthday = birthday; 67 } 68 69 }
1 package com.zhidi.test; 2 3 import java.util.Date; 4 5 import org.junit.Test; 6 7 import com.zhidi.entity.Student; 8 9 public class TestStudent extends BaseTest { 10 11 @Test 12 public void testSave() { 13 Student stu = new Student(); 14 stu.setName("小明"); 15 stu.setSex("男"); 16 stu.setBirthday(new Date()); 17 18 session.save(stu); 19 } 20 }
BaseTest
1 package com.zhidi.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 6 import org.hibernate.cfg.Configuration; 7 import org.junit.After; 8 import org.junit.AfterClass; 9 import org.junit.Before; 10 import org.junit.BeforeClass; 11 12 public class BaseTest { 13 14 protected static SessionFactory sessionFactory; 15 protected Session session; 16 17 @BeforeClass 18 public static void beforeClass() { 19 Configuration cfg = new Configuration().configure(); 20 //初始化会话工厂 21 sessionFactory = cfg 22 .buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build()); 23 } 24 25 @AfterClass 26 public static void afterClass() { 27 //如果会话工厂不为空,就关闭 28 if (sessionFactory != null) { 29 sessionFactory.close(); 30 } 31 } 32 33 @Before 34 public void beforeMethod() { 35 //获取session 36 session = sessionFactory.getCurrentSession(); 37 session.beginTransaction(); 38 } 39 40 @After 41 public void afterMethod() { 42 //有存活的事务就提交 43 if (session.getTransaction().isActive()) { 44 session.getTransaction().commit(); 45 } 46 } 47 48 49 }
hibernate.cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE hibernate-configuration PUBLIC 4 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 5 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 6 7 <hibernate-configuration> 8 <session-factory> 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="connection.url">jdbc:mysql://localhost:3306/db_hibernate</property> 11 <property name="connection.username">root</property> 12 <property name="connection.password">root</property> 13 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 14 <property name="current_session_context_class">thread</property> 15 <property name="format_sql">true</property> 16 <property name="connection.pool_size">1</property> 17 18 <!-- 自动检测表的状态 --> 19 <!-- 属性值有3:create,update,none --> 20 <property name="hbm2ddl.auto">none</property> 21 22 <!-- 将配置的类包含到核心配置文件 --> 23 <!-- <mapping class="com.zhidi.entity.Student"/> --> 24 25 <!-- <mapping class="com.zhidi.entity.many2one.Dept"/> 26 <mapping class="com.zhidi.entity.many2one.Emp"/> --> 27 28 <!-- <mapping class="com.zhidi.entity.one2many.Dept"/> 29 <mapping class="com.zhidi.entity.one2many.Emp"/> --> 30 31 <!-- <mapping class="com.zhidi.entity.many2one.bothway.Dept"/> 32 <mapping class="com.zhidi.entity.many2one.bothway.Emp"/> --> 33 34 <!-- <mapping class="com.zhidi.entity.many2many.Course"/> 35 <mapping class="com.zhidi.entity.many2many.Student"/> --> 36 37 <mapping class="com.zhidi.entity.one2one.Clazz"/> 38 <mapping class="com.zhidi.entity.one2one.Master"/> 39 40 </session-factory> 41 </hibernate-configuration>
log4j.properties
1 ### direct log messages to stdout ### 2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 log4j.appender.stdout.Target=System.out 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 6 7 log4j.rootLogger=warn, stdout 8 9 log4j.logger.org.hibernate=debug 10 11 log4j.logger.org.hibernate.tool.hbm2ddl=debug
标签:hsi cascade thread 1.0 字段 one 指定 rem master
原文地址:http://www.cnblogs.com/zclqian/p/7449825.html