【问题标题】:Two different queries returning the same object Spring Boot JPA两个不同的查询返回相同的对象 Spring Boot JPA
【发布时间】:2021-12-27 11:33:50
【问题描述】:

我创建了一个包含 3 个不同查询的存储库。每个查询返回不同的结果。但是当涉及到映射时,第一个查询结果会转到第二个查询结果。如果我更改查询的顺序,第一个结果将是下一个查询的结果。我什至尝试创建两个不同的存储库

这些是结果对象查询: First Result Second Result

这是我的第一个存储库:

package com.springboot.first.app;


@Repository
public interface TotalSRepository extends JpaRepository<TotalEntity, Date>{


@Query(value= "select `data` , SUM(importo) as totaleContanti, 0 AS totaleBanca from spese s where (dataAss is null or dataAss > DATE(NOW()) ) and MONTH(s.`data` )= :mese and year(s.`data`) = :anno and s.tipoPag IN('CONTANTI', 'VOUCHER') group by s.`data`", 
        countQuery = "SELECT count(*) from spese s where (dataAss is null or DATE(NOW()) >= dataAss) and MONTH(s.`data` )= :mese and year(s.`data`) = :anno and s.tipoPag IN('CONTANTI', 'VOUCHER') group by s.`data`", 
        nativeQuery = true)
        List<TotalEntity> findSpeseTotalsContanti(@Param("mese") Integer mese, @Param("anno") Integer anno);

@Query(value= "select `data` , SUM(importo) as totaleBanca, 0 AS totaleContanti from spese s where (dataAss is null or dataAss > DATE(NOW()) ) and MONTH(s.`data` )= :mese and year(s.`data`) = :anno and s.tipoPag NOT IN('CONTANTI', 'VOUCHER') group by s.`data`", 
        countQuery = "SELECT count(*) from spese s where (dataAss is null or DATE(NOW()) >= dataAss ) and MONTH(s.`data` )= :mese and year(s.`data`) = :anno and s.tipoPag NOT IN('CONTANTI', 'VOUCHER') group by s.`data`", 
        nativeQuery = true)
        List<TotalEntity> findSpeseTotalsBanca(@Param("mese") Integer mese, @Param("anno") Integer anno);

}

这是第二次回购

@Repository
public interface TotalRepository extends JpaRepository<TotalEntity, Date>{

@Query(value= "select `data`, (SUM(importoC) + SUM(importoT)) as totaleContanti, SUM(importoP) as totaleBanca from incassi i where MONTH(i.`data`) = :mese and YEAR(i.`data`) = :anno group by i.`data`", 
        countQuery = "SELECT count(*) from incassi i where MONTH(i.`data`) = :mese and YEAR(i.`data`) = :anno group by i.`data`", 
        nativeQuery = true)
        List<TotalEntity> findIncassiTotals(@Param("mese") Integer mese, @Param("anno") Integer anno);


}

这是实体

@Entity
public class TotalEntity {
private Date data;
private Double totaleBanca; 
private Double totaleContanti; 


public TotalEntity() {
}


@Id
@Column(name = "data")
public Date getData() {
    return data;
}

public void setData(Date data) {
    this.data = data;
}

@Basic
@Column(name = "totalebanca")
public double getTotaleBanca() {
    return totaleBanca;
}

public void setTotaleBanca(double importoC) {
    this.totaleBanca = importoC;
}


@Basic
@Column(name = "totalecontanti")
public double getTotaleContanti() {
    return totaleContanti;
}

public void setTotaleContanti(double importoC) {
    this.totaleContanti = importoC;
}



}

我尝试执行 Hibernate 记录查询

Hibernate: select `data`, (SUM(importoC) + SUM(importoT)) as totaleContanti, SUM(importoP) as totaleBanca from incassi i where MONTH(i.`data`) = ? and YEAR(i.`data`) = ? group by i.`data`
Hibernate: select `data` , SUM(importo) as totaleBanca, 0 AS totaleContanti from spese s where (dataAss is null or dataAss > DATE(NOW()) ) and MONTH(s.`data` )= ? and year(s.`data`) = ? and s.tipoPag NOT IN('CONTANTI', 'VOUCHER') group by s.`data`
Hibernate: select `data` , SUM(importo) as totaleContanti, 0 AS totaleBanca from spese s where (dataAss is null or dataAss > DATE(NOW()) ) and MONTH(s.`data` )= ? and year(s.`data`) = ? and s.tipoPag IN('CONTANTI', 'VOUCHER') group by s.`data`

结果是正确的。但是调试中的映射对象是相同的。有什么建议吗?

【问题讨论】:

    标签: java mysql spring-boot spring-mvc spring-data-jpa


    【解决方案1】:

    我找到的解决方案是为每个查询创建不同的实体。不是最好的选择,但这是我现在所拥有的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多