hibernate注解的测试
生活随笔
收集整理的這篇文章主要介紹了
hibernate注解的测试
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
注解用到的jar包:下載地址
/**
?? ? * ========================================================================?? ? * 注解
?? ? * 步驟:
?? ? * 1.導(dǎo)入jar包
?? ? *??? (1)hibernate-annotations.jar
?? ? *??? (2)hibernate-commons-annotations.jar
?? ? *??? (3)ejb3-persistence.jar
?? ? * 2.使用注解配置持久化類級對象關(guān)聯(lián)關(guān)系
?? ? * 3.使用AnnotationConfiguration建立會話工廠
?? ? * sessionFaction=new AnnotationConfiguration().configure().buildSessionFactory();
?? ? * 4.在hibernate配置文件中(hibernate.cfg.xml)中生成持久化類
?? ? * <mapping class="持久化類完整限定名">
?? ? * ========================================================================
?? ? *
?? ? */
@Testpublic void test1(){//必須先在數(shù)據(jù)庫里面創(chuàng)建序列,名字是seq_emp_idSession session=new AnnotationConfiguration().configure().buildSessionFactory().openSession();Transaction tx=session.beginTransaction();Emp1 emp1=new Emp1();emp1.setEname("test2");emp1.setHiredate(new Date());session.save(emp1);tx.commit();}
//測試多對一的配置注解@Testpublic void test2(){Session session=new AnnotationConfiguration().configure().buildSessionFactory().openSession();Emp1 emp1=(Emp1) session.get(Emp1.class,7788);System.out.println(emp1.getEname()+","+emp1.getDept().getDname());}//級聯(lián)操作@Testpublic void test3(){Session session=new AnnotationConfiguration().configure().buildSessionFactory().openSession();Transaction tx=session.beginTransaction();Dept1 dept1=new Dept1(1,"產(chǎn)品部");Emp1 emp1=new Emp1();emp1.setEname("Villy");emp1.setHiredate(new Date());dept1.getEmps().add(emp1);emp1.setDept(dept1);session.save(dept1);tx.commit();}
總結(jié)
以上是生活随笔為你收集整理的hibernate注解的测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hibernate中使用Criteria
- 下一篇: hibernate注解实体类(Dept.