Use of the formula for Hibernate (HQL) subqueries

Source: Internet
Author: User

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

  1. <? Xml version ="1.0"?>
  2. <! DOCTYPE hibernate-mapping PUBLIC
  3. "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
  4. Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <Hibernate-mapping>
  6. <Class name ="Com. norm. model. Norm"
  7. Table ="NORM">
  8. <Id name ="Id"Type ="Java. lang. String"Column ="ID">
  9. <Generator class ="Uuid. hex"/>
  10. </Id>
  11. <Property name ="Name"Type ="Java. lang. String"Column ="NAME"
  12. Length ="100"/>
  13. <Property name ="EvaluateCount"
  14. Formula ="(Select count (d. id) from evaluate d where d. normId = id )"/>
  15. </Class>
  16. <Query name ="GetAllNorms"> From Norm </query>
  17. </Hibernate-mapping>

Note that HQL is not used in the formula and pure SQL is used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.