I am using micronaut in backend and I have a table called transactions
in postgres which has about 11 columns in it. I want to make a query to select only 4 columns from it.
We also did a look at this stack overflow link but it does not help in micronaut. Official Docs also does not provide precise answers for the same. We did google lot of stuffs about this and finally solved by doing this:
- Created a DTO record for the same
MonthlyTransactionDTO.java
package com.example.transactions.dto;
import java.util.Date;
public record MonthlyTransactionDTO(Long id, Long userId, Date dateOfTransaction, double amount) {
}
- Customised the query in repository class
transactionsRepository.java
@Repository
public interface TransactionsRepository extends CrudRepository<Transaction, Long> {
@Query(value ="select new com.example.transactions.dto.MonthlyTransactionDTO(T.id, T.userId, T.dateOfTransaction, T.amount) from transactions T")
List<MonthlyTransactionDTO> findMonthlyTransactionsDetails(Long userId);
}
My question is: Is there any other nice way in which we can achieve to select specific columns or a way where I can write my custom SQL query in a clean way?
TBH, I really don't like this kind of approach to select specific columns from Database. Coz everytime when I change my package name I have to change this com.example.transactions.dto.
manually coz intelliJ does not auto detect it.
Would be very helpful if you could give me any leads or anything for the same problem. Thanks in advance :)