Hibernate version: 3.1.2
This has probably been done a 100 times before, but as of yet I havn't found a solution.
I have a entity User that has a set of zero or more enums (UserRole) assigned to it. The mapping document was easy to create and uses a UserType similar to the class described
here.
I want to do a Criteria query to return the set of User(s) that have one of a given UserRole assigned.
The mapping document is straight-forward (trimmed for simplicity).
Mapping documents:
Code:
<hibernate-mapping>
<class name="com.test.data.User" table="users">
<id name="id" column="id">
<generator class="native"/>
</id>
<natural-id mutable="false">
<property name="login" type="string" column="login" />
</natural-id>
<property name="email" type="string" column="email" />
<component name="name" class="com.test.data.component.Name">
<property name="prefix" type="string" column="prefix"/>
<property name="firstName" type="string" column="first_name"/>
<property name="lastName" type="string" column="last_name"/>
</component>
<set name="roles" table="user_roles" lazy="false">
<key column="login" property-ref="login" />
<element column="role" type="com.test.hibernate.HibernateUserRole" />
</set>
</class>
</hibernate-mapping>
Unfortunately, I've tried a bunch of variations but I still can get the query to work (various exceptions based on the current query attempt). Here's is a sample usage:
Code between sessionFactory.openSession() and session.close():
Code:
Criteria crit = session.createCriteria(User.class);
// magic code to allow me to return only those users with a specificed role(s) ...
// something like:
Disjunction or = Expression.disjunction();
or.add(Expression.eq("???", UserRole.ADMIN);
or.add(Expression.eq("???", UserRole.READER);
crit.add(or);
// end ...
// should contain User instances
List list = crit.list();
Name and version of the database you are using: MySQL 4.1.10