JPQL查询和本机查询在spring-data-jpa

时间:2018-05-29 09:00:56

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

我在我的项目中使用spring-data-jpa。我试图使用@Query注释来获取上周最后一次的记录。

JPQL

@Query("SELECT SUM(t.quantity) as QTY, SUM(t.retailAmount) as RETAIL_AMT, SUM(t.discountAmount) as DISCOUNT_AMT, SUM(t.netAmount) as NET_AMT, SUM(t.costAmount) as COST_AMT, SUM(t.profit) as PROFIT, t.transactionDate as DATE FROM SaleByTransaction t where t.transactionDate >= :date group by t.transactionDate")
public List<Object[]> aggregateTransactionsBasedOnDate(@Param("date") Date date);

原生查询

@Query(nativeQuery = true, value = "SELECT SUM(QTY) as QTY, SUM(RETAIL_AMT) as RETAIL_AMT, SUM(DISCOUNT_AMT) as DISCOUNT_AMT, SUM(NET_AMT) as NET_AMT, SUM(COST_AMT) as COST_AMT, SUM(PROFIT) as PROFIT, TRANS_DATE FROM SALE_BY_TRANSACTION where TRANS_DATE >= :date group by TRANS_DATE")
public List<Object[]> aggregateTransactionsBasedOnDate(@Param("date") Date date);

预计记录应为6,因为我在数据库中有6天的记录。

JPQL Query提供了6条预期的记录。 Native Query提供5条记录,这是不正确的。

但是当我在我的数据库工具中运行本机查询时,它会获取正确的6条记录。 使用mysql作为我的数据库。

我将值传递为'2018-05-22'。当我尝试调试本机查询时,即使我的条件为TRANS_DATE&gt; =:date

,也没有获取2018-05-22的记录

任何人都遇到过类似的问题。可能是什么原因。

在两种情况下由hibernate打印的查询 -

原生查询

Hibernate: SELECT SUM(QTY) as QTY, SUM(RETAIL_AMT) as RETAIL_AMT, SUM(DISCOUNT_AMT) as DISCOUNT_AMT, SUM(NET_AMT) as NET_AMT, SUM(COST_AMT) as COST_AMT, SUM(PROFIT) as PROFIT, TRANS_DATE FROM SALE_BY_TRANSACTION where TRANS_DATE >= ? group by TRANS_DATE

JPQL

Hibernate: select sum(salebytran0_.qty) as col_0_0_, sum(salebytran0_.retail_amt) as col_1_0_, sum(salebytran0_.discount_amt) as col_2_0_, sum(salebytran0_.net_amt) as col_3_0_, sum(salebytran0_.cost_amt) as col_4_0_, sum(salebytran0_.profit) as col_5_0_, salebytran0_.trans_date as col_6_0_ from sale_by_transaction salebytran0_ where salebytran0_.trans_date>=? group by salebytran0_.trans_date

在我的数据库中记录,在我的数据库工具中使用sql查询获取。(使用本机查询时不会出现第一条记录)

QTY     RETAIL_AMT          DISCOUNT_AMT        NET_AMT             COST_AMT            PROFIT              DATE
54.0    586.760003566742    27.769999504089355  558.9900002479553   313.270001411438    245.75999808311462  2018-05-22
95.0    399.7000057697296   16.479999780654907  383.2200062274933   212.6899983882904   170.5800033658743   2018-05-23
64.0    609.2199983596802   25.700000166893005  583.5199975967407   369.6899971961975   213.89000129699707  2018-05-24
62.0    474.439998626709    60.47999978065491   413.95999908447266  257.2700011730194   156.75999996066093  2018-05-25
33.0    342.32999992370605  2.75                339.57999992370605  193.520001411438    146.08999752998352  2018-05-26
73.0    426.76999855041504  10.600000143051147  416.17000007629395  241.5299997329712   174.7099997550249   2018-05-27

0 个答案:

没有答案