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: '&'
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.