Hibernate 笔记 HQL查询(三) 分页,表连接,批量更新,引用SQL
2011-11-06 12:08
603 查看
1 分页
setFirstResult(0),(从0开始)
setMaxResults(5),每页显示5条数据
Hibernate: select emp0_.eage as col_0_0_ from emp emp0_ order by emp0_.eage limit ?
21
21
24
24
24
[b]2 表连接[/b]
Hibernate 支持内链接和外链接(左连接,右连接)
hql: from Emp e inner join fetch e.dept; 内链接
hql: from Emp e left join fetch e.dept; 左连接
hql:from Emp e right join fetch e.dept 右连接
结果
Hibernate: select emp0_.eid as eid2_0_, dept1_.did as did1_1_, emp0_.ename as ename2_0_, emp0_.eage as eage2_0_, emp0_.esal as esal2_0_, emp0_.did as did2_0_, dept1_.dname as dname1_1_, dept1_.daddress as daddress1_1_ from emp emp0_ inner join dept dept1_ on emp0_.did=dept1_.did where eage<30
白百何
301
文章
301
林月如
301
刘诗诗
302
3 批量更新
将年龄在25岁一下的员工改成25岁
hql="update Emp e set e.eage=25 where e.eage<25";
删除25岁一下的员工
hql="delete Emp e where e.eage<25";
使用executeUpdate()方法必须启用事务。
4 Hibernate 中使用SQL
HQL不是万能的,无法执行插入语句和非常复杂的查询,Hibernate 也支持SQL查询。通过连接直接调用cerateSQLQuery(sql)即可
sql语句中存在问号,同样使用setParameter(位置,属性值)方法设置。问号的位置从0开始,最后调用executeUpdate执行。事务提交后数据库开始工作。
setFirstResult(0),(从0开始)
setMaxResults(5),每页显示5条数据
public void Test1() throws Exception{ Configuration config=new Configuration().configure(); SessionFactory sessionFactory= config.buildSessionFactory(); Session session=null; Transaction tr=null; try{ session= sessionFactory.openSession(); tr=session.beginTransaction(); String hql="select eage from Emp order by eage"; Query query= session.createQuery(hql).setFirstResult(0).setMaxResults(5); //从0开始,现实5条数据。 List<Integer> list = query.list(); for(Integer message:list){ System.out.println(message); } tr.commit(); }catch(Exception e){ tr.rollback(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }
Hibernate: select emp0_.eage as col_0_0_ from emp emp0_ order by emp0_.eage limit ?
21
21
24
24
24
[b]2 表连接[/b]
Hibernate 支持内链接和外链接(左连接,右连接)
hql: from Emp e inner join fetch e.dept; 内链接
hql: from Emp e left join fetch e.dept; 左连接
hql:from Emp e right join fetch e.dept 右连接
public void Test2() throws Exception{ //实体 Configuration config=new Configuration().configure(); SessionFactory sessionFactory= config.buildSessionFactory(); Session session=null; Transaction tr=null; try{ session= sessionFactory.openSession(); tr=session.beginTransaction(); Query query=session.createQuery("from Emp e inner join fetch e.dept where eage<30"); List<Emp> list = query.list(); for(Emp user:list){ System.out.println(user.getEname()); System.out.println(user.getDept().getDaddress()); tr.commit(); }catch(Exception e){ tr.rollback(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }
结果
Hibernate: select emp0_.eid as eid2_0_, dept1_.did as did1_1_, emp0_.ename as ename2_0_, emp0_.eage as eage2_0_, emp0_.esal as esal2_0_, emp0_.did as did2_0_, dept1_.dname as dname1_1_, dept1_.daddress as daddress1_1_ from emp emp0_ inner join dept dept1_ on emp0_.did=dept1_.did where eage<30
白百何
301
文章
301
林月如
301
刘诗诗
302
3 批量更新
将年龄在25岁一下的员工改成25岁
hql="update Emp e set e.eage=25 where e.eage<25";
删除25岁一下的员工
hql="delete Emp e where e.eage<25";
使用executeUpdate()方法必须启用事务。
public void Test3() throws Exception{ Configuration config=new Configuration().configure(); SessionFactory sessionFactory= config.buildSessionFactory(); Session session=null; Transaction tr=null; try{ session= sessionFactory.openSession(); tr=session.beginTransaction(); String hql="update Emp e set e.eage=25 where e.eage<25"; Query query=session.createQuery(hql); query.executeUpdate(); tr.commit(); }catch(Exception e){ tr.rollback(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }
4 Hibernate 中使用SQL
HQL不是万能的,无法执行插入语句和非常复杂的查询,Hibernate 也支持SQL查询。通过连接直接调用cerateSQLQuery(sql)即可
sql语句中存在问号,同样使用setParameter(位置,属性值)方法设置。问号的位置从0开始,最后调用executeUpdate执行。事务提交后数据库开始工作。
public void Test4() throws Exception{ Configuration config=new Configuration().configure(); SessionFactory sessionFactory= config.buildSessionFactory(); Session session=null; Transaction tr=null; try{ session= sessionFactory.openSession(); tr=session.beginTransaction(); String sql="insert into emp (ename,eage) values (?,?)"; session.createSQLQuery(sql).setParameter(0, "曹雪芹").setParameter(1, 22).executeUpdate(); tr.commit(); }catch(Exception e){ tr.rollback(); }finally{ if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }
相关文章推荐
- [原创]java WEB学习笔记92:Hibernate学习之路-- -QBC 检索和本地 SQL 检索:基本的QBC 查询,带 AND 和 OR 的QBC,统计查询,排序,分页
- Hibernate(二)HQL&SQL参数绑定、投影和分页以及命名查询
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- (转)Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- Hibernate(二)HQL&SQL参数绑定、投影和分页以及命名查询
- hibernate 系列之BaseDao --基础创建sql,hql,分页查询
- hibernate 分页后,查询总数 使用hql和sql
- [原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询
- Hibernate HQL与Native_SQL_查询_Query接口_分页
- hibernate----懒加载,原生 sql查询及多种hql查询,表连接查询
- hibernate根据条件动态组装sql/hql语句(仿ibatis动态查询语句功
- Hibernate 笔记 HQL查询:条件查询,聚集函数,子查询,导航查询
- Hibernate HQL与Native_SQL_查询_Query接口_选择list方法和iterate方法
- 数据库学习笔记和小练习(6)sql查询和更新练习
- Hibernate的查询 HQL查询 参数,分页,分组