Since the JPA 2.2 version, you can use the getResultStream Query method to transform the List result into a Map<Long, String>
Map<Long, String> findAllComuneIdAndDescrizione = entityManager.createQuery("""
SELECT COM_PRG_PK, COM_DESCRIZIONE FROM com_comune ORDER BY
com_descrizione
""", Tuple.class)
.getResultStream()
.collect(
Collectors.toMap(
tuple -> ((Long) tuple.get("COM_PRG_PK")).longValue(),
tuple -> ((String)tuple.get("COM_DESCRIZIONE")).stringValue()
)
);
OR... the following repository class might give you an idea
@Repository
public interface DBReportRepository extends JpaRepository<TransactionModel, Long> {
List<TransactionModel> findAll();
default Map<Long, TransactionModel> findAllMap() {
return findAll().stream().collect(Collectors.toMap(TransactionModel::getId, v -> v));
}
List<TransactionModel> findByClientId(Long id);
default Map<Long, TransactionModel> findByClientIdMap(Long id) {
return findByClientId(id).stream().collect(Collectors.toMap(TransactionModel::getId, v -> v));
}
}