以下几个SQL语句怎样转成HQL语句?
select*fromawhereb<cselect*fromawherebbetween20and30select*fromawherebin(21,32)select...
select * from a where b<c
select * from a where b between 20 and 30
select * from a where b in(21,32)
select * from a where b like '%m%'
select * from a where b is null
select * from a order by b ASC 展开
select * from a where b between 20 and 30
select * from a where b in(21,32)
select * from a where b like '%m%'
select * from a where b is null
select * from a order by b ASC 展开
1个回答
展开全部
1、如果是Query query = session.createQuery("")的话,试一下去掉前面的“select *”即createQuery方法内的条件直接是“from ......”,有几个是可以用的,因为支持部分SQL。
不行的话就用下面的格式(排序必用):
select OBJECT(o) from A o where o.b in(21,32)
select OBJECT(o) from A o order by o.b ASC
2、Hibernate中实现模糊查询,可有以下三种方式:
第一种方式:QBC查询
String name = "", info = "";
if (sub != null && sub.getSubname() != null) {
name = sub.getSubname();
}
if (sub != null && sub.getSubinfo() != null) {
info = sub.getSubinfo();
}
Criteria cr = session.createCriteria(Subject.class);
cr.add(Expression.like("subname","%"+name+"%"));
cr.add(Expression.like("subinfo","%"+info+"%"));
第二种方式:HQL查询语句
String hql = "from Subject as s where s.subname like :name and s.subinfo like :info";
// 调用session的获得数据列表方法,传递HQL查询语句
Query query = session.createQuery(hql);
query.setString("name","%"+name+"%");
query.setString("info","%"+info+"%");
System.out.println("*********"+hql);
第三种方式:HQL查询语句(查询条件只能为英文或数字,汉字在传递到hibernate内部时出现乱码问题)
String hql = "from Subject as s where s.subname like '%"+name+"%' and s.subinfo like '%"+info+"%'";//调用session的获得数据列表方法,传递HQL查询语句
Query query = session.createQuery(hql);
注:Subject为数据库表subject映射的类;它有相应的属性subname、subinfo及相应的get和set方法;sub为Subject的一个实例化对象。
不行的话就用下面的格式(排序必用):
select OBJECT(o) from A o where o.b in(21,32)
select OBJECT(o) from A o order by o.b ASC
2、Hibernate中实现模糊查询,可有以下三种方式:
第一种方式:QBC查询
String name = "", info = "";
if (sub != null && sub.getSubname() != null) {
name = sub.getSubname();
}
if (sub != null && sub.getSubinfo() != null) {
info = sub.getSubinfo();
}
Criteria cr = session.createCriteria(Subject.class);
cr.add(Expression.like("subname","%"+name+"%"));
cr.add(Expression.like("subinfo","%"+info+"%"));
第二种方式:HQL查询语句
String hql = "from Subject as s where s.subname like :name and s.subinfo like :info";
// 调用session的获得数据列表方法,传递HQL查询语句
Query query = session.createQuery(hql);
query.setString("name","%"+name+"%");
query.setString("info","%"+info+"%");
System.out.println("*********"+hql);
第三种方式:HQL查询语句(查询条件只能为英文或数字,汉字在传递到hibernate内部时出现乱码问题)
String hql = "from Subject as s where s.subname like '%"+name+"%' and s.subinfo like '%"+info+"%'";//调用session的获得数据列表方法,传递HQL查询语句
Query query = session.createQuery(hql);
注:Subject为数据库表subject映射的类;它有相应的属性subname、subinfo及相应的get和set方法;sub为Subject的一个实例化对象。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询