Hibernate 标准中关联的大小

发布于 2024-09-01 13:02:17 字数 1250 浏览 0 评论 0原文

我在 Grails 中使用 Hibernate Criteria 进行查询时遇到了问题。

看一下:

def visitors = Client.withCriteria{
          visits{
             use ( TimeCategory ) {between('date',date,date+1.month-1)}
          }
          sizeGe("visits",params.from)
          sizeLe("visits",params.to)
          fetchMode("visits", FM.JOIN)
};

我只需要那些每月访问次数界限之间的客户。

但现在大小*限制适用于所有访问。因此,如果客户在本月访问过一次,并且在上个月访问过一次。如果我设置from=2,则该客户端将成为结果。但它不应该在那里。

//更新: 事情是 sizeGesizeLe 限制并不像我预期的那样。

从我的角度来看,它们应该在 Between 限制之后应用,但事实并非如此。

例如:

def client = new Client();

client.visits.add(new Visit(date:'2010-03-16'));
client.visits.add(new Visit(date:'2010-05-16'));
client.visits.add(new Visit(date:'2010-05-17'));
client.save();

然后我想要 if:

  • date = Date.parse('yyyy-MM','2010-05')
  • params.from=2

标准应该返回此客户端,并且 if

  • params.from = 3

不返回;
但它会返回,因为 sizeGe 会应用于所有访问,无论它的日期是哪一天。

// 结束更新。

如果仍然不清楚,请告诉我。

任何帮助表示赞赏。

谢谢,沃娃。

I've faced with a problem when querying with Hibernate Criteria in Grails.

Take a look:

def visitors = Client.withCriteria{
          visits{
             use ( TimeCategory ) {between('date',date,date+1.month-1)}
          }
          sizeGe("visits",params.from)
          sizeLe("visits",params.to)
          fetchMode("visits", FM.JOIN)
};

I need only those clients, which has number of visits in month between from and to bounds.

But now size* restrictions is being applied to all visits. So if client has one visit in this month, and visit in previous month. And if I set from=2, this client will be in result. But it should not be there.

//UPD:
the thing as that sizeGe and sizeLe restrictions work not as I expect.

From my point of view they should be applied after between restriction, but they not.

For example:

def client = new Client();

client.visits.add(new Visit(date:'2010-03-16'));
client.visits.add(new Visit(date:'2010-05-16'));
client.visits.add(new Visit(date:'2010-05-17'));
client.save();

And then I want if:

  • date = Date.parse('yyyy-MM','2010-05')
  • params.from=2

criteria should return this client, and if

  • params.from = 3

not return;
But it returns, because sizeGe is being applied to all visits, no matter which date it has.

// END UPD.

Let me know if smth still isn't clear.

Any help is appreciated.

Thanks, Vova.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[旋木] 2024-09-08 13:02:17

你需要一个having子句来做到这一点,我在 Issue open in Hibernate's 中发现Jira 添加对此的支持。

同时,您应该使用 HQL 并手动编写查询, 看到这个。如果您不需要根据某些选定字段更改查询的限制,我应该始终使用 HQL,它可以被缓存。

You'd need a having clause to do this and I found on issue open in Hibernate's Jira to add support for this.

Meanwhile you should use HQL and write the query by hand, see this. If you don't need to change the query's restrictions based on some selected fields you should I always go with HQL, it can be cached.

煮酒 2024-09-08 13:02:17

谢谢费利佩。

另外,我选择使用 sqlRestriction。当然,它有一些可移植性问题,但它有效:

def visitors = c.listDistinct{
        use(TimeCategory){
                visits{

                    between('date',date,date+1.month-1)
                }

                sqlRestriction(
                        """(
                            select count(*)
                                  from visit v
                            where
                                v.visitor_id={alias}.id
                                and v.date between 
                                      '${date.format("yyyy-MM-dd")}' 
                                      and '${(date+1.month-1).format("yyyy-MM-dd")}'
                            )
                            between ${params.from} and ${params.to}"""
                )
            }
    }

谢谢,Vova。

Thanks Felipe.

Also, I've choose to use sqlRestriction. Of course, it has some portability issues but it works:

def visitors = c.listDistinct{
        use(TimeCategory){
                visits{

                    between('date',date,date+1.month-1)
                }

                sqlRestriction(
                        """(
                            select count(*)
                                  from visit v
                            where
                                v.visitor_id={alias}.id
                                and v.date between 
                                      '${date.format("yyyy-MM-dd")}' 
                                      and '${(date+1.month-1).format("yyyy-MM-dd")}'
                            )
                            between ${params.from} and ${params.to}"""
                )
            }
    }

Thanks, Vova.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文