【问题标题】:hibernate on to many reverse query休眠到许多反向查询
【发布时间】:2016-01-18 08:13:26
【问题描述】:

我在QuestionAnswer 两个实体之间有many2one 关系,其中many2one 定义在Answer' and no one2many defined inQuestion` 中。

如果不在Question实体中添加one2many关系,我如何通过休眠查询没有任何答案的问题?

类似:

select distinct q from Question q
left join Answer a on a.question_id=q.id
where a.id is null

【问题讨论】:

    标签: hibernate hql hibernate-onetomany


    【解决方案1】:

    类似这样的:

    select q from Question q
    where q not in (select a.question from Answer a)
    

    顺便说一句,您提供的查询“SQL 太多”,请记住,尽管它们的语法相似,但 SQL 和 JPQL 在概念上是不同的。

    【讨论】:

    • 谢谢,效果很好。我不知道如何编写 hql,所以我以这种方式为像我这样通常不阅读描述并跳到代码的人编写了半 SQL:P
    猜你喜欢
    • 2015-06-14
    • 1970-01-01
    • 2010-09-24
    • 2015-01-25
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多