-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: criteria search on a collection of mapped enums
PostPosted: Tue May 16, 2006 10:32 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 11:20 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm pretty sure that you can't query on a collection of values, only entities. Not even in HQL. You'll have to map your enum as a class, then convert it to an enum in your User object. That is, the collection in User that hibernate writes to/reads from is a collection of UserRoleClass objects, but you can provide mehtods like
Code:
public List<UserRole> getUserRoles() {
  List<UserRole> roles = new ArrayList<UserRole>(getInternalRoles().size());
  for (UserRoleClass r : getInternalRoles())
  {
    roles.add(Enum.valueOf(UserRole.class, r.getEnumName());
  }
  return roles;
}
Where r.getEnumName() is a method in your mapped class that returns the name of the associated Enum instance (e.g. ADMIN or READER, from your example).

It's a pain, and we do see questions about it on the forum quite a lot.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.