TransWikia.com

Spring boot nested native sql query is not working

Stack Overflow Asked by Shivam Kumar on February 8, 2021

@Query(value = "select m from IN_MESSAGE m where m.masterOrderNo=(select n.masterOrderNo from IN_MESSAGE n where n.orderNo=:orderNo)", nativeQuery = true)
public List<InMessageDO> findAllByOrderNo(@Param("orderNo") String orderNo);

Above code is in my Jpa Repository.

List<InMessageDO> inMessages = inMessageRepo.findAllByOrderNo(input.getOrderNo());

Above line shows how I am calling JPA method in my Application. Whenever above line is getting executed I am getting exception.

‘org.springframework.dao.InvalidDataAccessResourceUsageException’ exception.

Thank you

One Answer

The problem is you are not using a native query at all. SELECT m FROM ... is not native SQL.

You have to use SELECT * or SELECT m.* like this (not tested at all but select m should be the problem):

@Query(value = "SELECT * FROM in_messagedo m WHERE m.MASTER_ORDER_NO = (SELECT n.MASTER_ORDER_NO FROM in_messagedo n WHERE n.ORDER_NO = :orderNo)", nativeQuery = true)
public List<InMessageDO> findAllByOrderNo(@Param("orderNo") String orderNo);

Edit: Also, if your @Entity is named InMessageDO check how is your table name, because you are trying to read IN_MESSAGE table, and should be in_messagedo or something similar.

Also your query has to be the same as your DB, not as your object attribute name, so you have to look for MASTER_ORDER_NO instead of masterOrderNo. And I suposse orderNo has the same problem.

Also, the last orderNo is not neccesary to edit, this is the parameter variable.

Answered by J.F. on February 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP