-->
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.  [ 3 posts ] 
Author Message
 Post subject: Is it possible to use enum in HQL query
PostPosted: Fri Apr 21, 2006 9:25 pm 
Newbie

Joined: Tue Jul 20, 2004 1:25 pm
Posts: 12
Hibernate version: 3.1.3
Hibernate Annoations 3.1 beta9

I have properties defined on an enumerated type. Is it possible to use an enum in an HQL query? The enum in question is a lookup table and is references, via @Enumerated, by mapped entities.

Enumerated type definition...
Code:
public enum ComponentType
{
   GLOBAL(1,"Global","Global"),
   ... other enum types ...

   private long   _id;
   private String   _name;
   private String   _description;
   
   ComponentType(long id, String name, String desc)
   {
      _id = id;
      _name = name;
      _description = desc;
   }

   ... getters/setters ...
}



I then have a mapped entity that references this enum

Code:
@Entity
@Table(name="Component")
@SequenceGenerator(name="ComponentSeq",sequenceName="COMPONENT_SEQ")
public class Component implements Serializable
{

   private long         _id;
   private ComponentType   _componentType;

   @Id
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ComponentSeq")
   public long getId()
   {
      return _id;
   }

   public void setId(long id)
   {
      _id = id;
   }

   @Enumerated
   @Column(name="componenttype_id",nullable=false)
   public ComponentType getComponentType()
   {
      return _componentType;
   }

   public void setComponentType(ComponentType componentType)
   {
      _componentType = componentType;
   }
}


Now I want to query for Components that have a particular ComponentType.name like so...

Code:
   Query query = session.createQuery("from Component where componentType.name = :typeName");
   query.setParameter("typeName", ComponentType.GLOBAL, Hibernate.STRING);


When I execute the query I get an exception saying "could not resolve property: name"

Is it possible to use an enum type this way or will I have to map the ComponentType class as a normal class and use static constants instead of an enum type?

thanks,
Brett


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 02, 2008 6:47 am 
Newbie

Joined: Sun Feb 24, 2008 5:12 pm
Posts: 9
I have the same issue
Does somebody know the solution?

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 02, 2008 7:31 am 
Newbie

Joined: Sun Feb 24, 2008 5:12 pm
Posts: 9
ok, nevermind - solved it
i had:
Code:
        @Enumerated(EnumType.ORDINAL)
   @Column(name="myenum")
   private MyEnumType enumType;

and i was trying to query:
Code:
where o.myenum=?

instead
Code:
where o.enumType=?


Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.