13

Are type-safe native named queries supported in Hibernate version 4.2.3.Final? I get this exception with one:

java.lang.ArrayIndexOutOfBoundsException: 0
    at org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:637)
    at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
    at $Proxy78.createNamedQuery(Unknown Source)

When I perform this query with a Query class all is fine. TypedQuery seems to be the issue here and the exception is not very helpful. I've tried this with simple queries to very complex queries and they all seem to fail unless I used Query for the named native query.

2 Answers 2

11

You could specify a SqlResultSetMapping to get rid of this error. For example:

@javax.persistence.Entity
@javax.persistence.SqlResultSetMapping(
    name = "implicit", entities =
    @javax.persistence.EntityResult(entityClass = Account.class)
)
@javax.persistence.NamedNativeQuery(
        name = "findAccount",
        query = "SELECT a.* FROM account a WHERE a.account_id=?1",
        resultSetMapping = "implicit")
public class Account implements java.io.Serializable {
    [...]
}

This way Hibernate knows how to deal with the values that the native query returns.

0

I had this problem when I moved a hard coded HQL query from the code into the mapping file, but accidentally put it in a <sql-query> element instead of a<query>.

But yes, that is not a very helpful message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.