今天徒弟用到了一句复杂的查询语句。。。。结果执行报错,但是在SQL中执行没有问题,于是来求助我了。。。
语句的HQL/SQL格式如下:
lect count(1) ,cxltype,sum(dzsje),sum(iperson)from (ltype,g.iperson,(lect sum(y.dzsje) from Ysklist as y kpid = g.cregno) as dzsje from Guestreg as g,Xl as xl where g.xluuid = xl.uuid ) as t where …… group ltype
结果执行出错,最终发现,HQL无法支持from后面跟子查询的方式,网上查了N多资料,发现遇到这个问题的人还真是不少,但每一个相关的问题帖都没有满意的答复,甚至于多数都是没有跟帖的。。。。
一阵心寒,hibernate叱咤风云,竟然对这个支持如此之弱?虽然这个语句可以通过另外的方法来做(比如建视图或者直接使用SQL来做),但总是不甘心,于是又开始查阅各处资料,最后找到了思路,觉得既然HQL不支持,那么只能把这种子查询封装为对象来做了,那么肯定是需要hbm配置这种临时的子查询对象的,于是开始着手hbm配置的资料中查,hbm中配
置对象的都是class标签,开始把范围缩小,针对hbm的class标签的属性资料开始翻查,找到了几个比较可能的属性,where、sublect、春节习俗简笔画mutable、entity-bean,貌似这些都可能跟临时对象有关。。。
于是反复尝试,并继续翻查资料
最终在Hibernate reference 3.2.0 ga 正式版中文参考手册中找到了一些比较可靠的资料:
5.1.3. class
你可以使用class元素来定义一个持久化类:
<class
name="ClassName"
table="tableName"
discriminator-value="discriminator_value"
mutable="true|fal"
schema="owner"
catalog="catalog"
proxy="ProxyInterface"
dynamic-update="true|fal"
dynamic-inrt="true|fal"
lect-before-update="true|fal"
polymorphism="implicit|explicit"
where="arbitrary sql where condition"
persister="PersisterClass"
batch-size="N"
optimistic-lock="none|version|dirty|all"
lazy="true|fal"
entity-name="EntityName"
check="arbitrary sql check condition"
rowid="rowid"
sublect="SQL expression"
abstract="true|fal"
node="element-name"
/>
| name (可选): 持久化类(或者接口)的Java全限定名。 如果这个属性不存在,Hibernate将假定这是一个非POJO的实体映射。 |
| table (可选 - 默认是类的非全限定名): 对应的数据库表名。 |
| discriminator-value (可选 - 默认和类名一样): 一个用于区分不同的子类的值,在多态行为时使用。它可以接受的值包括 null 和 not null。 |
| mutable (可选,默认值为true): 表明该类的实例是可变的或者不可变的。 |
| schema (可选): 覆盖在根<hibernate-mapping>元素中指定的schema名字。 |
| catalog (可选): 覆盖在根<hibernate-mapping>元素中指定的catalog名字。 |
| proxy (可选): 指定一个接口,在延迟装载时作为代理使用。 你可以在这里使用该类自己的名字。 |
| dynamic-update (可选, 默认为 fal): 指定用于UPDATE 的SQL将会在运行时动态生成,并且只更新那些改变过的字段。 |
| dynamic-inrt (华为手机短信删除了怎么恢复可选, 默认为 fal): 指定用于INSERT的 SQL 将会在运行时动态生成,并且只包含那些非空值字段。 |
| lect-before-update (可选, 默认为 fal): 指定Hibernate除非确定对象真正被修改了(如果该值为true-译注),否则不会执行SQL UPDATE操作。在特定场合(实际上,它只在一个瞬时对象(transient object)关联到一个 新的ssion中时执行的update()中生效),这说明Hibernate会在UPDATE 之前执行一次额外的SQL SELECT操作,来决定是否应该执行 UPDATE。 |
| polymorphism(多态)夸人有才华的成语 (可选, 默认值为 implicit (隐式) ): 界定是隐式还是显式的使用多态查询(这只在Hibernate的具体表继承策略中用到-译注)。 |
| where (可选) 指定一个附加的SQLWHERE 条件, 在抓取这个类的对象时会一直增加这个条件。车辆挂靠合同 |
| persister (可选): 指定一个定制的ClassPersister。 |
| batch-size (可选,默认是1) 指定一个用于 根据标识符(identifier)抓取实例时使用的"batch size"(批次抓取数量)。 |
| optimistic-lock(乐观锁定) (可选,默认是version): 决定乐观锁定的策略。 |
(16) | lazy (可选): 通过设置lazy="fal", 所有的延迟加载(Lazy fetching)功能将被全部禁用(disabled)。 |
(17) | entity-name (可选,默认为类名): Hibernate3允许一个类进行多次映射( 前提是映射到不同的表),并且允许使用Maps或XML代替Java层次的实体映射 (也就是实现动态领域模型,不用写持久化类-译注)。 更多信息请看第 4.4 节 “动态模型(Dynamic models)” and 第 18 章 XML映射。 |
(18) | check (可选): 这是一个SQL表达式, 用于为自动生成的schema添加多行(multi-row)约束检查。 |
(19) | rowid (可选): Hibernate可以使用数据库支持的所谓的ROWIDs,例如: Oracle数据库,如果你设置这个可选的rowid, Hibernate可以使用额外的字段rowid实现快速更新。ROWID是这个功能实现的重点, 它代表了一个存储元组(tuple)的物理位置。 |
(20) | sublect (可选): 它将一个不可变(自馁immutable)并且只读的实体映射到一个数据库的 子查询中。当你想用视图代替一张基本表的时候,这是有用的,但最好不要这样做。更多的介绍请看下面内容。 |
(21) | abstract (可选): 用于在<union-subclass>的继承结构 (hierarchies)中标识抽象超类。 |
| |
注意其中红色的字体,这就是关键之处,往下我找到了相关的内容:
对Hibernate映射来说视图和表是没有区别的,这是因为它们在数据层都是透明的( 注意:一些数据库不支持视图属性,特别是更新的时候)。有时你想使用视图,但却不能在数据库 中创建它(例如:在遗留的schema中)。这样的话,你可以映射一个不可变的(immutable)并且是 只读的实体到一个给定的SQL子查询表达式:
<class name="Summary">
<sublect>
lect item.name, max(bid.amount), count(*)
from item
join bid on bid.item_id = item.id
group by item.name
</sublect>
<synchronize table="item"/>
<synchronize table="bid"/>
<id name="name"/>
...
</class>
定义这个实体用到的表为同步(synchronize),确保自动刷新(auto-flush)正确执行, 并且依赖原实体的查询不会返回过期数据。<sublect>在属性元素 和一个嵌套映射元素中都可见。
显然这就是我一直在找的东东了,hibernate支持自身建立视图,而不需要依赖于数据库。。虽然它本身的说法这是用来替代视图的,但其实这就是带子查询的sql,看我们最终的配置结果。
临时子查询视图Bean[其中第二个非默认的构造函数是不能少的,不然对象无法创建]:
Java代码
1. public class TestBean {
2. private Integer id;
3. private String cxltype;
4. private Integer iperson;
5. private Double dzsje;
6. public TestBean(){}
7.
8. public TestBean(String cxltype, Integer iperson, Double dzsje) {
9. super();
10. ltype = cxltype;
11. this.iperson = iperson;
12. this.dzsje = dzsje;
13. }
14. public String getCxltype() {
15. return cxltype;
16. }
17. public void tCxltype(String cxltype) {
18. ltype = cxltype;
19. }
20. public Integer getIperson() {
21. return iperson;
22. }
23. public void tIperson(Integer iperson) {
喝酒的十大危害
24. this.iperson = iperson;
25. }
26. public Double getDzsje() {
27. return dzsje;
28. }
29. public void tDzsje(Double dzsje) {
30. this.dzsje = dzsje;
31. }
32.
33. public Integer getId() {
34. return id;
35. }
36.
37. public void tId(Integer id) {
38. this.id = id;
39. }
40. }
public class TestBean {
河源镜花缘 private Integer id;
private String cxltype;
private Integer iperson;
private Double dzsje;
public TestBean(){}
public TestBean(String cxltype, Integer iperson, Double dzsje) {
super();
一筹莫展是什么意思 ltype = cxltype;
this.iperson = iperson;
this.dzsje = dzsje;
}
public String getCxltype() {
return cxltype;
}
public void tCxltype(String cxltype) {
ltype = cxltype;
}
public Integer getIperson() {
return iperson;
}
public void tIperson(Integer iperson) {
this.iperson = iperson;
}
public Double getDzsje() {
return dzsje;
}
public void tDzsje(Double dzsje) {
this.dzsje = dzsje;
}
public Integer getId() {
return id;
}
public void tId(Integer id) {
this.id = id;
}
}
TestBean的hbm配置:
Xml代码
1. <hibernate-mapping>
2. <class name="TestBean" mutable="fal">
3. <sublect>
4. ltype,g.iperson,(lect sum(y.dzsje) from Ysklist as y kpid = g.cregno) as dzsje