pa.equipmentaccount.id=?pa.equipmentaccount.idpa.equipmentaccount.idHQL
例如对于TUser类
1.实体查询
String hql = " from User";
执行这条语句会
返回User以及User子类的
纪录。
注: 如果 TUser 类具有外键, 查询结果不会报错,但结果中的外键为空值,访问就报空指针错误!
解决方法: select 别名.属性 from 类 as 别名. 没有别名.属性仍然报错! //此方法不一定能解决,但不会错.
hql = "from java.lang.Object"
会返回数据库中所有库表的纪录。
where 语句,as可以省略
hql = "from User as user where user.name='yyy'"; //user.name为类的属性
hql = "from User user where user.name='yyy'";
where子句中,我们可以通过比较运算符设定条件,如:
=, <>, >, <, >=, <=, between XX and XX, not between, in (xx,xx), not in, is, like %XX% 等。
2.属性查询
(1)List list = session.createQuery("select user.name, user.age from User user").list();
还可以在HQL中动态构造对象实例的方法,将数据封装。
(2)List list = session.createQuery("select new User(user.name, user.age) from TUser as user").list();
Iterator it = list.iterator();
while(it.hasNext() ) {
User user = (User)it.next();
System.out.println(user.getName());
}
但是要注意这里的User对象只是对name和age属性的封装,其他状态均未赋值,所以不能用它来进行更新操作。
也可以在HQL的Select子句中使用统计函数
"select count(*) ,min(user.age) from User as user"
也可以使用distinct关键字来删除重复纪录。
select distinct user.name from User as user;
3.实体的更新与删除
hibernate 3中,提供了更灵活的方式(bulk(集体) delete/update)
更新:
Query query = session.createQuery("update User set age=18 where id=1"); //age==User.age
query.executeUpdate();
删除:
session.createQuery("delete User where age>=18");
query.executeUpdate();
注:不支持联表更新,即此处的User对象不能外键关联.因为更新操作不允许多表联接更新,只能更新一张表(SQL规定).
如:update TbPartaccount pa set inoutflag=9 where pa.equipmentaccount.id=:id instr(pa.equipmentaccount.id,'Z')=1
pa.equipmentaccount.inoutcode报错,
pa.equipmentaccount.id还是
TbPartaccount 的字段(外键关联)
Hibernate 不能像我们想像的那样转换成以下方式,所以还是需要手工写子查询:
UPDATE TB_PARTACCOUNT pa set PA.INOUTFLAG=9 where pa.equipmentaccount.id=? and pa.eaccount_code=(
SELECT ea.id from TB_EQUIPMENTACCOUNT ea
where ea.id=pa.EACCOUNT_CODE and INSTR(ea.inoutcode, 'Z')=1)
UPDATE TB_PARTACCOUNT pa set PA.INOUTFLAG=9 where
pa.equipmentaccount.id=? and INSTR(pa.eaccount_code, 'Z')=1
4.分组与排序
Order by子句:
from User user order by user.name, user.age desc
Group by子句和Having子句
"select count(user), user.age from User user group by user.age having count(user)>10"
5.参数邦定
通过顺序占位符?来填充参数:
1)hibernate 2 中通过session.find方法来填充
session.find("from User user where user.name=?", "Erica", Hibernate.STRING);
多个参数的情况:
Object[] args = new Object[] {"Erica", new Integer(20)};
Type[] types = new Type{Hibernate.STRING, Hibernate.INTEGER};
session.find("from TUser user where user.name=? and user.age=?", args, types);
2)通过Query接口进行参数填充:
Query query = session.createQuery("from User user where user.name=? and user.age>?");
query.setString(0,"Erica");
query.setInteger(1, 20);
通过引用占位符来填充参数:
String hql = "from User where name=:name";
Query query = session.createQuery(hql);
query.setParameter("name","Erica");
甚至可以将查询条件封装为一个JavaBean
class UserQuery {
private String name;
private Integer age;
//getter and setter
}
String hql = "from TUser where name=:name and age=:age";
Query query = session.createQuery(hql);
UserQuery uq = new UserQuery();
uq.setName("Erica");
uq.setAge(new Integer(20));
query.setProperties(uq); //会调用里面的getter?
query.iterate();
6.联合查询
(org.hibernate.LazyInitializationException: could not initialize proxy - no Session 异常,可能是HQL语句没有进行联表查询,产生访问的属性不存在.)
User表: id, name
Addresses表: user_id, addresses
(1)自然联接(内联接): inner join [fetch]
1.HQL: from User u inner join fetch u.addresses
SQL: select * from User表 u inner join Addresses a on u.id=a.user_id //u.id=a.user_id在XML中已配置,所以可以省略.
fetch: Addresses对象读出后,立即填充到User对象对应的外键(集合)中.
2.HQL: from User u inner join u.addresses
不加fetch,则返回的结果集中的每一条记录是一个Object数组,数组包括User和Addresses两个对象, 并且User对象.addresses集合已被Addresses对象自动填充.
(2)左连接: left outer join [fetch]
同(1), 返回的结果不同而已.
(3)右连接: right out join
fetch无效,因为User对象可能为NULL,无法进行填充,但返回结果里的记录是数组.
(4)笛卡尔交集: full join (使用很少)
同(3), 返回结果不同而已.
(5)笛卡尔集: 排列组合:form User, Address
联合查询如有不懂, 详见SQL联合语句:
SQL中的left outer join,inner join,right outer join用法: http://www.blogjava.net/algz/articles/228219.html
SQL中的各种JOIN(inner join,full outer join,left join,right join,cross join ):http://www.blogjava.net/algz/articles/228218.html
sql的left join 命令详解 : http://www.blogjava.net/algz/articles/228220.html
posted on 2008-04-10 13:33
紫蝶∏飛揚↗ 阅读(12218)
评论(1) 编辑 收藏 所属分类:
Hibernate 、
重点