Spring Data JDBC - 无法安全地确定存储分配给仓库候选项

5

我对一次提交进行了几处更改,但突然间我的Spring程序似乎无法初始化实体并将它们链接到存储库。当我启动Spring时,我的日志如下:

2022-07-10 16:39:17.137  INFO 18040 --- [  restartedMain] com.vendify.BackendDbApplication         : Starting BackendDbApplication using Java 17.0.2 on DESKTOP-HSDNP64 with PID 18040 (applicaiton started by (my name) in directory)
2022-07-10 16:39:17.140  INFO 18040 --- [  restartedMain] com.vendify.BackendDbApplication         : No active profile set, falling back to default profiles: default
2022-07-10 16:39:17.186  INFO 18040 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-07-10 16:39:17.187  INFO 18040 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-07-10 16:39:18.193  INFO 18040 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2022-07-10 16:39:18.194  INFO 18040 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2022-07-10 16:39:18.240  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.Auth0TokensCacheRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.242  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.ItemIndexRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.244  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.ItemsRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.246  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.LoggingRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.248  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.SerialsRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.251  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.TransactionsRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.253  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.ValidUsersRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.255  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.VendorsRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.256  INFO 18040 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 58 ms. Found 0 JDBC repository interfaces.
2022-07-10 16:39:18.266  INFO 18040 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2022-07-10 16:39:18.267  INFO 18040 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-07-10 16:39:18.329  INFO 18040 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 51 ms. Found 8 JPA repository interfaces.
2022-07-10 16:39:19.172  INFO 18040 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2022-07-10 16:39:19.184  INFO 18040 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-07-10 16:39:19.184  INFO 18040 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
2022-07-10 16:39:19.275  INFO 18040 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-07-10 16:39:19.276  INFO 18040 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2089 ms
2022-07-10 16:39:19.754  INFO 18040 --- [  restartedMain] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 7.7.3 by Redgate
2022-07-10 16:39:19.760  INFO 18040 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-07-10 16:39:20.192  INFO 18040 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-07-10 16:39:20.240  INFO 18040 --- [  restartedMain] o.f.c.i.database.base.DatabaseType       : Database: jdbc:mysql://localhost:3306/vendify (MySQL 8.0)
2022-07-10 16:39:20.276  INFO 18040 --- [  restartedMain] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.015s)
2022-07-10 16:39:20.284  INFO 18040 --- [  restartedMain] o.f.core.internal.command.DbMigrate      : Current version of schema `vendify`: 1
2022-07-10 16:39:20.285  INFO 18040 --- [  restartedMain] o.f.core.internal.command.DbMigrate      : Schema `vendify` is up to date. No migration necessary.
2022-07-10 16:39:20.502  INFO 18040 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-07-10 16:39:20.564  INFO 18040 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.32.Final
2022-07-10 16:39:20.705  INFO 18040 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-07-10 16:39:20.866  INFO 18040 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2022-07-10 16:39:21.555  INFO 18040 --- [  restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-07-10 16:39:21.563  INFO 18040 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-07-10 16:39:22.307  WARN 18040 --- [  restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-07-10 16:39:22.779  INFO 18040 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@6c38469b, org.springframework.security.web.context.SecurityContextPersistenceFilter@52d983cb, org.springframework.security.web.header.HeaderWriterFilter@67a31d2b, org.springframework.security.web.authentication.logout.LogoutFilter@2c8dea82, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@631bab6, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1f96917a, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@64458fc6, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@46d290b3, org.springframework.security.web.session.SessionManagementFilter@1312d3d0, org.springframework.security.web.access.ExceptionTranslationFilter@37e85798, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@2cc95e93]
2022-07-10 16:39:23.986  INFO 18040 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-07-10 16:39:23.995  INFO 18040 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 14 endpoint(s) beneath base path '/****{hidden for security}'
2022-07-10 16:39:24.057  INFO 18040 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2022-07-10 16:39:24.073  INFO 18040 --- [  restartedMain] com.vendify.BackendDbApplication         : Started BackendDbApplication in 7.398 seconds (JVM running for 7.938)

主要,我担心的错误日志是这样的:

2022-07-10 16:39:18.240  INFO 18040 --- [  restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.Auth0TokensCacheRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.

这是我编写实体类的示例(它们全部都是这样编写的,并且我已经有一段时间没有修改它们了。在此之前,它们似乎是可以工作的...)

package com.vendify.Entities;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.lang.NonNull;

import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "Logging")
@Getter
@Setter
public class LoggingEntity {

//specific code removed for security

}

使用类似于以下的存储库类:

package com.vendify.Repositories;

import java.util.Date;
import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.vendify.Entities.Auth0TokensCacheEntity;

@RepositoryRestResource(exported = false)
public interface Auth0TokensCacheRepository extends CrudRepository<Auth0TokensCacheEntity, String> {

}

作为证明,我的MySQL数据库结构表看起来有点像这样:

enter image description here

Spring似乎能够连接到数据库,而且一直以来都没有问题……我添加了一些包,它会影响其他一些东西,似乎就是在那时出现了问题,因此为了提供一些背景信息,下面是我的程序大致的布局:

enter image description here

非常感谢您的帮助!祝愿你和你的项目顺利!
更新:我尝试了Dhana建议的用JPARepository替换CRUDRepository等注释的解决方案(谢谢:)),Spring找到了所有这些repositories。但是,现在我的所有API端点都没有响应,甚至连开发者工具的端点也没反应了。当我添加@ComponentScan(basePackageClasses = {my controller classes})时,它会报一个应用程序错误,说Spring找不到服务类。请注意,截至目前,当我调用一个端点时,函数仍然可以正常运行(它可以正确地system.out.print输出结果),但因某种原因,无法将其正确返回到Web服务器以显示。
Description:

Field auth0TokensCacheService in com.vendify.Controllers.{ControllerClass} required a bean of type 'com.vendify.Services.{ControllerClass}' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.vendify.Services.{ControllerClass}' in your configuration.

我的Service类看起来像这样:

    @Service
public class HiddenService {
    @Autowired
    HiddenRepository hiddenRepository;
//methods in here
}

我的Controller长这样:

@RestController
public class HiddenController {

@GetMapping("/endpointURL")
//method here

} 

感谢!

我想赞扬这篇文章的作者,在贴出Java代码时,特别注明了具体的包导入,尽管他是个IntelliJ用户。有很多包里都有Table类,其中一些并不明显该选哪一个。你会选择Hibernate的吗?还是JPA、Spring Boot呢?省略包的做法是不太规范的。 - Woodsman
3个回答

10
让我解释一下,据我理解,在项目中有多个spring-data模块和Spring Data JDBC的情况下,如果您尝试通过扩展通用模块中未带有特定持久性注释的特定实体的接口来创建接口,那么这肯定行不通,因为spring基本上不知道您的实体与哪个数据存储相关联。因此,尽量将其扩展到特定的存储库中,例如,如果您使用MySQL,请选择JpaRepository而不是CrudRepository,无论如何,在层次结构的某个点上它们都会相互关联,建议将实体与特定存储库关联起来。
最后,请确保将以下内容添加到您各自的文件中:
1. 在自定义repo中添加@Repository 2. 将以下代码添加到application.properties:
spring.data.jdbc.repositories.enabled=false 希望对您有所帮助! :)

嘿,Dhana,我按照你的要求做了,它有点起作用了!我将我的@RepositoryRestResource(exported = false)更改为@Repository,将CrudRepository更改为JpaRepository,并添加了spring.data.jdbc.repositories.enabled=false。Spring成功找到了存储库,但由于某种原因,我的控制器端点不起作用,即使访问localhost:8081(是的,我正在运行8081),也没有任何反应。即使对于我的执行器,尽管Spring在启动时说“在基本路径'/ {hidden for security}'下公开14个端点”,但当我去那里时,我得到404错误。 - Jason Chan
嗨,Jason,我想我找到了你的问题所在,它不是代码的问题,而是你创建的包结构有误。我会在你分享的特定帖子中发送我的结构,这样你就可以解决这个疑问了。 - Dhana_Kulkarni

1
如果您遇到未识别的软件包错误,则可以尝试按照以下方式构建项目。

Project Stucture

Detailed structure of the project


搞定了!我有一个日志记录服务,看起来像这样:@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {} 这是导致问题的原因。非常感谢你! - Jason Chan

0
我在pom.xml中有这3个库。
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>

我移除了 spring-data-jpaspring-boot-starter-data-jdbc,问题解决了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接