When writing HQL, subqueries can be used except in the where condition. subquery statements are not allowed in other places, such as select and from. If subqueries are to be written after select, you can use the formula attribute to write data. If you want to use the subquery after the from statement, you cannot use HQL. Instead, use pure SQL (<SQL-query name = "">... </SQL _query> ).
Example of formula usage:
*. Hbm. xml Code
- <? Xml version ="1.0"?>
- <! DOCTYPE hibernate-mapping PUBLIC
- "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
- Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
-
- <Hibernate-mapping>
- <Class name ="Com. norm. model. Norm"
- Table ="NORM">
- <Id name ="Id"Type ="Java. lang. String"Column ="ID">
- <Generator class ="Uuid. hex"/>
- </Id>
- <Property name ="Name"Type ="Java. lang. String"Column ="NAME"
- Length ="100"/>
- <Property name ="EvaluateCount"
- Formula ="(Select count (d. id) from evaluate d where d. normId = id )"/>
- </Class>
- <Query name ="GetAllNorms"> From Norm </query>
- </Hibernate-mapping>
Note that HQL is not used in the formula and pure SQL is used.