将 Spring 数据与 PagingAndSortingRepository 和 IgniteRepository 一起使用会引发错误
Using Spring data with PagingAndSortingRepository and IgniteRepository is throwing error
我正在研究一个 spring 数据项目,并试图将 Ignite 缓存与其集成。
我已经在使用 PagingAndSortingRepository
@Repository public interface UserRepository extends PagingAndSortingRepository<User, String> {
Page<User> findByUserId(final String userId, final Pageable pageable);
Page<User> findByFirstName(final String firstName, final Pageable pageable);
Page<User> findByEmailAddress(final String emailAddress, final Pageable pageable);
Page<User> findAllBy(final Pageable pageable);
}
并且我添加了一个新的存储库 IgniteRepository
@RepositoryConfig(cacheName = "UserCache")
public interface UserCacheRepository extends IgniteRepository<UserCacheDto, String> {
@Query("select UserCacheDto FROM UserCacheDto WHERE userId = ?")
List<UserCacheDto> findByUserId(final String userId);
@Query("select UserCacheDto FROM UserCacheDto WHERE firstName = ?")
List<UserCacheDto> findByFirstName(final String firstName);
@Query("select UserCacheDto FROM UserCacheDto WHERE emailAddress = ?")
List<UserCacheDto> findByEmailAddress(final String emailAddress);
}
此设置背后的想法是仅当在缓存中找不到数据时才从数据库中获取数据
下面是我的项目依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.src.portal</groupId>
<artifactId>portalDataBaseCacheModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>portalDataBaseCacheModule</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring.version>2.5.4</spring.version>
<ignite.version>2.9.0</ignite.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-data_2.2</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-boot-autoconfigure-ext</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-zookeeper</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j2</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.8.Final</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
进行此集成后,出现以下编译错误,
java: name clash: deleteAllById(java.lang.Iterable) in
org.apache.ignite.springdata22.repository.IgniteRepository and
deleteAllById(java.lang.Iterable<? extends ID>) in
org.springframework.data.repository.CrudRepository have the same
erasure, yet neither overrides the other
从错误看来,由于这两个存储库都在内部扩展 CrudRepository,所以它导致了一个问题,我无法弄清楚为什么以及如何解决这个问题。
请在这里帮忙。
根据您的错误 UserCacheRepository
继承 IgniteRepository.deleteAllById(Iterable<K> ids)
和 CrudRepository.deleteAllById(Iterable<? extends ID> ids)
,但是 Iterable<K>
和 Iterable<? extends ID>
的擦除是相同的(只是 Iterable
),所以我们会遇到 class 有两个签名完全相同的方法,这会导致名称冲突错误。
这个错误的根本原因是 IgniteRepository
最初是在 CrudRepository
没有 deleteAllById()
方法时编写的,同时 CrudRepository
带有现代版本的 spring-boot-starter-data-jpa有这个方法
如果您的应用程序的其余部分可能的话,您可以尝试对 spring 数据使用旧版本。
您也可以尝试显式覆盖 deleteAllById(Iterable iterable) 方法,但我不确定它是否有帮助。
最好的选择是更新 apache-ignite-extensions 以使用最新的 spring 数据,因此您可以为此在 Apache Ignite 项目中创建 Jira 票证。
我正在研究一个 spring 数据项目,并试图将 Ignite 缓存与其集成。
我已经在使用 PagingAndSortingRepository
@Repository public interface UserRepository extends PagingAndSortingRepository<User, String> {
Page<User> findByUserId(final String userId, final Pageable pageable);
Page<User> findByFirstName(final String firstName, final Pageable pageable);
Page<User> findByEmailAddress(final String emailAddress, final Pageable pageable);
Page<User> findAllBy(final Pageable pageable);
}
并且我添加了一个新的存储库 IgniteRepository
@RepositoryConfig(cacheName = "UserCache")
public interface UserCacheRepository extends IgniteRepository<UserCacheDto, String> {
@Query("select UserCacheDto FROM UserCacheDto WHERE userId = ?")
List<UserCacheDto> findByUserId(final String userId);
@Query("select UserCacheDto FROM UserCacheDto WHERE firstName = ?")
List<UserCacheDto> findByFirstName(final String firstName);
@Query("select UserCacheDto FROM UserCacheDto WHERE emailAddress = ?")
List<UserCacheDto> findByEmailAddress(final String emailAddress);
}
此设置背后的想法是仅当在缓存中找不到数据时才从数据库中获取数据
下面是我的项目依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.src.portal</groupId>
<artifactId>portalDataBaseCacheModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>portalDataBaseCacheModule</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring.version>2.5.4</spring.version>
<ignite.version>2.9.0</ignite.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-data_2.2</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-boot-autoconfigure-ext</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-zookeeper</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j2</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.8.Final</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
进行此集成后,出现以下编译错误,
java: name clash: deleteAllById(java.lang.Iterable) in org.apache.ignite.springdata22.repository.IgniteRepository and deleteAllById(java.lang.Iterable<? extends ID>) in org.springframework.data.repository.CrudRepository have the same erasure, yet neither overrides the other
从错误看来,由于这两个存储库都在内部扩展 CrudRepository,所以它导致了一个问题,我无法弄清楚为什么以及如何解决这个问题。 请在这里帮忙。
根据您的错误 UserCacheRepository
继承 IgniteRepository.deleteAllById(Iterable<K> ids)
和 CrudRepository.deleteAllById(Iterable<? extends ID> ids)
,但是 Iterable<K>
和 Iterable<? extends ID>
的擦除是相同的(只是 Iterable
),所以我们会遇到 class 有两个签名完全相同的方法,这会导致名称冲突错误。
这个错误的根本原因是 IgniteRepository
最初是在 CrudRepository
没有 deleteAllById()
方法时编写的,同时 CrudRepository
带有现代版本的 spring-boot-starter-data-jpa有这个方法
如果您的应用程序的其余部分可能的话,您可以尝试对 spring 数据使用旧版本。
您也可以尝试显式覆盖 deleteAllById(Iterable iterable) 方法,但我不确定它是否有帮助。
最好的选择是更新 apache-ignite-extensions 以使用最新的 spring 数据,因此您可以为此在 Apache Ignite 项目中创建 Jira 票证。