Hibernate中使用HQL语句进行增,删,改,查

Hibernate的所有的操作都是通过Session完成的.

基本步骤如下:

1:通过配置文件得到SessionFactory:

  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

  SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");

  //以上配置基于spring主配置文件配置的方式

2:通过SessionFactory 得到一个Session

     Session session=sessionFactory.openSession();

3:通过session进行插入,删除,修改以及查询.

    插入例子:(1)声明一个事务;(2)Session执行save()操作;(3)事务提交;(4)关闭Session,可选.                   
             public void insert(Person p){
         Transaction tran=session.beginTransaction();
      session.save(p); 

                tran.commit();  
        //session.close();             
             }     
    修改例子:(1)声明一个事务;(2)Session执行update()操作;(3)事务提交;(4)关闭Session,可选.

             public void update(Person p){

        Transaction tran=session.beginTransaction();

        session.update(p);

           tran.commit();

      // session.close();

      }

     

复制代码

通过瞬时对象修改数据
  JobBean aJob=new JobBean();
  aJob.setJobId(17);
  aJob.setJobDesc("编辑工作1");
  aJob.setMaxLvl(10);
  aJob.setMinLvl(10);
  s.update(aJob);
  tx.commit(); 
通过持久对象修改数据
  JobBean aJob=(JobBean)s.get(JobBean.class, 17);
  aJob.setJobDesc("编辑工作2");
  tx.commit();
持久对象脱管对象
  JobBean aJob=(JobBean)s.get(JobBean.class, 17);
  s.evict(aJob);
  aJob.setJobDesc("编辑工作3");
  //这个修改不会反应到数据库
  tx.commit();

复制代码

    删除例子(主键删除,推荐使用):(1) 声明删除的SQl语句;(2)创建session的Query对象;(3)设置Query对象的参数;(4)执行Query的executeUpdate()操作;(5)Session事务提交

     public void delete(int id){

     String hql="delete Person as p where p.id=?";

   //此处使用标准语句同样适用:"delete from Person where id=?";

     //同样select和update都适用

     Query query=session.createQuery(hql);

     query.setInteger(0,id);

     query.executeUpdate();

     session.beginTransaction().commit();

    }

   删除例子(对象删除):(1)声明一个事务;(2)Session执行delete()操作;(3)事务提交;(4)关闭Session,可选.
    public void delete(Person p){

     Transaction tran = session.beginTransaction();

     //Person p = new Person();

    //p.setid = id;(如果传入的只有部分数据)

     session.delete(p);  
     tran.commit();

     session.close();  
   }

   删除例字(持久对象->瞬时对象):

JobBean aJob=(JobBean)s.get(JobBean.class, 17);
    s.delete(aJob);
    tx.commit();

   查询例子: 查询语句不需要事务提交

(1) 声明删除的SQl语句;(2)创建session的Query对象;(3)设置Query对象的参数;

   public Persion queryById(int id){

     String hql="from Person as p where p.id=?";

     Query query=session.createQuery(hql);
  
     query.setInteger(0,id);

     List rsList=query.list();

     iterator it=rsList.iterator();

     Person person=null;

     while(it.haseNext()){

       person=(Person)it.next();

     return person;

   }

 

session.delete()- -
session.delete(obj)将obj的状态变为transient。两种情况
1)obj是session的cache里边的cache没有的,比如:
         session.delete(new Employee(4));
2)obj存在于session的cache中,比如:
         Employee employee = (Employee)session.load(Employee.class, new Integer(4));
         session.delete(employee);

这两种情况都是允许的,hibernate都会发送一条delete语句给数据库。

delete执行之后,如果调用了session.load(), 又可以分为两种情况:
1)在session.flush()之前,如:
           tx.beginTransaction();
    session.delete(new Employee(4));
           session.load(Employee.class, new Integer(4));//发生在session.flush()之前
           tx.commit();
      那么hibernate会抛出ObjectDeletedException:The object with that id was deleted:

2)在session.flush()之后,如:
           tx.beginTransaction();
    session.delete(new Employee(4));
           session.load(Employee.class, new Integer(4));
           tx.commit();

           tx.beginTransaction();
           session.load(Employee.class, new Integer(4));//同一个session中,上面的tx.commit()将session flush了一次。
           tx.commit();
      那么这个时候hibernate仅仅会抛出ObjectNotFoundException:No row with the give...
表示找不到该object。如果第二个tx里边采用session.get()也就不会抛出exception了。

delete执行之后,如果调用了session.save(obj):
           tx.beginTransaction();
           Employee employee = (Employee)session.load(Employee.class, new Integer(4)); 
    session.delete(employee);
           System.out.println(employee);
           session.save(employee);
           System.out.println(employee);
           tx.commit();
      这种情况是完全合理的,合法的,
      delete将employee从persistent的状态变为transient的状态。
      save将employee从transient状态变为persistent的状态。
      save一个被delete的obj的时候,在save处hibernate强制执行session.flush(),发送delete语句,然后按照常规的save流程来进行。为什么要这么做,还没有完全想明白。

delete执行之后,如果对obj对象属性的修改,tx.commit()时不会进行dirtyChecking。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值