0

In my springboot project, I need to run a jpa query on a Table and store the result Object in a different class object. Is it a right way? Are there any simple ways to do this..

The JPA Repository method is : @query("select date, sum(crAmt), sum(drAmt) from Daybook u where u.date = ?1")

public DaybookBalance findDaybookBalance(String d1);

@Table(name="daybook")
public class Daybook implements Comparable<Daybook>{
    @Id
    @Column(name="dbid")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long dbId;
    
    private int sNo;
    private String date;
    private String narration;
    private int acccode;
    private Double drAmt;
    private Double crAmt;
    private int sktValue;

public class DaybookBalance {

    private String date;
    private long crTot;
    private long drTot;

I got the following error : No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [in.trident.crdr.entities.DaybookBalance], mergedContextConfiguration = [MergedContextConfiguration@51dcb805 testClass = UserRepositoryTests, locations = '{}', classes = '{class in.trident.crdr.CrDrReportApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@66498326 key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration

Nandha
  • 192
  • 1
  • 2
  • 11
  • Try this: https://stackoverflow.com/questions/36328063/how-to-return-a-custom-object-from-a-spring-data-jpa-group-by-query – Volodya Lombrozo Mar 26 '21 at 13:52
  • Does this answer your question? [No converter found capable of converting from type to type](https://stackoverflow.com/questions/46083329/no-converter-found-capable-of-converting-from-type-to-type) – Paul Mar 26 '21 at 15:38

1 Answers1

1

You seem to require aggregation, so I think this is a perfect use case for Blaze-Persistence Entity Views.

I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.

A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:

@EntityView(Daybook.class)
public interface DaybookBalance {
    @IdMapping
    String getDate();
    @Mapping("SUM(crAmt)")
    Double getCrTot();
    @Mapping("SUM(drAmt)")
    Double getDrTot();
}

The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

List<DaybookBalance> findAll(Pageable pageable);

The best part is, it will only fetch the state that is actually necessary!

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58