Constant value enum in HQL

I have a working query that I need to modify by filtering with a constant enumeration value.

Now it looks like this:

public static final String venueQuery = "select distinct v from package.Venue v " + "<some joins here> " + "WHERE v.venueType = package.enums.VenueType.VOUCHER_PROVIDER "; 

Changing data in this way causes

 org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token 

The column definition is as follows:

 @Enumerated(EnumType.STRING) @Column(name = "venue_type") private VenueType venueType; 

The definition of Enum is as follows:

 public enum VenueType { RESTAURANT, BAR, CAFE, FUN_CLUB, VOUCHER_PROVIDER } 

I am sure that other parts of the request work fine, because exceptions are not excluded after its removal.

Are there any tricks to set the enum constant value in an HQL query?

+7
java enums hibernate hql
source share
2 answers

The preferred way would be to add the addition of parameters to the query and pass the enum instance as the parameter value, but if you do not (or cannot) make it a parameterized query, you can still do it with String concatenation:

 public static final String venueQuery = "select distinct v from package.Venue v " + "<some joins here> " + "WHERE v.venueType = '" + VenueType.VOUCHER_PROVIDER.name() +"'"; 

If you want to receive a String compile time constant query:

 public static final String venueQuery = "select distinct v from package.Venue v " + "<some joins here> " + "WHERE v.venueType = 'VOUCHER_PROVIDER'"; 
+11
source share

Make sure the table column names and the new instance class property names are the same.

0
source share

All Articles