【发布时间】: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