spring-data mongodb自定义实现PropertyReferenceException [英] spring-data mongodb custom implementation PropertyReferenceException

查看:205
本文介绍了spring-data mongodb自定义实现PropertyReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据参考4.4自定义实现来实现自定义查询:

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

Spring Data的MongoTemplate和MongoRepository有什么区别?

我这样做是因为我需要使用mongoTemplate进行特殊查询.

I'm doing this because I need special queries using mongoTemplate.

我得到的错误是PropertyReferenceException.因此,似乎spring-data试图自动生成我不想要的查询.我想使用自己的自定义查询.

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-自定义存储库功能

问题

如何在不使用spring-data尝试自动生成查询的情况下实现我的自定义查询接口和实现?

How can I implement my custom query interface and implementation without spring-data trying to auto-generate the query?

配置

春季配置

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(接口) com.eerra.core.common.service.UserRepositoryCustomImpl.java(实现)

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

推荐答案

问题已解决.错误地命名Impl类时,将出现此错误.必须根据存储库类来命名Impl类.因此,此示例的名称必须为以下名称:

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(主存储库)
  • com.eerra.core.common.service.UserRepositoryInterfaceImpl.java(自定义存储库方法的实现)
  • com.eerra.core.common.service.UserRepositoryInterfaceCustom.java(具有自定义方法的接口)

在此处查看答案: Spring Data的MongoTemplate和MongoRepository有什么区别?

这篇关于spring-data mongodb自定义实现PropertyReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆