基本注解介绍:
@Test 用于单元测试
@before 表示在执行其他方法之前执行该方法
@after 表示在调用其他方法之后执行该方法
简单demo
@Entity//标示该类是实体类 @Table(name="EMP")//标示该类对应数据库中的EMP表 public class Emp { @Id//标示该属性对应数据库表的主键列 @GeneratedValue(generator="mygen")//设置主键生成器 @GenericGenerator(name="mygen",strategy="uuid")//定义主键生成器并设置生成策略为uuid private String empid; @Column(name="EName") private String empName; }
demo2:
public class Main { private Session session; @Before//标示在调用该类其他方法之前先调用该方法 public void befor(){ session = HibernateUtil.currentSession(); session.beginTransaction(); System.out.println("Before++++"); } @Test//表名该方法是单元测试方法 public void test(){ Emp emp=new Emp(); emp.setEmpName("张一铭"); session.save(emp); System.out.println("save ok!"); } @After public void after(){ session.getTransaction().commit(); HibernateUtil.closeSession(); System.out.println("After+++++++++"); } }
运行上述代码的运行结果如下:
当然在运行之前还需要在Hibernate的cfg文件中加入如下配置:
一对一关联注解
一对一关联关系使用@OneToOne来表示 下面以学生和身份证为例来讲解。
demo:
学生类:
@Entity @Table(name="STUDENT") public class Student { @Id @GeneratedValue(generator="mygen") @GenericGenerator(name="mygen",strategy="uuid") private String sid; @Column private String sName; @OneToOne(cascade={CascadeType.ALL},targetEntity=IdCard.class) @JoinColumn(name="CID")//设定学生表中保存身份证表的关联的外键是CID private IdCard cid; public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public IdCard getCid() { return cid; } public void setCid(IdCard cid) { this.cid = cid; } }
身份证:
@Entity @Table(name="IDCARD") public class IdCard { @Id @GeneratedValue(generator="mygen") @GenericGenerator(name="mygen",strategy="uuid") private String cid; @Column private String address; @OneToOne(cascade={CascadeType.ALL},mappedBy="cid",targetEntity=Student.class)//mappedBy=“cid”设定关联关系由学生实体类维护,cid是学生实体类中保存身份证实体类的引用变量 private Student stu; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Student getStu() { return stu; } public void setStu(Student stu) { this.stu = stu; } }
测试:
Student stu=new Student(); stu.setsName("张一铭"); IdCard card=new IdCard(); card.setAddress("河南"); card.setStu(stu); stu.setCid(card); session.save(stu);
测试结果:
一对多关联和多对一关联配置:
一对多使用OneToMany来设置,多对一使用ManyToOne来设置。以员工和部门为例测试代码如下:
员工类:
@Entity//标示该类是实体类 @Table(name="EMP")//标示该类对应数据库中的EMP表 public class Emp { @Id//标示该属性对应数据库表的主键列 @GeneratedValue(generator="mygen")//设置主键生成器 @GenericGenerator(name="mygen",strategy="uuid")//定义主键生成器并设置生成策略为uuid private String empid; @Column(name="EName") private String empName; @ManyToOne(cascade={CascadeType.ALL}) @JoinColumn(name="deptNo")//设置员工表中保存部门表的外键为deptNo private Dept dept; public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public String getEmpid() { return empid; } public void setEmpid(String empid) { this.empid = empid; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } }
部门类:
@Entity//标示该类是实体类 @Table(name="Dept")//标示该类对应数据库中的Dept表 public class Dept { @Id//标示该属性对应数据库表的主键列 @GeneratedValue(generator="mygen")//设置主键生成器 @GenericGenerator(name="mygen",strategy="uuid")//定义主键生成器并设置生成策略为uuid private String did; @Column private String dname; @OneToMany(mappedBy="dept")//设定关联关系由Emp来维护 private Set<Emp> emps=new HashSet<Emp>(); public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } public String getDid() { return did; } public void setDid(String did) { this.did = did; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } }
测试:
Emp emp=new Emp(); emp.setEmpName("王哲"); Dept dept=new Dept(); dept.setDname("开发部"); dept.getEmps().add(emp); emp.setDept(dept); session.save(emp); System.out.println("save ok!");结果:
多对多关联配置:
多对多关联配置使用@ManyToMany来进行配置。以员工和项目为例代码如下:
员工类:
@Entity//标示该类是实体类 @Table(name="EMP")//标示该类对应数据库中的EMP表 public class Emp { @Id//标示该属性对应数据库表的主键列 @GeneratedValue(generator="mygen")//设置主键生成器 @GenericGenerator(name="mygen",strategy="uuid")//定义主键生成器并设置生成策略为uuid private String empid; @Column(name="EName") private String empName; @ManyToOne(cascade={CascadeType.ALL}) @JoinColumn(name="deptNo")//设置员工表中保存部门表的外键为deptNo private Dept dept; @ManyToMany(cascade={CascadeType.ALL}) @JoinTable( joinColumns=@JoinColumn(name="eid"), inverseJoinColumns=@JoinColumn(name="pid"), name="ReEmpProject" ) private Set<Project> project=new HashSet<Project>(); public Set<Project> getProject() { return project; } public void setProject(Set<Project> project) { this.project = project; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public String getEmpid() { return empid; } public void setEmpid(String empid) { this.empid = empid; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } }
项目类:
@Entity//标示该类是实体类 @Table(name="Project")//标示该类对应数据库中的Project表 public class Project { @Id//标示该属性对应数据库表的主键列 @GeneratedValue(generator="mygen")//设置主键生成器 @GenericGenerator(name="mygen",strategy="uuid")//定义主键生成器并设置生成策略为uuid private String pid; @Column private String pName; @ManyToMany(cascade={CascadeType.ALL},mappedBy="project") private Set<Emp> emps=new HashSet<Emp>(); public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getpName() { return pName; } public void setpName(String pName) { this.pName = pName; } public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } }
测试类:
Emp emp=new Emp(); emp.setEmpName("王哲"); Dept dept=new Dept(); dept.setDname("开发部"); dept.getEmps().add(emp); emp.setDept(dept); Project p=new Project(); p.setpName("在线拍卖系统"); emp.getProject().add(p); session.save(emp); System.out.println("save ok!");
结果: