15

I've got an simple Hibernate entity for which I use the @Formula annotation:

@Id
private Long id;

private String name;

@Formula("(select count(f.*) from foo f where f.id = id)")
private long bar;

When I try to load an entity with a native SQL Query:

EM.createNativeQuery("SELECT f.*, count(something) as bar FROM foo f WHERE f.name = '...'", Entity.class)

I get an NullPointerException:

java.lang.NullPointerException
at org.hibernate.loader.DefaultEntityAliases.intern(DefaultEntityAliases.java:193)
at org.hibernate.loader.DefaultEntityAliases.getSuffixedPropertyAliases(DefaultEntityAliases.java:151)
at org.hibernate.loader.DefaultEntityAliases.determinePropertyAliases(DefaultEntityAliases.java:93)
at org.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:65)
at org.hibernate.loader.ColumnEntityAliases.<init>(ColumnEntityAliases.java:45)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.generateCustomReturns(SQLQueryReturnProcessor.java:197)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:152)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:67)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:140)
at org.hibernate.impl.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:160)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:179)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:236)

When I remove the @Formula attribute all works fine.

I found this bug report: BugReport. But for me it's not clear how to solve my problem.

3 Answers 3

5

Indeed, this seems to be the problem reported in HHH-2225 (that HHH-2536 duplicates). Unfortunately, the issue is not fixed and I'm afraid you'll have to either use HQL or submit a patch (you can start with voting on the issue but I wouldn't expect a fast resolution given that this issue is almost four years old).

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

2 Comments

Years later, I still stumble upon this bug :(
I have the same issue with Hibernate 5.4.3.Final
0

We could use currentSession.createQuery() instead of currentSession.createSQLQuery(). The query will be slightly different.

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html#queryhql-subqueries

Comments

0

Simply way , don't use createNativeQuery , just use createQuery .. or make a transiant with getter convat

1 Comment

Provide some examples please

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.