-->
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.  [ 6 posts ] 
Author Message
 Post subject: Enums in static JPQL queries
PostPosted: Mon Oct 02, 2006 10:14 am 
Newbie

Joined: Mon Oct 02, 2006 9:24 am
Posts: 2
Using: Hibernate 3.2 CR4 / hibernate-annotations-3.2.0.CR1

According to Java EE 5 Tutorial & Specs enums should be allowed in static JPQL queries:

Quote:

Enum Literals

The Java Persistence Query Language supports the use of enum literals using the Java enum literal syntax. The enum class name must be specified as fully qualified class name.

Code:
SELECT e
FROM Employee e
WHERE e.status = com.xyz.EmployeeStatus.FULL_TIME


(http://java.sun.com/javaee/5/docs/tutor ... ml#wp80051)



Having for example:
Code:

package test;

enum Status {
  ACTIVE, DELETED
}


@Entity
@NamedQueries( {
   @NamedQuery(name="Country.selectAll",    
            query="SELECT c from Country c " +
     "WHERE c.status = test.Status.ACTIVE" ),
class Country {
...
@Enumerated
Status status;

}



This kind of exception is throwed:

Code:
15:59:45,823 ERROR [SessionFactoryImpl] Error in named query: Country.selectAll
org.hibernate.QueryException: Could not format constant value to SQL literal: . [SELECT c from test.Country c WHERE c.status = test.Status.ACTIVE]
   at org.hibernate.hql.ast.util.LiteralProcessor.setConstantValue(LiteralProcessor.java:177)
   at org.hibernate.hql.ast.util.LiteralProcessor.lookupConstant(LiteralProcessor.java:114)
   at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:178)
   at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
....



Does it mean JBoss EJB 3.0 is not 100% compatible with the specs, therefore I should use some kind of enum UserTypes (what is against the spirit of EJB3) or, what is worse, use parameters+dynamic queries?

Thanks for help,

Konrad


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 5:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
AFAIK and unit test, it does work on the latest releases.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 03, 2006 9:38 am 
Newbie

Joined: Mon Aug 15, 2005 2:00 pm
Posts: 19
it does indeed work on the latest version (CR4/CR2)

Was it ever supposed to be working for enums declared within an entity (like below)?

Code:
@Entity
@NamedQueries ( { @NamedQuery (name = "Visit.getInProgress", query = "SELECT v FROM Visit v WHERE v.status = test.Visit.Status.InProgress") })
public class Visit {
   public static enum Status {
      Closed,
      InProgress,
      TestResultsGenerated
   }
   @Id
   private Long id;
   @Enumerated (EnumType.STRING)
   private Status status;

   public Long getId() {
      return id;
   }
   public Status getStatus() {
      return status;
   }
   public void setId(Long id) {
      this.id = id;
   }
   public void setStatus(Status status) {
      this.status = status;
   }
}


Currently, it ends with an exception:

Code:
15   [main] ERROR org.hibernate.impl.SessionFactoryImpl  - Error in named query: Visit.getInProgress
org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'test.Visit.Status.InProgress' [SELECT v FROM test.Visit v WHERE v.status = test.Visit.Status.InProgress]
   at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
   at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
   at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:235)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
   at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:400)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:351)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1218)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:688)
   at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:127)


Top
 Profile  
 
 Post subject: Works fine at last
PostPosted: Thu Oct 05, 2006 6:14 am 
Newbie

Joined: Mon Oct 02, 2006 9:24 am
Posts: 2
Thank you both for help, after updating to JBos 4.0.4, and patching with EJB 3.0 RC9 it works fine now.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 4:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
ASavitsky wrote:
Was it ever supposed to be working for enums declared within an entity (like below)?


Can you open a JIRA issue (simple test case would be cool) on the Hibernate3 project?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 11:42 am 
Newbie

Joined: Mon Aug 15, 2005 2:00 pm
Posts: 19
Finally found the proper way to make internal enums work, too - it requires the classfile notation (Visit$Status) instead of the Java notation (Visit.Status). That is, the following DOES work:

Code:
WHERE v.status = test.Visit$Status.InProgress


Maybe it's worth to put a mention of that in the docs? I'm sure internal enums are quite a commonplace...


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