![](/img/trans.png)
[英]Relationship between two entities of different schemes - Spring Boot Data JPA
[英]Two different queries returning the same object Spring Boot JPA
我用 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`
结果是正确的。 但是调试中的映射对象是相同的。 有什么建议吗?
我找到的解决方案是为每个查询创建一个不同的实体。 不是最好的选择,但这就是我现在所拥有的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.