我有一个简单的 Hibernate 实体,我使用 @Formula 注释:
@Id
private Long id;
private String name;
@Formula("(select count(f.*) from foo f where f.id = id)")
private long bar;
当我尝试使用本机 SQL 查询加载实体时:
EM.createNativeQuery("SELECT f.*, count(something) as bar FROM foo f WHERE f.name = '...'", Entity.class)
我得到一个 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)
当我删除 @Formula 属性时,一切正常。
我发现了这个错误报告:BugReport。但对我来说,不清楚如何解决我的问题。