【问题标题】:Trying to query in a table entity and store result in another object尝试在表实体中查询并将结果存储在另一个对象中
【发布时间】:2021-06-23 08:00:13
【问题描述】:

在我的 springboot 项目中,我需要在 Table 上运行 jpa 查询并将结果 Object 存储在不同的类对象中。 这是正确的方法吗?有没有简单的方法可以做到这一点..

JPA 存储库方法是: @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;

我收到以下错误 : 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

【问题讨论】:

标签: java spring-boot hibernate jpa spring-data-jpa


【解决方案1】:

您似乎需要聚合,所以我认为这是 Blaze-Persistence Entity Views 的完美用例。

我创建了该库以允许在 JPA 模型和自定义接口或抽象类定义模型之间轻松映射,例如 Spring Data Projections on steroids。这个想法是您按照自己喜欢的方式定义目标结构(域模型),并通过 JPQL 表达式将属性(getter)映射到实体模型。

使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:

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

Spring Data 集成让您可以像使用 Spring Data Projections 一样使用它:https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

List<DaybookBalance> findAll(Pageable pageable);

最好的部分是,它只会获取实际需要的状态!

【讨论】:

    猜你喜欢
    • 2011-02-11
    • 2015-06-02
    • 2017-05-08
    • 2017-06-30
    • 2019-08-17
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多