Details
Description
I am trying to do a search on a localized string. The strings are mapped by an ElementCollection of type Map. Key is a string interpreted as locale.
Here is the entity:
@Entity
@Table(name="category", schema="myschema")
@Access(AccessType.FIELD)
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@ElementCollection(fetch=FetchType.EAGER)
Map<String, String> test = new HashMap<String, String>();
...
}
Here is the query:
String search = "%test%";
Locale locale = Locale.ENGLISH;
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Category> criteria = cb.createQuery(Category.class);
Root<Category> category = criteria.from(Category.class);
criteria.select(category);
MapJoin<Category, String, String> tests = category.join(Category_.test);
Predicate searchClause = cb.and(
cb.equal(tests.key(), locale.toString()),
cb.like(tests.value(), search)
);
criteria.where(searchClause);
entityManager.createQuery(criteria).getResultList();
When the query is executed, I get this stack trace:
[err] java.lang.NullPointerException
[err] at org.apache.openjpa.jdbc.kernel.exps.PCPath.<init>(PCPath.java:105)
[err] at org.apache.openjpa.jdbc.kernel.exps.JDBCExpressionFactory.newPath(JDBCExpressionFactory.java:302)
[err] at org.apache.openjpa.persistence.criteria.Joins$MapKey.toValue(Joins.java:625)
[err] at org.apache.openjpa.persistence.criteria.Expressions.toValue(Expressions.java:66)
[err] at org.apache.openjpa.persistence.criteria.Expressions$Equal.toKernelExpression(Expressions.java:851)
[err] at org.apache.openjpa.persistence.criteria.PredicateImpl.toKernelExpression(PredicateImpl.java:171)
[err] at org.apache.openjpa.persistence.criteria.CriteriaExpressionBuilder.evalFilter(CriteriaExpressionBuilder.java:216)
[err] at org.apache.openjpa.persistence.criteria.CriteriaExpressionBuilder.getQueryExpressions(CriteriaExpressionBuilder.java:75)
[err] at org.apache.openjpa.persistence.criteria.CriteriaQueryImpl.getQueryExpressions(CriteriaQueryImpl.java:418)
[err] at org.apache.openjpa.persistence.criteria.CriteriaBuilderImpl.eval(CriteriaBuilderImpl.java:83)
[err] at org.apache.openjpa.kernel.ExpressionStoreQuery$DataStoreExecutor.<init>(ExpressionStoreQuery.java:763)
[err] at org.apache.openjpa.kernel.ExpressionStoreQuery.newDataStoreExecutor(ExpressionStoreQuery.java:179)
[err] at org.apache.openjpa.kernel.QueryImpl.createExecutor(QueryImpl.java:749)
I assume the key() function returns an invalid or non-existing path.
When I remove the key() part the query works.