1、什么是注解?java
解析:来源:Hibernate提供了Hibernate Annotations扩展包,它能够替换复杂的hbm.xml文件( Annotations扩展包是hibernate-annotation-3.4.0GA.zip)数据库
做用:使得Hibernate程序的开发大大的简化。利用注解后,可不用定义持久化类对应的*.hbm.xml,而直接以注解方式写入持久化类的实现中。session
2、注解配置持久化类经常使用注解。app
注解 | 含义和做用 |
@Entity | 将 一个类声明为一个持久化类 |
@Id | 声明了持久化类的标识属性(至关于数据表的主键) |
@GeneratedValue | 定义标识属性值的生成策略 |
@Table | 为持久化类映射指定表(table)、目录(catalog)和schema的名称。默认值,持久化类名,不带包名 |
@UniqueConstraint | 定义表的惟一约束 |
@Lob | 表示属性将被持久化为Blob或者Clob类型 |
@Column | 将属性映射到列 |
@Transient | 忽略这些字段和属性,不用持久化到数据库 |
三.测试
注解配置对象关联关系ui
(一)@OneToOne创建持久化类之间一对一关联关系this
场景一:员工对应一张身份证编码
Emp(员工表)spa
package cn.ljm.entity; import javax.persistence.*; /** * Created by win7 on 2017/2/16. */ @Entity @Table(name = "Emp2") public class Emp { private Integer eid; @Column private String ename; private Idcard idcard; @OneToOne @JoinColumn(name = "iid") public Idcard getIdcard() { return idcard; } public void setIdcard(Idcard idcard) { this.idcard = idcard; } @Id @GeneratedValue public Integer getEid() { return eid; } public void setEid(Integer eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } }
Idcard类hibernate
关键代码:
//mappedBy 表示由craid所在的对象维护关联关系
@OneToOne(mappedBy="cardid")
1 package cn.ljm.entity; 2 3 import javax.persistence.*; 4 5 /** 6 * Created by win7 on 2017/2/16. 7 */ 8 @Entity 9 @Table(name = "Idcard2") 10 public class Idcard { 11 @Id 12 @GeneratedValue 13 private Integer iid; 14 private String inum; 15 @OneToOne(mappedBy = "idcard",cascade = CascadeType.ALL) 16 private Emp emp; 17 18 public Integer getIid() { 19 return iid; 20 } 21 22 public void setIid(Integer iid) { 23 this.iid = iid; 24 } 25 26 public String getInum() { 27 return inum; 28 } 29 30 public void setInum(String inum) { 31 this.inum = inum; 32 } 33 34 public Emp getEmp() { 35 return emp; 36 } 37 38 public void setEmp(Emp emp) { 39 this.emp = emp; 40 } 41 }
测试类
1 import cn.ljm.entity.Emp; 2 import cn.ljm.entity.Idcard; 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 import org.junit.After; 8 import org.junit.Before; 9 import org.junit.Test; 10 11 /** 12 * Created by win7 on 2017/2/16. 13 */ 14 public class test { 15 Configuration cfg; 16 Session session; 17 Transaction tx; 18 @Before 19 public void myBefore(){ 20 cfg=new Configuration().configure("hibernate2.cfg.xml"); 21 SessionFactory factory=cfg.buildSessionFactory(); 22 session= factory.getCurrentSession(); 23 tx= session.beginTransaction(); 24 } 25 @After 26 public void myAfter(){ 27 tx.commit(); 28 } 29 @Test 30 public void test1(){ 31 Emp emp=new Emp(); 32 Idcard idcard=new Idcard(); 33 emp.setEname("123"); 34 idcard.setInum("321"); 35 idcard.setEmp(emp); 36 emp.setIdcard(idcard); 37 session.save(idcard); 38 } 39 }
二)@OneToMang and @MangToOne(创建双向关联一对多多对一)
场景一:一个部门对应多个员工;多个员工对应一个部门
Emp类
package cn.happy.entity.one; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; /** * 1.2 员工类 * @author happy * */ @Entity @Table public class Emp { @Id private Integer empId; @Column private String empName; @ManyToOne @JoinColumn(name="deptNo") private Dept dept; public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } }
Dept类
package cn.happy.entity.one; import java.util.HashSet; import java.util.Set; //jpa注解 import javassist.expr.NewArray; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerators; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; /** * 1.1 部门表 uuid guid 全球惟一编码 自增列 序列 32位16进制数 * * @author happy * */ //Dept部门类能够被HIbernate进行管理 @Entity @Table(name="Dept") public class Dept { @Id //引用生成器 @GeneratedValue private Integer deptNo; //默认Hibernate管理 private String deptName; @OneToMany(mappedBy="dept") @LazyCollection(LazyCollectionOption.FALSE) private Set<Emp> emps=new HashSet<Emp>(); public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } public Integer getDeptNo() { return deptNo; } public void setDeptNo(Integer deptNo) { this.deptNo = deptNo; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } }
测试类
package cn.happy.entity.one; import org.hibernate.Session; import org.hibernate.Transaction; import cn.happy.entity.Dept; import cn.happy.entity.Emp; import cn.happy.until.HibernateUtil; public class mangoneonemang { public static void main(String[] args) { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); // Dept dept = (Dept)session.load(Dept.class, 21); // // System.out.println(dept.getDeptName()); Emp emp = (Emp)session.load(Emp.class, 21); System.out.println(emp.getEmpName()); tx.commit(); HibernateUtil.closeSession(); } }
(三)@MangToMang(创建双向关联一对多多对一)
场景一:一个员工对应多个项目;一个项目对应多个员工(多对多的关联)
Employee类
关键代码解读:
//准备的第三张表就是员工和项目的关系
@JoinTable( name="Myproemp",//表的名字 joinColumns=@JoinColumn(name="empid"),//emp员工和第三表的外键关系 inverseJoinColumns=@JoinColumn(name="proid")//Project员工和第三表的外键关系 )
package entity; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; /* * 员工表 * */ @Entity @Table(name="MyEmployee") public class Employee { @Id @GeneratedValue private Integer empid;//员工编号 private String empname;//员工名称 //准备一个项目集合 @ManyToMany(cascade=CascadeType.ALL) //准备的第三张表就是员工和项目的关系 @JoinTable( name="Myproemp",//表的名字 joinColumns=@JoinColumn(name="empid"),//emp员工和第三表的外键关系 inverseJoinColumns=@JoinColumn(name="proid")//Project员工和第三表的外键关系 ) private Set<Project> pros=new HashSet<Project>(); public Set<Project> getPros() { return pros; } public void setPros(Set<Project> pros) { this.pros = pros; } public Employee(String empname) { super(); this.empname = empname; } public Employee(Integer empid, String empname) { this.empid = empid; this.empname = empname; } public Employee() { } public Integer getEmpid() { return empid; } public void setEmpid(Integer empid) { this.empid = empid; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } }
project类
关键代码:
//声明员工集合
@ManyToMany(mappedBy="pros")//将控制权交由员工
package entity; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; /* * 项目表 * */ @Entity @Table(name="MyProject") public class Project { @Id @GeneratedValue private Integer proid;//项目编号 private String proname;//项目名称 //声明员工集合 // @ManyToMany(mappedBy="pros")//将控制权交由员工 @ManyToMany(mappedBy="pros") private Set<Employee> emps=new HashSet<Employee>(); public Set<Employee> getEmps() { return emps; } public void setEmps(Set<Employee> emps) { this.emps = emps; } public Project(String proname) { this.proname = proname; } public Integer getProid() { return proid; } public void setProid(Integer proid) { this.proid = proid; } public String getProname() { return proname; } public void setProname(String proname) { this.proname = proname; } public Project(Integer proid, String proname) { this.proid = proid; this.proname = proname; } public Project() { } }
测试类
package test; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import util.HibernateUtil; import entity.Employee; import entity.Project; /* * 多对多关系 * */ public class Test { public static void main(String[] args) { //查询 //select(); //添加数据 innesrt(); } //添加数据 public static void innesrt(){ //获取Session Session session=HibernateUtil.currentSession(); //开启事务 Transaction tx = session.beginTransaction(); //构建两个项目 Project pro1=new Project("项目一"); Project pro2=new Project("项目二"); //构建多个员工 Employee emp1=new Employee("巴黎的雨季"); Employee emp2=new Employee("盛夏的果实"); //关系交由员工处理 emp1.getPros().add(pro1); emp1.getPros().add(pro2); emp2.getPros().add(pro2); //保存 session.save(emp1); session.save(emp2); //提交事务 tx.commit(); //关闭链接 HibernateUtil.closeSession(); } //查询数据 public static void select(){ //获取Session Session session=HibernateUtil.currentSession(); //开启事务 Transaction tx = session.beginTransaction(); String hql="from Project"; List<Project> list = session.createQuery(hql).list(); for (Project item : list) { System.out.println(item.getProname()+item.getProid()); for (Employee emp : item.getEmps()) { System.out.println(emp.getEmpid()+emp.getEmpname()); } } //Project pro = (Project)session.get(Project.class, 3); //System.out.println(pro.getProname()); //提交事务 tx.commit(); //关闭链接 HibernateUtil.closeSession(); } }