@Formula 注释不理解“计数”

我有一个带有计算属性的实体组,我正在尝试使用 @Formula 进行计算。


还有其他三个实体具有该实体的外键,我要计算的是有多少实体依赖于每个 Group 实体,因此公式如下所示:


public class Group {


...


@Formula("(select count (distinct (s.id)) + count(distinct(ses.id)) + count (distinct(u.id))

    from section s, session ses, user u, group_user gu

    where s.group_id = id and ses.group_id = id and gu.user_id = u.id and

          gu.group_id = id )")

    private int dependencies;

}

但是当我在 Group 上执行 findAll 时,它会抛出异常:


ORA-00904: "GROUP0_"."COUNT": invalid identifier

生成的 SQL 看起来很奇怪,像这样:


    Hibernate: select * from ( select distinct group0_.id as id1_8_, group0_.active as active2_8_, group0_.code as code3_8_, 

... 

(select group0_.count (distinct (s.id)) + count(distinct(ses.id)) + group0_.count (distinct(u.id)) from section s, session ses, user u, group_user gu where s.group_id = group0_.id and ses.group_id = group0_.id and gu.user_id = u.id and gu.group_id = group0_.id ) as formula1_ 

from group group0_ where 1=1 order by group0_.code asc ) where rownum <= ?

我应该如何编写公式才能使其发挥作用?


红颜莎娜
浏览 141回答 1
1回答

12345678_0001

最后这只是一个愚蠢的问题:count 和 ( 如果我写公式:@Formula("(select&nbsp;count(distinct&nbsp;(s.id))&nbsp;+&nbsp;count(distinct(ses.id))&nbsp;+&nbsp;count(distinct(u.id)) &nbsp;&nbsp;&nbsp;&nbsp;from&nbsp;section&nbsp;s,&nbsp;session&nbsp;ses,&nbsp;user&nbsp;u,&nbsp;group_user&nbsp;gu &nbsp;&nbsp;&nbsp;&nbsp;where&nbsp;s.group_id&nbsp;=&nbsp;id&nbsp;and&nbsp;ses.group_id&nbsp;=&nbsp;id&nbsp;and&nbsp;gu.user_id&nbsp;=&nbsp;u.id&nbsp;and &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gu.group_id&nbsp;=&nbsp;id&nbsp;)")有用!!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java