1

My Repository

@Repository
public interface TestNativeQRepository extends CrudRepository<TestNativeQ, String> {

    @Query( value="SELECT qplt.name price_list_name,  qplab.status_code, qplab.start_date, (SELECT charge_definition_code FROM oalfsaas_repl.QP_CHARGE_DEFINITIONS_B WHERE charge_definition_id=qplab.charge_definition_id  ) chargedefinitioncode "
                + "FROM  pricelistsall qplab, PRICELISTSTL qplt "
                + " WHERE qplab.price_list_id  =qplt.price_list_id ", nativeQuery = false)
    public List<TestNativeQDTO> getAllDetails();
}

Actual Result:

[{"ABC", "DEF", "15/05/2018", "XXZ"}]

Expected Result

[{name: "ABC", statuscode: "DEF", startDate: "15/05/2018", chargedefintioncode: "XXZ"}]
3
  • The result of native query is not automatically transformed to an Entity, you must do it manually or define mappings via @SqlResultSetMapping and @ColumnResult. Refer this question for details stackoverflow.com/questions/13012584/… Commented Nov 5, 2018 at 12:19
  • @NikolayShevchenko thanks for the correcting Commented Nov 5, 2018 at 13:33
  • Hi, Thanks for the answer. What if nativeQuery = false and columns are from more than 2 tables Commented Nov 5, 2018 at 16:28

1 Answer 1

1

As @Nikolay gave hint in comment.

The result of native query is not automatically transformed to an Entity, you must do it manually or define mappings via @SqlResultSetMapping and @ColumnResult.

to make that work follow the below code.

@Entity
@Configurable
@SqlResultSetMapping(name = "someName", entities = @EntityResult(entityClass = SamplePojo.class), columns = @ColumnResult(name = "columnName"))
public class SamplePojo{
//fields and getters/setters
}

and Then in query

List<SamplePojo> list = entityManager().createNativeQuery("Select ......", "someName").getResultList();

Note : someName should be same in both places.

Refer this-question

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Is there any blog respected to it ?
My hint was related to using native queries in repo methods denoted with @Query. If you use EntityManager directly then following should be enough: List<SamplePojo> list = entityManager().createNativeQuery("Select ......", SamplePojo.class).getResultList(); (without extra mappings)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.