为什么 Spring 数据存储库中的此方法被视为查询方法?
Why is this method in a Spring Data repository considered a query method?
我们已经实现了一个应该能够使用 JPA、Couchbase 或 MongoDB 的应用程序。 (目前,未来可能会增加)。我们通过为每个项目分离存储库成功地实现了 JPA 和 Couchbase。 JPA 将来自 org.company.repository.jpa 而 couchbase 将来自 org.company.repository.cb。所有存储库接口都扩展了 org.company.repository 中的公共存储库。我们现在通过创建一个新包 org.company.repository.mongo 来瞄准 MongoDB。但是我们遇到了这个错误:
No property updateLastUsedDate found for type TokenHistory!
这是我们的代码:
@Document
public class TokenHistory extends BaseEntity {
private String subject;
private Date lastUpdate;
// Getters and setters here...
}
在org.company.repository.TokenHistoryRepository.java
下
@NoRepositoryBean
public interface TokenHistoryRepository<ID extends Serializable> extends TokenHistoryRepositoryCustom, BaseEntityRepository<TokenHistory, ID> {
// No problem here. Handled by Spring Data
TokenHistory findBySubject(@Param("subject") String subject);
}
// The custom method
interface TokenHistoryRepositoryCustom {
void updateLastUsedDate(@Param("subject") String subject);
}
在org.company.repository.mongo.TokenHistoryMongoRepository.java
下
@RepositoryRestResource(path = "/token-history")
public interface TokenHistoryMongoRepository extends TokenHistoryRepository<String> {
TokenHistory findBySubject(@Param("subject") String subject);
}
class TokenHistoryMongoRepositoryCustomImpl {
public void updateLastUsedDate(String subject) {
//TODO implement this
}
}
以及Mongo配置
@Configuration
@Profile("mongo")
@EnableMongoRepositories(basePackages = {
"org.company.repository.mongo"
}, repositoryImplementationPostfix = "CustomImpl",
repositoryBaseClass = BaseEntityRepositoryMongoImpl.class
)
public class MongoConfig {
}
JPA 和 Couchbase 的设置相同,但我们没有遇到该错误。它能够使用带有 "CustomImpl" 前缀的内部 class,根据文档应该是这种情况。
我的 MongoDB 设置或配置有问题吗?
你的TokenHistoryMongoRepositoryCustomImpl
实际上并没有实现TokenHistoryRepositoryCustom
接口,这意味着我们无法找到class中的updateLastUsedDate(…)
是被认为是接口方法的实现。因此,它被认为是一种查询方法,然后触发查询推导。
我非常怀疑这是否适用于声称的其他商店,因为代码检查查询方法在 DefaultRepositoryInformation
中共享。
我们已经实现了一个应该能够使用 JPA、Couchbase 或 MongoDB 的应用程序。 (目前,未来可能会增加)。我们通过为每个项目分离存储库成功地实现了 JPA 和 Couchbase。 JPA 将来自 org.company.repository.jpa 而 couchbase 将来自 org.company.repository.cb。所有存储库接口都扩展了 org.company.repository 中的公共存储库。我们现在通过创建一个新包 org.company.repository.mongo 来瞄准 MongoDB。但是我们遇到了这个错误:
No property updateLastUsedDate found for type TokenHistory!
这是我们的代码:
@Document
public class TokenHistory extends BaseEntity {
private String subject;
private Date lastUpdate;
// Getters and setters here...
}
在org.company.repository.TokenHistoryRepository.java
下@NoRepositoryBean
public interface TokenHistoryRepository<ID extends Serializable> extends TokenHistoryRepositoryCustom, BaseEntityRepository<TokenHistory, ID> {
// No problem here. Handled by Spring Data
TokenHistory findBySubject(@Param("subject") String subject);
}
// The custom method
interface TokenHistoryRepositoryCustom {
void updateLastUsedDate(@Param("subject") String subject);
}
在org.company.repository.mongo.TokenHistoryMongoRepository.java
下@RepositoryRestResource(path = "/token-history")
public interface TokenHistoryMongoRepository extends TokenHistoryRepository<String> {
TokenHistory findBySubject(@Param("subject") String subject);
}
class TokenHistoryMongoRepositoryCustomImpl {
public void updateLastUsedDate(String subject) {
//TODO implement this
}
}
以及Mongo配置
@Configuration
@Profile("mongo")
@EnableMongoRepositories(basePackages = {
"org.company.repository.mongo"
}, repositoryImplementationPostfix = "CustomImpl",
repositoryBaseClass = BaseEntityRepositoryMongoImpl.class
)
public class MongoConfig {
}
JPA 和 Couchbase 的设置相同,但我们没有遇到该错误。它能够使用带有 "CustomImpl" 前缀的内部 class,根据文档应该是这种情况。
我的 MongoDB 设置或配置有问题吗?
你的TokenHistoryMongoRepositoryCustomImpl
实际上并没有实现TokenHistoryRepositoryCustom
接口,这意味着我们无法找到class中的updateLastUsedDate(…)
是被认为是接口方法的实现。因此,它被认为是一种查询方法,然后触发查询推导。
我非常怀疑这是否适用于声称的其他商店,因为代码检查查询方法在 DefaultRepositoryInformation
中共享。