I am using spring -data-mongodb 1.8.0; MongoDB 3.0.6; mongo-java-driver 3.1.0; spring -framework.version 4.0.3.
I want to request a user list with specific phone numbers. example for the user: { "_id" : ObjectId("5625e5c32e1ca013a03f0d1b"), "phone" : "12345535"}
On Mongo Shell, db.user.find({phone: { $in: [ "12345535", "123535"]}}) works fine. But in Spring, I failed. Java class user (with no getters / setters):
@Document(collection = "user") public class User { @Id String id; String phone; }
What I tried:
Query q = new Query(Criteria.where("phone").in("12345535","123535")); mongoTemplate.find(q, User.class);
Comes an error:
Exception in thread "main" java.lang.IllegalAccessError: tried to access class org.springframework.beans.PropertyMatches from class org.springframework.data.mapping.PropertyReferenceException at org.springframework.data.mapping.PropertyReferenceException.detectPotentialMatches(PropertyReferenceException.java:134) at org.springframework.data.mapping.PropertyReferenceException.<init>(PropertyReferenceException.java:59) at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.getPath(QueryMapper.java:837) at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:729) at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.with(QueryMapper.java:740) at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.with(QueryMapper.java:686) at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedKeyword(QueryMapper.java:258) at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObjectForField(QueryMapper.java:200) at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:123) at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1700) at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1690) at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:602) at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:593) at com.example.TestMongo.main(TestMongo.java:30)
But when changing the field telephone to id, the same code works fine.
Query q = new Query(Criteria.where("id").in("5625e5c32e1ca013a03f0d1b","f0d1e")); mongoTemplate.find(q, User.class);
When debugging, I found that it did not even go into the request phase, an error occurred at the stage of building the request. It seems that $in cannot be handled by PropertyPath.create , and in case of id it can.
How can i fix this? I am a newbie and searched a lot, but no luck. Can you help me. Each answer is welcome. Thanks guys.
source share