还支持数据库的类型转换函数,如cast(... as ...),第二个参数是Hibernate的类型名,或者extract(... from ...),前提是底层数据库支持ANSI cast()?和extract()。
例如:
cast(arg1 as arg2);arg1是要转换的数据,arg2是目标类型(不是数据库类型名,是hibernate类型名:比如目标类型是varchar,必须写string)
SELECT CAST( ’2012-12-24′ AS DATE ) 把字符串转换成date类型
SELECT CAST( 2012 AS string ) 把数字转化成字符串
SELECT CAST( ‘baiduzhidao’ AS char(3) ) 取指定长度的字符
在HQL中子查询一般出现在where子句中,不能像sql语句那样在from后嵌套子查询,通过子查询来查询某个字段等。如:
List list=session.createQuery(“from Customer c where 1>(select count(o) from c.orders o)”).list();
下面这种子查询是正确的:
Query query=em.createQuery("select g.ginID as ginID,(select x.name from Gstorage x where x.gstorageID=g.glocationID) as gstorageN from Gin g ");
下面这种子查询是错误的:
Query query=em.createQuery("select o.ginID as ginID from ( select g.ginID as ginID from Gin g ) o");
还可以在HQL语句中使用Java public static final 类型的常量,例如eg.Color.TABBY。
除此之外,where子句还支持如下的特殊关键字用法。
in与between...and可按如下方法使用:
from DomesticCat cat where cat.name between 'A' and 'B'
from DomesticCat cat where cat.name in ( 'Foo','Bar','Baz')
当然,也支持not in和not between...and的使用,例如:
from DomesticCat cat where cat.name not between 'A' and 'B'
from DomesticCat cat where cat.name not in ( 'Foo','Bar','Baz' )
子句is null与is not null可以被用来测试空值,例如:
from DomesticCat cat where cat.name is null;
from Person as p where p.address is not null;
size关键字用于返回一个集合的大小,例如:
from Cat cat where cat.kittens.size > 0
from Cat cat where size(cat.kittens) > 0
对于有序集合,还可使用minindex与maxindex函数代表最小与最大的索引序数。同理,可以使用minelement与maxelement函数代表集合中最小与最大的元素。例如:
from Calendar cal where maxelement(cal.holidays) > current date
from Order order where maxindex(order.items) > 100
from Order order where minelement(order.items) > 10000
where子句中,有序集合的元素(arrays, lists, maps)可以通过[ ]运算符访问。例如:
//items是有序集合属性,items[0]代表第一个元素
from Order order where order.items[0].id = 1234
//holidays是map集合属性,holidays[national day]代表其中一个元素
select person from Person person, Calendar calendar
where calendar.holidays['national day'] = person.birthDay
and person.nationality.calendar = calendar
//下面同时使用list 集合和map集合属性
select item from Item item, Order order
where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11
select item from Item item, Order order
where order.items[ maxindex(order.items) ] = item and order.id = 11
在[]中的表达式甚至可以是一个算术表达式,例如:
select item from Item item, Order order
where order.items[ size(order.items) - 1 ] = item
连接查询
迫切左外连接
from User u left join fetch u.student s where u.name like '%java%'
左外连接
左连接就是要把左边这个表里面的所有的数据显示出来。
from User u left join u.student s where u.name like '%java%'
迫切内连接
from User u inner join fetch u.student where u.name like '%java%'
内连接
内连接是把所有的相等的,完全符合条件的列出来。
内连(join).可以将inner 关键字省略,默认就是内连接。
from User u inner join u.student where u.name like '%java%'
右外连接
from User u right join u.student where u.name like '%java%'
order by子句
排序order by
HQL和SQL的使用方法很类似,"ASC"和"DESC"分别为升序和降序,如果不显式注明,HQL中默认为asc升序。
//先按照年龄降序排序,然后再按出生日期升序排序
Query query = sessionFactory.getCurrentSession().createQuery("select p from Person p order by p.age desc, p.birthday asc");
group by子句
分组查询,大部分标准的SQL聚集函数都可以在HQL语句中使用,比如:
count(),sum(),max(),min(),avg()等。
select cat.color, sum(cat.weight), count(cat) from Cat cat group by cat.color
Query query=sessionFactory.getCurrentSession().createQuery("select p.sex,count(*) from Person p group by p.sex");
HQL和可以使用和SQL那样的grouping,group by rollup(...)
如:
select g.belongOrganID,g.customerN,g.vtype,(case when grouping(g.belongOrganID)=0 then 1 when grouping(g.belongOrganID)=1 then 0 end )+(case when grouping(g.customerN)=0 then 1 when grouping(g.customerN)=1 then 0 end )+(case when grouping(g.vtype)=0 then 1 when grouping(g.vtype)=1 then 0 end ),uuid() as ID,count(*) as count, sum(g.totoalPrice) as totoalPrice from Gin o where 1=1 group by rollup(g.belongOrganID,g.customerN,g.vtype)
distinct
有时查询结果集中有相同的数据,关键词distinct用于返回唯一不同的数据。
select [distinct] object(variable) from abstractschemaname [as] variable [where value comparison value]
子查询
如果底层数据库支持子查询,则可以在HQL语句中使用子查询。与SQL中子查询相似的是,HQL中的子查询也需要使用()括起来。如:
from Cat as fatcat
where fatcat.weight > ( select avg(cat.weight) from DomesticCat cat )
如果select中包含多个属性,则应该使用元组构造符:
from Cat as cat
where not ( cat.name, cat.color ) in (
select cat.name, cat.color from DomesticCat cat
)
命名查询
命名查询也和EJB3 QL一样使用@NamedQuery注解。
@NamedQuery
我们可以使用@NamedQuery注释来定义命名查询,这个注释可以放在任何实体Bean的上方,但为了便于管理,最好放在相关的实体Bean上方。
@NamedQuery(name="MyQuery",query="select p from PersonFlight p where name like ?1")
@NamedQueries( {
@NamedQuery(name = "person.query", query = "from Person p where p.id=? "),
@NamedQuery(name = "person.query1", query = "from Person p where p.id=? ") })
也可以在hbm.xml文件中配置命名查询:
select distinct g
from org.jbpm.pvm.internal.identity.impl.MembershipImpl m
join m.user u
join m.group g
where u.id = :userId
]]>
Query query = session.getNamedQuery("findGroupsByUser");
query.setInteger("userId", 25);
List result= query.list();
Hibernate org.hibernate.Query类
setParameter()方法:
在Hibernate的HQL查询中可以通过setParameter()方法邦定任意类型的参数,如下代码:
String hql="from User user where user.name=:customername";//注意=:后不要有空格
Query query=session.createQuery(hql);
query.setParameter("customername",name,Hibernate.STRING);
如上面代码所示,setParameter()方法包含三个参数,分别是命名参数名称,命名参数实际值,以及命名参数映射类型。对于某些参数类型 setParameter()方法可
以更具参数值的Java类型,猜测出对应的映射类型,因此这时不需要显示写出映射类型,像上面的例子,可以直接这样写:
query.setParameter("customername",name); 但是对于一些类型就必须写明映射类型,比如java.util.Date类型,因为它会对应Hibernate的多种映射类型,比如
Hibernate.DATA或者Hibernate.TIMESTAMP。
setProperties()方法:
在Hibernate中可以使用setProperties()方法,将命名参数与一个对象的属性值绑定在一起,如下程序代码:
Customer customer=new Customer();
customer.setName("pansl");
customer.setAge(80);
Query query=session.createQuery("from Customer c where c.name=:name and c.age=:age");
query.setProperties(customer);
setProperties()方法会自动将customer对象实例的属性值匹配到命名参数上,但是要求命名参数名称必须要与实体对象相应的属性同名。
查询单个字段
String hql = " select name from Users";
Query query = session.createQuery(hql);
List list = query.list();
for(String str : list){
System.out.println(str);
}
输出结果为:
name1
name2
name3
查询其中几个字段
String hql = " select name,passwd from Users";
Query query = session.createQuery(hql);
//默认查询出来的list里存放的是一个Object数组
List
首先看看 WeakReference
wiki 上 Weak reference 的一个例子:
public class ReferenceTest {
public static void main(String[] args) throws InterruptedException {
WeakReference r = new Wea
有一个线上环境使用的是c3p0数据库,为外部提供接口服务。最近访问压力增大后台tomcat的日志里面频繁出现
com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResou
https://weblogs.java.net/blog/mriem/archive/2013/11/22/jsf-tip-45-create-composite-component-custom-namespace
When you developed a composite component the namespace you would be seeing would
一、复本集为什么要加入Arbiter这个角色 回答这个问题,要从复本集的存活条件和Aribter服务器的特性两方面来说。 什么是Artiber? An arbiter does
not have a copy of data set and
cannot become a primary. Replica sets may have arbiters to add a
# include <stdio.h>
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
//a 是数组的名字 5是表示数组元素的个数,并且这五个元素分别用a[0], a[1]...a[4]
int i;
for (i=0; i<5; ++i)
printf("%d\n",