spring-data mongodb custom implementation PropertyReferenceException

↘锁芯ラ 提交于 2019-12-04 17:15:09

问题


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);
}

回答1:


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?



来源:https://stackoverflow.com/questions/17035419/spring-data-mongodb-custom-implementation-propertyreferenceexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!