We're updating the issue view to help you get more done. 

Conversion from String to Locale not working when using Criteria API with Restriction

Description

I have following table:

1 2 3 create table user_user ( locale varchar(10) )

That has following contents:

Table user_user

locale

en_US

fi_FI

sv_SE

I have following class:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 package broken.test; import java.util.Locale; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name = "user_user") public class User { private Locale locale; @Column(length = 10) public Locale getLocale(){ return locale; } public void setLocale(Locale locale){ this.locale = locale; } /** * Gets user's locale in String form. * * @return */ @Transient public String getLocaleAsString(){ return getLocale() != null ? getLocale().toString() : null; } /** * Sets user's locale in String form. String consists of both ISO language * code and country (separated with underscore), or just language code. * Examples: <tt>fi_FI</tt>, <tt>sv_SE</tt>, <tt>en_US</tt>, <tt>fi</tt>, * <tt>no</tt> * * @param localeAsString */ public void setLocaleAsString(String localeAsString){ if(localeAsString != null){ String[] languageAndCountry = localeAsString.split("_"); if(languageAndCountry.length == 1){ setLocale(new Locale(languageAndCountry[0])); } else{ setLocale(new Locale(languageAndCountry[0], languageAndCountry[1])); } } else{ setLocale(null); } } }

which is mapped to that table as you can see. Fetching the contents of the table works fine:

1 2 3 Criteria elements = getSession().createCriteria(User.class); List<User> entities = elements.list(); System.out.println(entities.get(0).getLocale()); // wuhuu, prints en_US

The problems start when I want to have a freetext search that would search some arbitrary string from all fields of user entity:

1 2 3 4 5 6 Criteria elements = getSession().createCriteria(User.class); // okay, not a free text search, but this is what it translates to elements.add(Restrictions.eq("locale", "fi_FI")); List<User> entities = elements.list(); // suddenly throws java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Locale

As a side note, the above code works fine if you have Hibernate 3.5.6-Final.

Here's the full stack trace:

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Locale
at org.hibernate.type.descriptor.java.LocaleTypeDescriptor.unwrap(LocaleTypeDescriptor.java:19)
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:46)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:73)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:260)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:255)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:2045)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:2014)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1944)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1897)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1875)
at org.hibernate.loader.Loader.doQuery(Loader.java:919)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doList(Loader.java:2611)
at org.hibernate.loader.Loader.doList(Loader.java:2594)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423)
at org.hibernate.loader.Loader.list(Loader.java:2418)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1705)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)

Environment

Windows 7

ds.driverClassName=org.hsqldb.jdbcDriver
ds.url=jdbc:hsqldb:mem:testdb
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.hbm2ddl.auto=create-drop

Status

Assignee

Unassigned

Reporter

Jukka Hämäläinen

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.0.2

Priority

Major