打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Hibernate 笔记 HQL查询(三) 分页,表连接,批量更新,引用SQL

1 分页

      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

2 表连接

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();
}
}
}

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Hibernate 笔记 HQL查询(二)条件查询,聚集函数,子查询,导航查询
NHibernate Step by Step
主题:通过hibernate拦截器实现自定义分表策略
在Hibernate应用中如何处理批量更新和批量删除?
学习笔记之 O/R 映射技术的王牌Hibernate框架
mybatis和hibernate的对比总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服