首页 > 解决方案 > 我可以在同一模型中与 JpaRepository 一起使用 redis 吗?

问题描述

嗨,我刚刚学习使用redis作为我的辅助数据库,我的代码仍然用于学习目的,我在这里要做的是,我将我的数据同时保存到Postgresql和redis中,但我什至无法得到在我编写保存功能之前,我的 spring boot 运行。我收到这些错误:

描述:

无法注册在 RedisRepositoriesRegistrar.EnableRedisRepositoriesConfiguration 上声明的 @EnableRedisRepositories 中定义的 com.example.demo.repository.StockRepository 中定义的 bean 'stockRepository'。具有该名称的 bean 已经在 com.example.demo.repository.StockRepository 中定义,在 DemoApplication 上声明的 @EnableJpaRepositories 中定义,并且覆盖被禁用。

行动:

考虑重命名 bean 之一或通过设置 spring.main.allow-bean-definition-overriding=true 来启用覆盖

我只是添加@RedisHash(value = "bookStockRedis")了我的模型并且发生了这种情况,这是我的模型看起来像:

@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;

}

这是我的存储库:

@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();

}

如您所见,我使用 JpaRepository 和 RevisionRepository 来管理我的 Postgresql 数据库。我在这里错过了什么?或者我应该克隆模型并将其与 JpaRepository 一和 Redis 一分开?

标签: javaspringspring-bootredis

解决方案


Keep the model as it is.

Have two instances of your repository layer

JPA repository

@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

@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

In your java config class you are having something like:

@EnableRedisRepositories
@EnableJpaRepositories
public class JpaRedisConfiguration {

}

update with

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

}

推荐阅读