使用 @Formula 对 Hibernate 实体进行本机 SQL 查询会导致 NullPointerException
我有一个简单的 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。但对我来说,不清楚如何解决我的问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,这似乎是 HHH-2225 中报告的问题(即 HHH-2536 重复)。不幸的是,这个问题还没有解决,恐怕你必须使用 HQL 或提交补丁(你可以从对这个问题进行投票开始,但考虑到这个问题已经过去了近四年,我不指望能很快得到解决)老的)。
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).
我们可以使用 currentSession.createQuery() 而不是 currentSession.createSQLQuery()。查询会略有不同。
http://docs.jboss .org/hibernate/core/3.6/reference/en-US/html/queryhql.html#queryhql-subqueries
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
简单地说,不要使用 createNativeQuery ,只需使用 createQuery ..
或使用 getter conpat 进行瞬变
Simply way , don't use createNativeQuery , just use createQuery ..
or make a transiant with getter convat