0

I have a question. when I use spring data jpa, I want to it return Map Collections, but it wrong. Then I search on the internet found a solution. Flowing.

@Transactional(readOnly = true)
public interface GoodsRepository extends JpaRepository<TbGoodsEntity, Integer> {

    @Query(value = "select new map(t.id as id, t.goodsName as goodsName) from  TbGoodsEntity t group by t.goodsName")
    public List<Map<String, Object>> getGoodsNames();// it`s ok,

    @Query(value = "select * from  tb_goods t group by t.goodsName", nativeQuery = true)
    public List<Map<String, Object>> getGoods();//it`s error

}

But I don't think to use new map method its best solution, I`d like to ask if there any other solutions. Thanks.

Gavin
  • 1

1 Answers1

0

If use use "native" query then each row will be returned as "List" since spring doesn't have row transformer. So your output becomes List<List<Object>>.

If you try the below query then you should get List<Map<String,Object>>

@Query(value = "select t from  tb_goods t group by t.goodsName")
public List<Map<String, Object>> getGoods();

Note: I am guessing your DB column name is "goodsName" so not commenting if query is correct or not.

Punit Tiwan
  • 106
  • 5