Sql代码
//HQL-Associations
String hql = "select s.name, p.name, p.price from Product p inner join p.supplier as s";
Query query = session.createQuery(hql);
List results = query.list();
//HQL-Associations
String hql = "select s.name, p.name, p.price from Product p inner join p.supplier as s";
Query query = session.createQuery(hql);
List results = query.list();Sql代码
//HQL-Delete
String hql = "delete from Product where name = :name";
Query query = session.createQuery(hql);
query.setString("name","Product 1");
int rowCount = query.executeUpdate();
//HQL-Delete
String hql = "delete from Product where name = :name";
Query query = session.createQuery(hql);
query.setString("name","Product 1");
int rowCount = query.executeUpdate();Sql代码
//HQL-Function
String hql = "select min(product.price), max(product.price) from Product product";
Query query = session.createQuery(hql);
List results = query.list();
//HQL-Function
String hql = "select min(product.price), max(product.price) from Product product";
Query query = session.createQuery(hql);
List results = query.list();Sql代码
//HQL-Fetch Associations HQL Inner Join
String hql = "from Supplier s inner join fetch s.products as p";
Query query = session.createQuery(hql);
List results = query.list();
//HQL-Fetch Associations HQL Inner Join
String hql = "from Supplier s inner join fetch s.products as p";
Query query = session.createQuery(hql);
List results = query.list();Sql代码
//HQL-Named Parameters
String hql = "from Product where price > :price";
Query query = session.createQuery(hql);
query.setDouble("price",2.0);
List results = query.list();
String hql = "from Product as product where product.supplier=:supplier";
Query query = session.createQuery(hql);
query.setEntity("supplier",supplier);
List results = query.list();
//HQL-Named Parameters
String hql = "from Product where price > :price";
Query query = session.createQuery(hql);
query.setDouble("price",2.0);
List results = query.list();
String hql = "from Product as product where product.supplier=:supplier";
Query query = session.createQuery(hql);
query.setEntity("supplier",supplier);
List results = query.list();Sql代码
//HQL-Update
String hql = "update Supplier set name = :newName where name = :name";
Query query = session.createQuery(hql);
query.setString("name","Supplier Name 1");
query.setString("newName","s1");
int rowCount = query.executeUpdate();
//HQL-Update
String hql = "update Supplier set name = :newName where name = :name";
Query query = session.createQuery(hql);
query.setString("name","Supplier Name 1");
query.setString("newName","s1");
int rowCount = query.executeUpdate();Sql代码
//HQL-where
String hql = "from Product where price > 2.0 and name like 'P%'";
Query query = session.createQuery(hql);
List results = query.list();
//HQL-where
String hql = "from Product where price > 2.0 and name like 'P%'";
Query query = session.createQuery(hql);
List results = query.list();Sql代码
//HQL-Map
String hql = " select new map(usr.name as userName, usr.password as password) from User usr";
Query query = session.createQuery(hql);
List list = query.list();
Map goods =(Map)list.get(0);
//HQL-Map
String hql = " select new map(usr.name as userName, usr.password as password) from User usr";
Query query = session.createQuery(hql);
List list = query.list();
Map goods =(Map)list.get(0);
【注】
Sql代码
String hql = " select new map(usr.name as userName, usr.password as password)
from com.jason.User usr";
String hql = " select new map(usr.name as userName, usr.password as password)
from com.jason.User usr";
String hql = " select new map(usr.name as userName, usr.password as password)
from com.jason.User usr";
String hql = " select new map(usr.name as userName, usr.password as password)
from com.jason.User usr";由于from之前的空格,引起unexpected token: from
查询语句可以返回值为任何类型的属性或对象,包括返回类型为某种组件(Component)的属性:
Sql代码
select cust.name.firstName from Customer as cust
select cat.mate from Cat cat
select cust.name.firstName from Customer as cust
select cat.mate from Cat cat查询语句可以返回多个对象和(或)属性,存放在 Object[]队列中:
Sql代码
select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left join mother.kittens as offspr
select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left join mother.kittens as offspr
或存放在一个List对象中:
Sql代码
select new list(mother, offspr, mate.name)
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
select new list(mother, offspr, mate.name)
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr也可能直接返回一个实际的类型安全的Java对象(假设类Family有一个合适的构造函数):
Sql代码
select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr
select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr
也可以使用关键字as给“被选择了的表达式”指派别名:
Sql代码
select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat
select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat这种做法在与子句select new map一起使用时最有用:
Sql代码
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhutianxiang/archive/2010/07/06/5716058.aspx
=================================================================
Hibernate中使用Hql查询出一定时间段的记录
2010-04-09 15:44
Hibernate中使用Hql查询出一定时间段的记录
本人参考了网上的一些例子,希望可以给大家帮助。呵呵~
import java.sql.Timestamp; import
java.text.SimpleDateFormat; import java.util.Calendar; import
java.util.Date;
public class GenHql { private String hql =
"";
public static void main(String args[]) { GenHql genHql = new
GenHql(); Timestamp startTime = Timestamp.valueOf("2010-03-12
19:11:40"); Timestamp endTime = Timestamp.valueOf("2010-03-16
19:11:40"); genHql.setHqlByTimestamp(startTime); String hql1 = "select *
from Test t where 1=1 " +
genHql.getHql(); genHql.setHqlByTimestamp(startTime, endTime); String hql2
= "select * from Test t where 1=1 " +
genHql.getHql(); System.out.println("开始时间到现在" +
hql1); System.out.println("开始时间到结束时间" + hql2); }
//
根据开始时间和结束时间生成hql语句 public void setHqlByTimestamp(Timestamp startTime,
Timestamp endTime) { String beginDate = ""; String endDate =
""; SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
if (startTime != null && startTime.toString() != "")
{ // startTime = Timestamp.valueOf("2010-03-12 19:11:40"); Calendar cal =
Calendar.getInstance(); cal.setTime(startTime); Date begin =
cal.getTime(); beginDate = f.format(begin); hql += " and
t.timestamp>=to_date('" + beginDate + "','YYYY-MM-DD
HH24:MI:SS')"; }
if (endTime == null || endTime.toString().equals(""))
{ Date date = new Date(); endDate = f.format(date); hql += " and
t.timestamp<=to_date('" + endDate + "','YYYY-MM-DD HH24:MI:SS')"; }
else { // startTime = Timestamp.valueOf("2010-03-12 19:11:40"); Calendar
cal = Calendar.getInstance(); cal.setTime(endTime); Date end =
cal.getTime(); endDate = f.format(end); hql += " and
t.timestamp<=to_date('" + endDate + "','YYYY-MM-DD
HH24:MI:SS')"; } System.out.println(hql); }
//
根据开始时间生成hql语句 public void setHqlByTimestamp(Timestamp startTime) { String
beginDate = ""; String endDate = ""; hql = ""; SimpleDateFormat f = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (startTime != null &&
startTime.toString() != "") { // startTime = Timestamp.valueOf("2010-03-12
19:11:40"); Calendar cal =
Calendar.getInstance(); cal.setTime(startTime); Date begin =
cal.getTime(); beginDate = f.format(begin); hql = " and
t.timestamp>=to_date('" + beginDate + "','YYYY-MM-DD
HH24:MI:SS')"; } }
public String getHql() { return
hql; }
public void setHql(String hql) { this.hql =
hql; } }
|
分享到:
相关推荐
Hibernate hql查询语法总结 Hibernate hql查询语法总结
Hibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询....
Hibernate中的关联查询实际上生成的是数据库表连接查询的SQL语句
hibernate查询之HQLhibernate查询之HQLhibernate查询之HQL
hibernate hql各类查询范例
Hibernate HQL 深度历险 深入讲解Hibernate HQL 查询
Hibernate HQL基础练习小列子+数据库
Hibernate HQL查询语句总结.docx 网络下载的
hibernate HQL 查询教程 管理 Session ThreadLocal 接口的使用
Hibernate HQL.txt Hibernate HQL.txt Hibernate HQL.txt
hibernate持久化,hql查询 关联映射
对hibernate的hql进行了详尽的讲解
Hibernate查询语言HQL.PPT
hibernate hql 语法学习的文档
Hibernate(HQL、QBC查询)源码 Hibernate(HQL、QBC查询)源码 Hibernate(HQL、QBC查询)源码 Hibernate(HQL、QBC查询)源码 Hibernate(HQL、QBC查询)源码
Hibernate注解方式、HQL查询
Hibernate 配备了一种非常强大的查询语言,这种语言看上去很像 SQL。但是不要被语法结构上的相似所迷惑,HQL 是非常有意识的被设计为完全面向对象的查询,它可以理解如继承、多态和关联之类的概念。
Hibernate HQL.docHibernate HQL.docHibernate HQL.doc
Hibernate HQL 查询 Query 资料 3个word总结的资料。
hibernate hql基本操作