hibernate注解的测试

注解用到的jar包:下载地址



/**

     * ========================================================================
     * 注解
     * 步骤:
     * 1.导入jar包
     *    (1)hibernate-annotations.jar
     *    (2)hibernate-commons-annotations.jar
     *    (3)ejb3-persistence.jar
     * 2.使用注解配置持久化类级对象关联关系
     * 3.使用AnnotationConfiguration建立会话工厂
     * sessionFaction=new AnnotationConfiguration().configure().buildSessionFactory();
     * 4.在hibernate配置文件中(hibernate.cfg.xml)中生成持久化类
     * <mapping class="持久化类完整限定名">
     * ========================================================================
     *

     */

@Test
	public void test1(){
		//必须先在数据库里面创建序列,名字是seq_emp_id
		Session 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();
	}

	//测试多对一的配置注解
	@Test
	public 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());
	}
	
	//级联操作
	@Test
	public void test3(){
		Session session=new AnnotationConfiguration()
							.configure()
							.buildSessionFactory()
							.openSession();
	Transaction tx=session.beginTransaction();
	Dept1 dept1=new Dept1(1,"产品部");
	Emp1 emp1=new Emp1();
	emp1.setEname("Villy");
	emp1.setHiredate(new Date());
	dept1.getEmps().add(emp1);
	emp1.setDept(dept1);
	session.save(dept1);
	tx.commit();
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穆雄雄

哎,貌似还没开张来着呢~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值