`
gaozzsoft
  • 浏览: 414803 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Hibernate HQL查询必知汇总

阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics