Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

convert or cast output of JpaRepository

I have three interfaces that extend JpaRepository and a custom interface that has two functions. These two functions have their special output in each of three interfaces.
I implement it with List in the custom interface, so I have to use same output in the three interfaces, but I need to convert them to their special type.
Here is the custom interface:

public interface DataRepo {

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

And here are the three interfaces:

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>
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Make your custom repo generic:

public interface DataRepo<T> {

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

And then use it for example with bTable:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading