2017-07-18 97 views
2

SQL模式ONLY_FULL_GROUP_BY默認情況下處於啓用狀態。因此,而不是禁用ONLY_FULL_GROUP_BY我試圖使用ANY_VALUE(arg)帶CRUD存儲庫的MySql ANY_VALUE()方法

查詢獲取我預期的結果。

我希望這與Spring Boot CrudRepository一起使用。所以我在@查詢註釋類似於下面(只是一個示例)。我想用@Query標註有一個複雜的查詢

@Query("select any_value(c.id), c.number, any_value(c.type) from Call c group by c.number") 
public List<Call> getCallsByType(Pageable pageable); 

但它拋出一個異常

Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
+-[METHOD_CALL] MethodNode: '(' 
| +-[METHOD_NAME] IdentNode: 'any_value' {originalText=any_value} 
| \-[EXPR_LIST] SqlNode: 'exprList' 

如何能不能做到?

+1

你嘗試設置[nativeQuery(https://stackoverflow.com/a/15949012/5594670)爲真? – Javvano

+0

@Javvano是的,通過將nativeQuery設置爲true可以工作。謝謝 – Rajagopal

+0

'nativeQuery'完全破壞了JPA的目的...加上:本機查詢是萬惡之源。不要使用它們。永遠。 – specializt

回答

1

您已經告訴Spring將該查詢視爲本地查詢。否則,它會嘗試根據JPA規範進行驗證。

嘗試:

@Query(value = "select any_value(c.id), c.number, any_value(c.type) from Call c group by c.number" 
     , nativeQuery = true) 
public List<Object[]> getCallsByType(Pageable pageable); 

請記住,你不能在這種情況下,使用new運算符語法,你將不得不處理結果作爲對象的數組。

或者

如果你想使用的結果直接映射到一個POJO類,你將不得不(假設你正在使用JPA 2.1+):

1)定義的映射:

@SqlResultSetMapping(
    name="resultMapping", 
    classes={ 
     @ConstructorResult(
      targetClass=ResultClass.class, 
      columns={ 
       @ColumnResult(name="id"), 
       @ColumnResult(name="number") 
       // further mappings ... 
      } 
     ) 
    } 
) 

2)定義一個本地查詢

@NamedNativeQuery(name="getCallsByType" 
    , query="select any_value(c.id), c.number, any_value(c.type) from Call c group by c.numbe") 

3)定義在CrudRepository這種方法沒有@Query註釋:

public List<ResultClass> getCallsByType();