JPA @repository @Query 能处理空实体吗?

huangapple go评论62阅读模式
英文:

JPA @repository @Query Can handle a null entity?

问题

我正在使用 jpaspring 完成一个项目,我需要执行类似这样的 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

空指针异常出现在第一个查询中,因为你正在执行左连接,这可能会导致Calendarnull,而你在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

huangapple
  • 本文由 发表于 2020年4月7日 09:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61071247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定