冬眠中的魔法ê [英] magic npe in hibernate

查看:176
本文介绍了冬眠中的魔法ê的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写作时,会话session = sessionFactory.getCurrentSession();
清单< Candidate>候选人=(List< Candidate>)session.createQuery(
select Candidate from Candidate as candidate left outer join
+candidate.skills as skill where skill.id =+ 1).list );

我在第二行有npe

如果我写

  Session session = sessionFactory.getCurrentSession(); 
清单< Candidate>候选人=(List< Candidate>)session.createSQLQuery(
select candidate。* from candidate inner join candidate_skill
+on candidate.id = candidate_skill.candidate_id
+inner join技能在candidate_skill.skill_id = skill.id
+where skill.id = 1)。list();

我的结果很好



在第一个例子中有NPE吗?


SEVERE:servlet [appServlet]在路径[/ ui]上下文中的Servlet.service() [请求处理失败;嵌套异常是带有根本原因的java.lang.NullPointerException]
java.lang.NullPointerException $ b $ org.hibernate.dialect.function.SQLFunctionRegistry.findSQLFunction(SQLFunctionRegistry.java:41)


解决方案

从第一个查询中删除 select Candidate $ c> Candidate 在候选人实体)?
完整实体选择的HQL语法是<映射实体>的 ...





关于您的问题:

NPE如何发生:一些调试或HQL解析器)
HQL解析器 - 在AST构建过程中 - 寻找名为 Candidate 的属性引用,但找不到它因此请尝试将其解析为ClassName或函数(请参阅 org.hibernate.hql.internal.ast.util.LiteralProcessor.processConstant() function);
候选是一个类名和解析器威胁它作为鉴别值(用于实体的子类)返回null。
在解析器之后,在select的显式列表的翻译过程中找到 Candidate null 并尝试将其解析为函数,因为 null 不是有效的数据类型(实体,基元或其他有效的数据类型),所以尝试将它们解析为函数;值 null 我们用作函数 SQLFunctionRegistry.findSQLFunction()的参数:

  public SQLFunction findSQLFunction(String functionName){
return sfi.getSqlFunctionRegistry()。findSQLFunction(functionName.toLowerCase());

functionName.toLowerCase()导致NPE。


When I write

Session session = sessionFactory.getCurrentSession();
List<Candidate> candidates = (List<Candidate>) session.createQuery(
        "select Candidate from Candidate as candidate  left outer join"
        + " candidate.skills as skill    where skill.id =" + 1).list();

I have npe at second line

if I write

Session session = sessionFactory.getCurrentSession();
List<Candidate> candidates = (List<Candidate>) session.createSQLQuery(
        "select candidate.* from candidate inner join candidate_skill"
        + " on candidate.id = candidate_skill.candidate_id"
        + " inner join skill on candidate_skill.skill_id = skill.id"
        + " where skill.id = 1").list();

I have good result

why I have NPE in first example?

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ui] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at org.hibernate.dialect.function.SQLFunctionRegistry.findSQLFunction(SQLFunctionRegistry.java:41)

解决方案

Remove select Candidate from first query (do you have a field Candidate in Candidate entity)? HQL syntax for full entity select is from <mapped entity>....


About your question:

How magic NPE happens: (...After a bit of debugging or HQL parser)
HQL parser - during AST building - look for a property-reference called Candidate and doesn't find it so try to resolve as a ClassName or a function (look in org.hibernate.hql.internal.ast.util.LiteralProcessor.processConstant() function); Candidate IS a classname and parser threat it as a discriminator value (for entity subclassing) returning null.
After that parser, during translation of select's explicit list find Candidate with value null and try to resolve as a function because null is not a valid dataType (entity, primitive or other valid datatatype) so try to resolve them as a function; value null us used as parameter for function SQLFunctionRegistry.findSQLFunction() which as this body:

public SQLFunction findSQLFunction(String functionName) {
        return sfi.getSqlFunctionRegistry().findSQLFunction( functionName.toLowerCase() );
    }

and functionName.toLowerCase() cause the NPE.

这篇关于冬眠中的魔法ê的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆