Issues with creating a query when using Enum in essence

In the Question object, I have the following:

@NamedQuery(name = "Question.allApproved", query = "SELECT q FROM Question q WHERE q.status = 'APPROVED'") 

and

 @Enumerated(EnumType.STRING) private Status status; // usual accessors 

I get this exception:

Exception Description: Request compilation failed [Question.countApproved: SELECT COUNT(q) FROM Question q WHERE q.status = 'APPROVED' ], row 1, column 47: invalid enumeration equal to expression, cannot compare enumeration value of type [myCompnay.application.Status] with an unlisted value of type [java.lang.String] . in org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy (EntityManagerSetupImpl.java►01)

How to fix it?

+46
java java-ee jpa
Nov 21 '11 at 19:08
source share
3 answers

I think you should use your (fully qualified) Status enum instead of a literal value, so something like this: (if your Status enum is in the com.myexample package)

 @NamedQuery(name = "Question.allApproved", query = "SELECT q FROM Question q WHERE q.status = com.myexample.Status.APPROVED"). 
+85
Nov 21 '11 at 19:10
source share

Use the property below in application.properties logging.level.org.hibernate.type.descriptor.sql.BasicBinder = TRACE

-one
Jan 28 '17 at 9:10
source share

4 years after the initial publication, some changes occurred. Using spring 4 and Hibernate 4, you can now fool Hibernate using the SpEL expression. For example:

Listing:

 package com.mycompany.enums public enum Status { INITIAL, PENDING, REJECTED, APPROVED, SHIPPED, DELIVERED, COMPLETE; } 

Here is a wrapper class called "Filter", which we will move on to the repository filtering method.

 package com.mycompany.enums public class Filter implements Serializable { /** The id of the filtered item */ private Integer id; /** The status of the filtered item */ private Status status; // more filter criteria here... // getters, setters, equals(), hashCode() - omitted for brevity /** * Returns the name of the status constant or null if the status is null. This is used in the repositories to filter * queries by the status using a the SPEL (T) expression, taking advantage of the status qualified name. For example: * {@code :#{T(com.mycompany.enums.Status).#filter.statusName}} * * @return the status constant name or null if the status is null */ public String getStatusName() { return null == status ? status : status.name(); } } 

Finally, in the repository we can now use the Filter class as the only parameter and make the request translate what seems to be a mixture of literals and SpEL expressions to the Status object:

Repository:

 package com.mycompany.repository @Repository public interface OrderRepository extends CrudRepository<Order, Integer> { @Query("SELECT o from Order o " + "WHERE o.id = COALESCE(:#{#filter.id},o.id) " + "AND o.status = COALESCE(:#{T(com.mycompany.enums.Status).#filter.statusName},o.status)") public List<Order> getFilteredOrders(@Param(value = "filter") Filter filter); } 

This works fine, but for some odd reason, I still don't understand if you turn on SQL debugging in Hibernate and turn on bind logging, you won’t be able to see that Hibernate binds this expression to variable requests.

-2
Apr 30 '16 at 14:52
source share



All Articles