8

In the JPA, I defined a native sql which will return String,

@NamedNativeQuery(name = "alert", 
query = " select distinct c.accountId from account c ", 
        resultClass = String.class)

the error message is

org.hibernate.MappingException: Unknown entity: java.lang.String

Any clues ? Thanks

user595234
  • 6,007
  • 25
  • 77
  • 101

4 Answers4

18
@SqlResultSetMappings({
    @SqlResultSetMapping(name = "alertMapping", columns = {
        @ColumnResult(name = "accountId")})
})
@NamedNativeQuery(name = "alert", 
query = " select distinct c.accountId from account c ", 
        resultSetMapping = "alertMapping")

Usage:

EntityManager em = ...... / injected / etc
TypedQuery<String> query = em.createNamedQuery("alert", String.class);
List<String> accountIds = query.getResultList();

(unchecked syntax, but I hope the basic idea comes through)

For NamedNativeQueries you can only use resultClass when the result actually maps to an Entity. It's also possible to not specify a result mapping, in which case you'd get a List of Object[] back. Each element of the list would be one record, and you'd have to explicitly cast each Object to the type you wanted. Hm, the last part might only be available to NativeQueries and not Named - sorry unsure at the moment.

esej
  • 3,059
  • 1
  • 18
  • 22
  • Good answer, but I don't find the bit about NamedNativeQueries only allowing "entity" result classes in the JPA spec. Have you got a section/page number? Thx – Neil Stockton Mar 19 '12 at 18:21
  • @esej "Hm, the last part might only be available to NativeQueries and not Named - sorry unsure at the moment." - I'm not sure what you meant by that, but I can confirm that treating the result list as Object[], like it's done [here](http://stackoverflow.com/questions/11452586/sqlresultsetmapping-columns-as-and-entities/19790951#19790951) also works with a _@NamedNativeQuery_. – Hein Blöd Apr 15 '15 at 09:45
2

Seems Hibernate only allows Entity result classes. Obviously not all JPA implementations have this restriction. DataNucleus JPA, for example, allows that query to run fine.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
1

In my case, I changed hibernate version from 3.5.6 to 4.3.3. it is working fine.

<!--     <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency> -->


        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.3.Final</version>
        </dependency>
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>4.3.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.3.Final</version>
        </dependency>
1

Do not include resultClass in query declaration

davidcesarino
  • 16,160
  • 16
  • 68
  • 109
Andrey
  • 676
  • 2
  • 8
  • 12