一、一对多关系
1、在上一篇日志中用.xml配置文件项目基础上,再往lib目录先添加一个包-hibernate-jpa-2.0-api-1.0.0.Final.jar
2、新建一个com.st.bean4 包,往包里面添加两个含有注解的类:
a)、DeptBean2类:
1 package com.st.bean4; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 7 import javax.persistence.Column; 8 import javax.persistence.Entity; 9 import javax.persistence.GeneratedValue; 10 import javax.persistence.Id; 11 import javax.persistence.JoinColumn; 12 import javax.persistence.OneToMany; 13 import javax.persistence.Table; 14 15 import org.hibernate.annotations.Cascade; 16 import org.hibernate.annotations.CascadeType; 17 import org.hibernate.annotations.GenericGenerator; 18 19 @Entity //指定实体类 20 @Table(name="DEPT") //对应表的名称 21 @GenericGenerator(name="genID", strategy="increment") //声明主键生成策略 22 public class DeptBean2 { 23 @Id //指定主键 24 @GeneratedValue(generator="genID") //设定主键生成策略 25 @Column(name="ID") //指定类中的属性与表中的列的对应关系 26 private long id; 27 @Column(name="NAME") //指定类中的属性与表中的列的对应关系 28 private String name; 29 30 @OneToMany //指定一对多关系 31 @Cascade(value={CascadeType.SAVE_UPDATE}) //设定级联关系 32 @JoinColumn(name="dept_id") //指定与本类主键所对应的外表的外键 33 private Set<EmployeeBean2> emp = new HashSet<EmployeeBean2>(); 34 @Override 35 public String toString() { 36 return "DeptBean [id=" + id + ", name=" + name +"]"; 37 } 38 public long getId() { 39 return id; 40 } 41 public void setId(long id) { 42 this.id = id; 43 } 44 public String getName() { 45 return name; 46 } 47 public void setName(String name) { 48 this.name = name; 49 } 50 public Set<EmployeeBean2> getEmp() { 51 return emp; 52 } 53 public void setEmp(Set<EmployeeBean2> emp) { 54 this.emp = emp; 55 } 56 }
b)、EmployeeBean类:
1 package com.st.bean4; 2 3 import javax.persistence.Column; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.JoinColumn; 8 import javax.persistence.ManyToOne; 9 import javax.persistence.Table; 10 11 import org.hibernate.annotations.GenericGenerator; 12 13 @Entity //指定一个实体 14 @Table(name="employee") //指定表的名称 15 @GenericGenerator(name="genID", strategy="increment") //声明主键生成策略 16 public class EmployeeBean2 { 17 @Id //指定主键 18 @GeneratedValue(generator="genID") //设定主键生成策略 19 @Column(name="ID") //类中的属性和表中的列名的对应关系 20 private long id; 21 @Column(name="NAME") 22 private String name; 23 @Column(name="SEX") 24 private String sex; 25 @Column(name="JOB") 26 private String job; 27 @ManyToOne// 指定多对一关系 //指定多对一关系 28 @JoinColumn(name="DEPT_ID") 29 //一个员工对应于一个部门号,所以这里不用集合 30 private DeptBean2 dept ; //注意这个地方不要new对象,否则会无法运行 31 public long getId() { 32 return id; 33 } 34 public void setId(long id) { 35 this.id = id; 36 } 37 public String getName() { 38 return name; 39 } 40 public void setName(String name) { 41 this.name = name; 42 } 43 public String getSex() { 44 return sex; 45 } 46 public void setSex(String sex) { 47 this.sex = sex; 48 } 49 public String getJob() { 50 return job; 51 } 52 public void setJob(String job) { 53 this.job = job; 54 } 55 public DeptBean2 getDept() { 56 return dept; 57 } 58 public void setDept(DeptBean2 dept) { 59 this.dept = dept; 60 } 61 @Override 62 public String toString() { 63 return "EmployeeBean [id=" + id + ", name=" + name + ", sex=" + sex 64 + ", job=" + job + ", dept=" + dept 65 + "]"; 66 } 67 }
3、在hibernate.cfg.xml文件中引入上面两个类的映射
1 <mapping class="com.st.bean5.UserBean2" /> 2 <mapping class="com.st.bean5.RoleBean2" />
4、在BeanTest中添加相应的测试方法:
1 @Test 2 public void bean4test1(){ 3 Session session = HibernateTools.openSession(); 4 Transaction tran = session.beginTransaction(); 5 //首先在dept中新增一条数据,再关联的在employee中新增一条数据 6 //DeptBean2 dept = new DeptBean2(); 7 //先读去dept中的数据,再在读取的基础上关联的在employee中新增一条数据 8 DeptBean2 dept = (DeptBean2) session.get(DeptBean2.class,1L); //1L代表主键 9 EmployeeBean2 emp = new EmployeeBean2(); 10 //dept.setName("技术部"); 11 emp.setName("陈泽俊"); 12 emp.setSex("男"); 13 emp.setJob("STM32"); 14 dept.getEmp().add(emp); 15 session.save(dept); 16 //确认提交事物 17 tran.commit(); 18 }
二、多对多关系
1、新建一个com.st.bean4 包,往包里面添加两个含有注解的类
a)、UserBean2:
1 package com.st.bean5; 2 import java.util.HashSet; 3 import java.util.Set; 4 5 import javax.persistence.Column; 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.JoinColumn; 10 import javax.persistence.JoinTable; 11 import javax.persistence.ManyToMany; 12 import javax.persistence.Table; 13 14 import org.hibernate.annotations.Cascade; 15 import org.hibernate.annotations.CascadeType; 16 import org.hibernate.annotations.GenericGenerator; 17 18 @Entity //实体 19 @Table(name="T_USER") //表名 20 @GenericGenerator(name="genID", strategy="increment") //声明主键生成策略 21 public class UserBean2 { 22 @Id //指定主键 23 @GeneratedValue(generator="genID") //设定主键生成策略 24 @Column(name="ID") //指定类的属性和表的字段的对应关系 25 private long id; 26 @Column(name="NAME") 27 private String name; 28 @Column(name="SEX") 29 private String sex; 30 @ManyToMany //指定多对多关系 31 @Cascade(value={CascadeType.SAVE_UPDATE}) //设置级联关系 32 @JoinTable(name="USER_ROLE", //指定第三张表 33 joinColumns={@JoinColumn(name="USER_ID")}, //本表与中间表的外键对应 34 inverseJoinColumns={@JoinColumn(name="ROLE_ID")}) //另一张表与第三张表的外键的对应关系 35 private Set<RoleBean2> role = new HashSet<RoleBean2>(); 36 public long getId() { 37 return id; 38 } 39 public void setId(long id) { 40 this.id = id; 41 } 42 public String getName() { 43 return name; 44 } 45 public void setName(String name) { 46 this.name = name; 47 } 48 public String getSex() { 49 return sex; 50 } 51 public void setSex(String sex) { 52 this.sex = sex; 53 } 54 public Set<RoleBean2> getRole() { 55 return role; 56 } 57 public void setRole(Set<RoleBean2> role) { 58 this.role = role; 59 } 60 @Override 61 public String toString() { 62 return "UserBean [id=" + id + ", name=" + name + ", sex=" + sex 63 + ", role=" + role + "]"; 64 } 65 }
b)、RoleBean类:
1 package com.st.bean5; 2 import java.util.HashSet; 3 4 import java.util.Set; 5 6 import javax.persistence.Column; 7 import javax.persistence.Entity; 8 import javax.persistence.GeneratedValue; 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.Cascade; 16 import org.hibernate.annotations.CascadeType; 17 import org.hibernate.annotations.GenericGenerator; 18 19 @Entity //实体 20 @Table(name="T_ROLE") //表名 21 @GenericGenerator(name="genID", strategy="increment")//声明主键生成策略 22 public class RoleBean2 { 23 @Id //主键 24 @GeneratedValue(generator="genID") //设置主键生成策略 25 @Column(name="ID") //类中的属性与表的字段的对应关系 26 private long id; 27 @Column(name="POST") 28 private String post;//职位 29 @Column(name="PAY") 30 private int pay; //薪资 31 @ManyToMany //多对多关系 32 @Cascade(value={CascadeType.SAVE_UPDATE}) //级联关系 33 @JoinTable(name="USER_ROLE", //中间表的名称 34 joinColumns={@JoinColumn(name="ROLE_ID")}, //本表与中间表的外键对应关系 35 inverseJoinColumns={@JoinColumn(name="USER_ID")}) //另一张表与中间表的外键的对应关系 36 private Set<UserBean2> user = new HashSet<UserBean2>(); 37 38 public long getId() { 39 return id; 40 } 41 public void setId(long id) { 42 this.id = id; 43 } 44 public String getPost() { 45 return post; 46 } 47 public void setPost(String post) { 48 this.post = post; 49 } 50 public int getPay() { 51 return pay; 52 } 53 public void setPay(int pay) { 54 this.pay = pay; 55 } 56 public Set<UserBean2> getUser() { 57 return user; 58 } 59 public void setUser(Set<UserBean2> user) { 60 this.user = user; 61 } 62 @Override 63 public String toString() { 64 return "RoleBean [id=" + id + ", post=" + post + ", pay=" + pay + "]"; 65 } 66 }
2、在hibernate.cfg.xml中引入UserBean2和RoleBean2这两个类的映射:
1 <mapping class="com.st.bean5.UserBean2" /> 2 <mapping class="com.st.bean5.RoleBean2" />
3、在BeanTest类中添加测试方法:
1 @Test 2 public void bean5test1(){ 3 // 获取一个会话 4 Session session = HibernateTools.openSession(); 5 //开启一次事物 6 Transaction tran = session.beginTransaction(); 7 UserBean2 user = new UserBean2(); 8 // RoleBean2 role = (RoleBean2) session.get(RoleBean2.class,1L); 9 RoleBean2 role = new RoleBean2(); 10 11 user.setName("汪文仕"); 12 user.setSex("男"); 13 14 role.setPost("博士"); 15 role.setPay(10000); 16 17 role.getUser().add(user); 18 session.save(role); 19 //确认提交事物 20 tran.commit(); 21 } 22 @Test 23 public void bean5test2(){ 24 // 获取一个会话 25 Session session = HibernateTools.openSession(); 26 /* List<UserBean> list = session.createCriteria(UserBean.class).list(); 27 for(UserBean user : list) 28 System.out.println(user);*/ 29 String hql = "select new Map(u.name as name,u.sex as sex,r.post as post,r.pay as pay) from UserBean2 u join u.role r"; 30 List<Map<String,Object>> list = session.createQuery(hql).list(); 31 for(Map<String,Object> data : list) 32 System.out.println(data); 33 }
时间: 2024-11-08 08:56:01