I am trying to be able to select from multiple projections as return type in a collection when accessing a method in a jpa repository
I have come across this thread, however its information is not really sufficient: Spring Data Jpa generic projection findAll
It seems like i cant use a list, but i dont understand why and its not explained in that thread.
I want to get the following code to work:
public interface ThemingRepository extends JpaRepository<ThemeData, Long> {
<T> List<T> findByOrderByIdAsc(Class<T> type);
}
@Transactional
public byte[] findBackgroundImage() {
var list = themingRepository.findByOrderByIdAsc(ThemeDataBackgroundImage.class);
if (list.isEmpty()) {
throw new NoThemeColorsSetException();
}
return list.get(0).getBackgroundImage();
}
@Transactional
public byte[] findLogoImage() {
var list = themingRepository.findByOrderByIdAsc(ThemeDataLogoImage.class);
if (list.isEmpty()) {
throw new NoThemeColorsSetException();
}
return list.get(0).getLogo();
}
@Transactional
public ThemeDataColors findThemeColors() {
var list = themingRepository.findByOrderByIdAsc(ThemeDataColors.class);
if (list.isEmpty()) {
throw new NoThemeColorsSetException();
}
return list.get(0);
}
An example projection:
public interface ThemeDataBackgroundImage {
byte[] getBackgroundImage();
}