Skip to content
Advertisement

invalid column name in namedNativeQuery in spring-hibernate-jpa

I have very simple Entity class and two native query in it .

QUERY1 is run right and return PersonEntity , but the QUERY2 return exception (invalid column name !!! but my column name is exactly this) ;

MY ENTITY CLASS :

JavaScript

DAO CLASS :

JavaScript

EXCEPTION :

JavaScript

Table Describe

Advertisement

Answer

The problem is that your second query returns a scalar value and resultClass is trying to map the returned value to an Entity.

Even changing resultClass to String.class should result in an exception like:

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

So just remove the entire resultClass attribute:

JavaScript

But receiving scalar values with native queries seems only working properly if you use the JPA variants of the native query annotations:

JavaScript

If you use the Hibernate ones

JavaScript

You will get something like:

org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported

Tested with Hibernate in version 5.0.12.Final.

Mapping native queries

If you want to keep you Hibernate annotations for any reason, there is also some kind of workaround to go with instead.

Define a result set mapping on your entity and use it for the scalar query with the resultSetMapping attribute:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement