问题
I have a class like this:
class MyEntity {
@ElementCollection
Map<String, String> properties;
}
I'd like to find out which MyEntity entities have a property value that matches like
query using the criteria API. By this I mean I'd like to make a like
query on the values of the map entries.
For example if one of my MyEntity classes has a property named "email" and the value is "example@mail.com", how do I make a query that finds the entity with a query parameter "example%" using criteria API?
回答1:
Okay, I found a solution. There is a joinMap(String) method in Path API which can be used in this case:
builder.like(
root.<MyEntity, String, String>joinMap("properties").value(), "example%");
That piece of code will create a like
predicate against the values of the properties map. This would probably been easier to find out if I had generated the MetaModel of the MyEntity...
来源:https://stackoverflow.com/questions/7066122/how-to-make-a-like-query-to-elementcollection-of-type-map