For the next code I have some questions:
- Why do I need to create a Criteria object with an init key when I have to create a '$or' query?
- Is the query with name 'query' created with 'criteria' well formed? The execution of the method throws the exception wich trace is listed after the code in the line with 'for( DBObject dbo: cursor )', before executing conversion.
Code:
@Override
public List<UserReference> searchUsers( final String searchStr )
{
final List<UserReference> userReferences = new ArrayList<UserReference>();
List<Query> queriesList = new ArrayList<Query>();
queriesList.add( Query.query( Criteria.where( "name" ).regex("*"+searchStr+"*") ) );
queriesList.add( Query.query( Criteria.where( "surname" ).regex("*"+searchStr+"*") ) );
queriesList.add( Query.query( Criteria.where( "email" ).regex("*"+searchStr+"*") ) );
Criteria criteria = Criteria.where( "state" ).is( User.GuestState.ACTIVE.name() );
criteria.or( queriesList );
final Query query = Query.query( criteria );
mongoTemplate.execute( "user", new CollectionCallback<Boolean>()
{
public Boolean doInCollection( DBCollection collection )
throws MongoException, DataAccessException
{
DBObject ref = query.getQueryObject();
DBObject fields = new BasicDBObject();
fields.put( "name", 1 );
fields.put( "surname", 1 );
fields.put( "email", 1 );
fields.put( "_id", 1 );
DBCursor cursor = collection.find( ref, fields, 0, 5 );
for( DBObject dbo: cursor )
{
userReferences.add( mongoConverter.read( UserReference.class, dbo ) );
}
return true;
}
} );
return userReferences;
}
Code:
java.lang.IllegalArgumentException: can't serialize class org.springframework.data.document.mongodb.query.Query
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:213)
at org.bson.BSONEncoder.putIterable(BSONEncoder.java:238)
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:181)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:123)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:69)
at com.mongodb.OutMessage.putObject(OutMessage.java:189)
at com.mongodb.OutMessage._appendQuery(OutMessage.java:62)
at com.mongodb.OutMessage.query(OutMessage.java:39)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:293)
at com.mongodb.DBCursor._check(DBCursor.java:354)
at com.mongodb.DBCursor._hasNext(DBCursor.java:484)
at com.mongodb.DBCursor.hasNext(DBCursor.java:509)
at org.myproject.data.repositories.custom.impl.UserRepositoryImpl$3.doInCollection(UserRepositoryImpl.java:113)
at org.myproject.data.repositories.custom.impl.UserRepositoryImpl$3.doInCollection(UserRepositoryImpl.java:1)
at org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:363)
at org.myproject.data.repositories.custom.impl.UserRepositoryImpl.searchUsers(UserRepositoryImpl.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358)
at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:328)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy33.searchUsers(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy34.searchUsers(Unknown Source)
at org.myproject.data.services.impl.UserServiceImpl.searchUsers(UserServiceImpl.java:147)
at org.myproject.data.tests.UsersServiceTest.testSearchUser(UsersServiceTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)