英文:
JPA @repository @Query Can handle a null entity?
问题
我正在使用 jpa
和 spring
完成一个项目,我需要执行类似这样的 query
:
第一种写法:
Select models.dto.dealCalendarDTO(d, c) from deal d left outer join calendar c on calendar.deal_id=deal.id;
或者
第二种写法:
Select d,c from deal d left outer join calendar c on calendar.deal_id=deal.id;
当我尝试第一种写法时,出现了这个错误:java.lang.NullPointerException: null
当我使用第二种写法时,出现了这个错误:
Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode
\-[IDENT] IdentNode: 'c' {originalText=c}
我使用传统的 SQL
进行了相同的操作而没有问题,但我觉得我可能漏掉了什么。
英文:
I am doing a project with jpa
and spring
, and i need to do a query
like this:
Select models.dto.dealCalendarDTO(d, c) from deal d left outer join calendar c on calendar.deal_id=deal.id;
Or
Select d,c from deal d left outer join calendar c on calendar.deal_id=deal.id;
When I try the first one I have this error: java.lang.NullPointerException: null
When I use the second one, I have this one
Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode
\-[IDENT] IdentNode: 'c' {originalText=c}
I did it with classic SQL
without a problem, but I think I am missing something.
答案1
得分: 0
空指针异常出现在第一个查询中,因为你正在执行左连接,这可能会导致Calendar
为null
,而你在DTO
函数中试图访问它。
英文:
The NullPointerException
in the first query because you are doing a left join and this maybe cause that c Calendar
will be null
and you are trying to access it in the DTO
function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论