转换或转换JpaRepository的输出

dgsult0t  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(122)

我有三个扩展JpaRepository的接口和一个包含两个函数的自定义接口。这两个函数在三个接口中的每一个中都有其特殊的输出。我在自定义接口中用List实现了它,所以我必须在三个接口中使用相同的输出,但我需要将它们转换为它们的特殊类型。下面是自定义接口:

public interface DataRepo {

  List<Object> findAllEmptyData();
  List<Object> findAllByCreatedAtBefore(String createdAt);
}

下面是三个接口:

public interface bTableRepo extends JpaRepository<bTable, Integer>, DataRepo {

  @Query("SELECT b FROM bTable b WHERE b.value is null AND b.data")
  List<Object> findAllEmptyData();

  @Query("SELECT b FROM bTable b WHERE b.createdAt < ?1")
  List<Object> findAllByCreatedAtBefore(LocalDate createdAt); //I need to convert List<Object> to List<bTable>
}

public interface cTableRepo extends JpaRepository<cTable, Integer>, DataRepo {

  @Query("SELECT c FROM cTable c WHERE c.value is null AND c.dData is null AND c.date is null")
  List<Object> findAllEmptyData();

  @Query("SELECT c FROM cTable c WHERE c.createdAt < ?1")
  List<Object> findAllByCreatedAtBefore(LocalDate createdAt); //I need to convert List<Object> to List<cTable>
}

public interface dTableRepo extends JpaRepository<dTable, Integer>, DataRepo {

  @Query("SELECT d FROM dTable d WHERE d.value is null AND d.date is null AND d.DateAndTime is null")
  List<Object> findAllEmptyData();

  @Query("SELECT d FROM cTable d WHERE d.createdAt < ?1")
  List<Object> findAllByCreatedAtBefore(LocalDate createdAt); //I need to convert List<Object> to List<dTable>
}
kadbb459

kadbb4591#

使自定义存储库通用:

public interface DataRepo<T> {

  List<T> findAllEmptyData();
  List<T> findAllByCreatedAtBefore(String createdAt);
}

然后以bTable为例使用它:

public interface bTableRepo extends JpaRepository<bTable, Integer>, DataRepo<bTable> {

相关问题