HQL: How to select all entities distinct by some column?

后端 未结 7 1278
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 08:20



A simple question:
In this example I need to retrieve all objects, but these objects must have distinct msgFrom fields.
When I use

List&         


        
7条回答
  •  借酒劲吻你
    2020-12-14 08:49

    I have got a answer for Hibernate Query Language to use Distinct fields. You can use SELECT DISTINCT(TO_CITY) FROM FLIGHT_ROUTE. If you use SQL query, it return String List. You can't use it return value by Entity Class. So the Answer to solve that type of Problem is use HQL with SQL.

    "FROM FLIGHT_ROUTE F WHERE F.ROUTE_ID IN (SELECT SF.ROUTE_ID FROM FLIGHT_ROUTE SF GROUP BY SF.TO_CITY)";
    

    From SQL query statement it got DISTINCT ROUTE_ID and input as a List. And IN query filter the distinct TO_CITY from IN (List).

    Return type is Entity Bean type. So you can it in AJAX such as AutoComplement.

    May all be OK

提交回复
热议问题