Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-data mongodb custom implementation PropertyReferenceException

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);
}
like image 821
Christopher Armstrong Avatar asked Jun 11 '13 01:06

Christopher Armstrong


1 Answers

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:

  • com.eerra.core.common.service.UserRepositoryInterface.java (main repository)
  • com.eerra.core.common.service.UserRepositoryInterfaceImpl.java (implementation of custom repository methods)
  • com.eerra.core.common.service.UserRepositoryInterfaceCustom.java (interface with custom methods)

See the answer here: What's the difference between Spring Data's MongoTemplate and MongoRepository?

like image 125
Christopher Armstrong Avatar answered Sep 28 '22 15:09

Christopher Armstrong