分享

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

 昵称21365845 2015-06-04

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();
}
}
}
复制代码

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多