简体   繁体   English

我可以在同一个 model 中的 JpaRepository 旁边使用 redis 吗?

[英]Can i use redis along side JpaRepository in the same model?

Hi i just learn to use redis as my secondary database, my code is still for learning purpose, what i trying to do here is, i going to save my data into Postgresql and redis in a same time, but i can't even get my spring boot run, before i code the save function.嗨,我刚刚学会使用 redis 作为我的辅助数据库,我的代码仍然用于学习目的,我在这里要做的是,我将把我的数据保存到 Postgresql 和 Z86A1B907D54BF7010374BF316E183,但我可以在同一时间我的 spring 启动运行,在我编码保存 function 之前。 i got these error:我收到这些错误:

Description:描述:

The bean 'stockRepository', defined in com.example.demo.repository.StockRepository defined in @EnableRedisRepositories declared on RedisRepositoriesRegistrar.EnableRedisRepositoriesConfiguration, could not be registered.无法注册在 RedisRepositoriesRegistrar.EnableRedisRepositoriesConfiguration 上声明的 @EnableRedisRepositories 中定义的 com.example.demo.repository.StockRepository 中定义的 bean 'stockRepository'。 A bean with that name has already been defined in com.example.demo.repository.StockRepository defined in @EnableJpaRepositories declared on DemoApplication and overriding is disabled.具有该名称的 bean 已在 com.example.demo.repository.StockRepository 中定义,在 DemoApplication 上声明的 @EnableJpaRepositories 中定义,并且覆盖已禁用。

Action:行动:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true考虑重命名 bean 之一或通过设置 spring.main.allow-bean-definition-overriding=true 来启用覆盖

i just add @RedisHash(value = "bookStockRedis") in my model and that happend, here is my model looked like:我只是在我的 model 中添加@RedisHash(value = "bookStockRedis")并且发生了,这是我的 model 看起来像:

@Data
@Entity
@Audited
@EntityListeners(AuditingEntityListener.class)
@EqualsAndHashCode(of = "id")
@ToString(of = { "id" })
@Table(name = "bookStock")
@RedisHash(value = "bookStockRedis")
public class Stock extends AuditField {

    @Schema(description = "Id merupakan primary key, tipe datanya Long", example = "0", required = true)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Schema(description = "Warehouse Id yang merupakan Id yang di ambil dari Id di tabel transaksi, di jadikan id warehouse, ber tipe Long", example = "1", required = true)
    @NotNull
    private Long warehouseId;

    @Schema(description = "Kode gudang yang merupakan kode yang di ambil dari transaction code, di jadikan kode gudang virtual", example = "DP/2020/01", required = true)
    @NotBlank(message = "Tidak Boleh Kosong")
    private String warehouseCode;

    @Schema(description = "Tanggal transaksi, tanggal di ambil dari tanggal transaksi", example = "1", required = true)
    @NotNull
    private Date transactionDate;

    @Schema(description = "Trans Id yang merupakan Id yang di ambil dari Id di tabel transaksi, di jadikan id transaksi, ber tipe Long", example = "1", required = true)
    @NotNull
    private Long transId;

    @Schema(description = "Kode transaksi yang merupakan kode yang di ambil dari transaction code setiap transaksi, di jadikan trans number", example = "DP/2020/01", required = true)
    @NotBlank(message = "Tidak Boleh Kosong")
    private String transNumber;

    @Schema(description = "Product Id yang merupakan Id yang di ambil dari Id di tabel master product", example = "1", required = true)
    @NotNull
    private Long productId;

    @Schema(description = "Kode Product yang merupakan kode yang di ambil dari master product", example = "MCM-508", required = true)
    @NotBlank(message = "Tidak Boleh Kosong")
    private String productCode;

    @Schema(description = "Jumlah qty yang akan di kurang atau di tambah, bisa berupa value plus (tidak perlu tanda minus) untuk menambah stock, bisa berupa value minus (perlu tanda minus) untuk mengurangi stock ketika di jumlahkan", example = "10", required = true)
    @NotNull
    private BigInteger qty;

    @Schema(description = "id dari sebuah branch", example = "1", required = true)
    @NotNull
    private Long branchId;

    @Schema(description = "Kode Branch", required = true)
    @NotBlank(message = "Tidak Boleh Kosong")
    private String branchCode;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transTypeId")
    private TransType transType;

}

and here is my repository:这是我的存储库:

@Repository
public interface StockRepository extends RevisionRepository<Stock, Long, Integer>, JpaRepository<Stock, Long> {

    @Query(value = "here is manual sql query that is too long since i don't think there is to do with it, i just change it to this string to keep the code simple")
    List<Tuple> findStocktotal();

}

as you can see, i use JpaRepository and RevisionRepository to manage my Postgresql database.如您所见,我使用 JpaRepository 和 RevisionRepository 来管理我的 Postgresql 数据库。 What do i missed here?我在这里错过了什么? or i should clone the model and separate it from JpaRepository one and Redis one?或者我应该克隆 model 并将其与 JpaRepository 一和 Redis 一分开?

Keep the model as it is.保持 model 原样。

Have two instances of your repository layer有两个存储库层实例

JPA repository JPA 存储库

@Repository("jpaStockRepository")
public interface JpaStockRepository extends JpaRepository<Stock, Long>

    @Query(value = "here is manual sql query that is too long since i don't think there is to do with it, i just change it to this string to keep the code simple")
    List<Tuple> findStocktotal();
}

Redis repository Redis 存储库

@Repository("redisStockRepository")
public interface RedisStockRepository extends RevisionRepository<Stock, Long, Integer> {

    @Query(value = "query")
    List<Tuple> findStocktotal();

}

In your service layer use it like:在您的服务层中使用它:

@Service
public class StockService {
   
   @Autowired
   private RedisStockRepository redisStockRepo;

   @Autowired
   private JpaStockRepository jpaStockRepo;
}

UPDATE 1更新 1

In your java config class you are having something like:在你的 java 配置 class 你有类似的东西:

@EnableRedisRepositories
@EnableJpaRepositories
public class JpaRedisConfiguration {

}

update with更新

@EnableRedisRepositories(repositoryImplementationPostfix="redis")
@EnableJpaRepositories(repositoryImplementationPostfix="jpa")
public class JpaRedisConfiguration {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在 JpaRepository nativeQuery 中使用枚举参数吗? - Can I use enum parameter into JpaRepository nativeQuery? 如何在服务类中使用带有 @Autowired 的 JpaRepository? - How can I use a JpaRepository, with @Autowired, inside a service class? 我想以通用方式使用Spring JPARepository,因此我不必为所有实体编写同一行代码 - I want to Use Spring JPARepository in Generic way, so that same line of code i have to not write for all enitties 如何使用Jetty为Angular应用程序提供服务,还可以使用与Jersey相同的服务器? - How can I serve an Angular app using Jetty and also use the same server along with Jersey? 我可以强制maven依赖项A使用特定版本的依赖项B,同时使用其他版本的依赖项B吗? - Can I force a maven dependency A to use a specific version of a dependency B, whilst using a different version of dependency B along-side it? 如何在后端线程中使用JpaRepository? - How do I use JpaRepository in a backend thread? 如何在PrePersist批注内使用JPARepository? - How do I use a JPARepository inside of a PrePersist Annotation? 我应该如何在SpringBoot中使用JpaRepository.findOne()? - How should I use JpaRepository.findOne() with SpringBoot? 将 OrderBy 与 JPARepository 结合使用 - Use OrderBy with JPARepository 在JpaRepository中不使用@Query注释 - Use @Query annotation not in the JpaRepository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM