I'm trying to implement a custom query according to the Reference 4.4 Custom Implementations:
http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/repositories.html
What's the difference between Spring Data's MongoTemplate and MongoRepository?
I'm doing this because I need special queries using mongoTemplate.
The error I'm getting is a PropertyReferenceException. So it seems that spring-data is trying to auto-generate the query which I don't want. I want to use my own custom query.
org.springframework.data.mapping.PropertyReferenceException: No property search found for type com.eerra.core.common.dto.User
The problem is described also here but the solution doesn't seem to work for me:
http://forum.springsource.org/showthread.php?114454-Custom-repository-functionality
Question
How can I implement my custom query interface and implementation without spring-data trying to auto-generate the query?
Configuration
Spring Configuration
spring-data.xml
<!-- Spring Data MongoDB repository support -->
<mongo:repositories base-package="com.eerra.*.common.service" />
The Repository classes and interfaces are located in following package:
com.eerra.core.common.service.UserRepositoryInterface.java com.eerra.core.common.service.UserRepoistoryCustom.java (interface) com.eerra.core.common.service.UserRepositoryCustomImpl.java (implementation)
UserRepositoryCustom.java
public interface UserRepositoryCustom {
List<User> searchAllUsers();
}
UserRepositoryCustomImpl.java
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<User> searchAllUsers() {
return mongoTemplate.findAll(User.class);
}
}
UserRepositoryInterface.java
@Repository
public interface UserRepositoryInterface extends MongoRepository<User, String>, UserRepositoryCustom {
User findByEmail(String email);
List<User> findByEmailLike(String email);
List<User> findByEmailOrLastName(String email, String lastName);
List<User> findByEmailOrFirstNameLike(String email, String firstName);
@Query("{\"$or\" : [ { \"email\" : { \"$regex\" : ?0, \"$options\" : \"i\"}} , " +
"{ \"firstName\" : { \"$regex\" : ?0, \"$options\" : \"i\"}}, " +
"{ \"lastName\" : { \"$regex\" : ?0, \"$options\" : \"i\"}}]}")
List<User> findByEmailOrFirstNameOrLastNameLike(String searchText);
}
The problem is solved. This error appears when the Impl class is named incorrectly. The Impl class has to be named according to the repository class. So the names have to be following for this example:
See the answer here: What's the difference between Spring Data's MongoTemplate and MongoRepository?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With