3

I am trying to return custom object from spring data jpa - native query

Here's what I've done so far according to this How to return a custom object from a Spring Data JPA GROUP BY query

I should be able to create query like this:

Query(nativeQuery = true, value = "select sc.case_type as caseType, sc.revision as revision from smart_casemodel sc minus select s.casetype, s.revision from smart_case s")
List<CtRevOnly> findNotUsedCasemodels();

I declared projection Interface

public interface CtRevOnly {
    String getCaseType();
    String getRevision();
}

Then simply call it

caseModelRepository.findNotUsedCasemodels();

however I get an exception

javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: javax.persistence.Tuple

Any idea what am I doing wrong ?

Martin Čuka
  • 16,134
  • 4
  • 23
  • 49
  • You need to change your query from SQL query to JPQL query, and make CtRevOnly a class not an interface, check the answer from the link you referred to. – nsawaya Sep 12 '18 at 16:44
  • In accepted answer just below JPQL section is section about native query that's what i am interested in. I know it can be done other ways I am curious about native query and clean code. I would like to make it work using projections. Not mentioned it's harder to change it to JPQL since "minus" is not supported. Offcial documentation using Interfaces as well https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections – Martin Čuka Sep 12 '18 at 16:47

1 Answers1

2

For anyone running into the same problem
I am using Hibernate 5.1.2.Final

I guess there's a bug and I need to upgrade to hibernate 5.2.11 to make it work See this link

Martin Čuka
  • 16,134
  • 4
  • 23
  • 49