About the HQL statement in Hibernate

Source: Internet
Author: User

Java hql case-when usage

if (null = = Sorter) {            hql.append ("ORDER by m.mdate desc,case if m.mealtime = ' Morning ' then ' 1 ' when m.mealtime = ' Noon ' Then ' 2 ' when m.mealtime = ' Night ' and ' 3 ' when m.mealtime = ' midnight ' then ' 4 ' End ');        } 

Note:

The meaning of the above HQL statement can be understood as:

By mdate descending order, but also to meet when mealtime = ' morning ', on the basis of the date first, when mealtime = ' noon ' the same, and so on.

A case can be written after a select, or it can be written after the order by

Here's how SQL's case is written

Select T.id,t.name,t.code,t.select_time,t.now_level, CaseT.now_level when ' County level leadership position ' then 38 when' County level non-leadership position ' then 36 when' deputy leadership positions at county level ' then 34 when' Deputy non-leadership position at county level ' then 31 when' Township Ke ji ' leading position ' then 28 when' Township ke ji non-leadership position ' then 26 when' Xiang Ke ji deputy non-leading position ' then 21 when' Clerk ' then 20 when' New Hire undecided ' then 19 when' Level five staff ' then 18 when' Level six staff ' then 15 when' Level seven staff ' then 13 when' Level eight staff ' then 11 when' Level nine staff ' then 9 when' City Tube business ' then 6 when' City Tube Business deputy ' then 3 when' Enterprise Other ' then 1END levefrom torg_cadre t WHERE t.status=2ORDER by code DESC, (Date_format (select_time, '%Y ')) desc, leve desc;

Multi-field ordering, followed by DESC or ASC for each field

About the HQL statement in Hibernate

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.