HibernateEntityQuery 中的位函数

发布于 2024-12-05 08:52:50 字数 1111 浏览 1 评论 0原文

是否可以在使用 Seam 的 HibernateEntityQuery 的查询中使用位函数?

我刚刚继承了一些基于 JSF、Seam & 的代码。冬眠。现有的 DAO 代码利用了 Seam 的 HibernateEntityQuery,特别是它对限制表达式字符串的支持。

到目前为止,这一切都运行良好,但现在我的任务是向现有查询添加一个考虑位字段的限制。

Java 实体代码通过此枚举使用整数字段来表示一组“天”:

public enum WEEKDAY {

    MONDAY(1), TUESDAY(2), WEDNESDAY(4), THURSDAY(8), FRIDAY(16), SATURDAY(32), SUNDAY(64) ;

    private int value;

    private WEEKDAY(int value) {
        this.value = value;
    }
}

此枚举中的整数值存储到实体的 int days 字段中。

例如,如果实体包含 MONDAY、WEDNESDAY 和 FRIDAY,则 days 将设置为 1 | 8 | 16,其中 == 25

现在我想向查询添加新的限制,以便(例如)我可以请求天数包含星期一或星期六的实体: 1 | 32 == 33,所以在 SQL 中,我会这样做:

select id from entity where (select days & 33) > 0;

HibernateEntityQuery 有这样的语法吗?我尝试添加以下限制:

entity.days & #{myQuery.bitwiseMeetingDays} > 0

这会导致以下错误:

org.hibernate.QueryException: unexpected char: '&'

Is it possible to use bit functions in queries using Seam's HibernateEntityQuery?

I've just inherited some code based on JSF, Seam & Hibernate. The existing DAO code makes use of Seam's HibernateEntityQuery, in particular its support for restriction expression strings.

This has worked fine so far, but now I'm tasked with adding a restriction to an existing query that takes into account a bit field.

The Java entity code is using an integer field to represent a set of "days", via this enum:

public enum WEEKDAY {

    MONDAY(1), TUESDAY(2), WEDNESDAY(4), THURSDAY(8), FRIDAY(16), SATURDAY(32), SUNDAY(64) ;

    private int value;

    private WEEKDAY(int value) {
        this.value = value;
    }
}

The integer value from this enum is stored into the entity in an int days field.

For example, if the entity contains MONDAY, WEDNESDAY AND FRIDAY, days would be set to 1 | 8 | 16, which == 25.

Now I'd like to add a new restriction to the query so that (for example) I can ask for entities where days includes MONDAY OR SATURDAY: 1 | 32 == 33, so in SQL, I would do:

select id from entity where (select days & 33) > 0;

Is there such a syntax for HibernateEntityQuery? I tried adding the following restriction:

entity.days & #{myQuery.bitwiseMeetingDays} > 0

which results in the following error:

org.hibernate.QueryException: unexpected char: '&'

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

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

发布评论

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

评论(1

画中仙 2024-12-12 08:52:50

我最终重构了该表,使其具有 7 个布尔字段,而不是一个表示“天”的整数。这也使得命令行查询变得更加容易。

不过,如果知道一种处理位字段查询的方法仍然会很高兴。

I ended up refactoring the table to have 7 boolean fields rather than a single integer for "days". This makes command-line queries easier as well.

It would still be nice to know of a way to handle the bit-field query though.

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