【问题标题】:Can i use redis along side JpaRepository in the same model?我可以在同一模型中与 JpaRepository 一起使用 redis 吗?
【发布时间】:2021-03-17 05:16:48
【问题描述】:

您好,我刚刚学习使用 redis 作为我的辅助数据库,我的代码仍然用于学习目的,我在这里要做的是,我将我的数据同时保存到 Postgresql 和 redis,但我可以'在我编写保存功能之前,甚至没有让我的 spring boot 运行。我收到了这些错误:

说明:

bean 'stockRepository',定义在 com.example.demo.repository.StockRepository 定义在 @EnableRedisRepositories 声明于 RedisRepositoriesRegistrar.EnableRedisRepositoriesConfiguration,可以 不被注册。已在中定义了具有该名称的 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 一个分开?

【问题讨论】:

  • 将“StockRepository”拆分为两个独立的接口——一个用于 Redis,一个用于 JPA。我看不出你为什么要拆分模型。考虑使用两个不同的名称:@Repository("redisRepo") / @Repository("jpaRepo")
  • 你能给我完整的例子吗?我尝试添加另一个扩展 PagingAndSortingRepository 的存储库,但仍然出现该错误

标签: java spring spring-boot redis


【解决方案1】:

保持模型不变。

有两个存储库层实例

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("redisStockRepository")
public interface RedisStockRepository extends RevisionRepository<Stock, Long, Integer> {

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

}

在你的服务层使用它:

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

   @Autowired
   private JpaStockRepository jpaStockRepo;
}

更新 1

在你的 java 配置类中,你有类似的东西:

@EnableRedisRepositories
@EnableJpaRepositories
public class JpaRedisConfiguration {

}

更新

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

}

【讨论】:

  • 不,这不起作用,我的应用程序仍然无法启动,而且我仍然给我同样的错误
  • 我已经更新了回复 - 试试看 :)
  • 当我在 @EnableJpaRepositories 中添加 (repositoryImplementationPostfix="jpa") 时,它给我错误“注释类型 EnableJpaAuditing 的属性 repositoryImplementationPostfix 未定义”我错过了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 2023-03-03
  • 2011-10-27
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
相关资源
最近更新 更多