我在初始页面请求(第0页)上工作的本机查询(SQL Server)上使用分页,但尝试使用无效列名时失败获得第二页的结果。生成的查询在SSMS中起作用,但在Spring中不起作用。Spring数据JPA本机查询分页不适用于第二页
任何洞察力是赞赏。
以下是错误:
Hibernate:
/* dynamic native SQL query */ WITH query AS (SELECT
inner_query.*,
ROW_NUMBER() OVER (
ORDER BY
CURRENT_TIMESTAMP) as __hibernate_row_nr__
FROM
(SELECT
TOP(?) p.ProductKey as page0_,
p.ProductName as page1_,
p.Sku as page2_,
p.MaterialKey as page3_,
p.MfgItemNumber as page4_,
p.PackageSize as page5_,
p.PriceUom as page6_,
p.CasePack as page7_,
p.ProductDescription as page8_,
P.MediaDomain as page9_,
p.ImagePath as page10_,
p.ImageName as page11_,
plp.Price as page12_,
plp.ListPrice as page13_,
p.SearchText as page14_
FROM
[dbo].[BdmProductCategory] pc
INNER JOIN
[dbo].BdmProduct p
on pc.ProductKey = p.productKey
INNER JOIN
[dbo].BdmPriceListProduct plp
on plp.ProductKey = p.ProductKey
WHERE
pc.CategoryKey = ?
AND pc.ProductKey > 0
AND pc."Delete" = 0
AND plp.PriceListKey = ? -- #pageable
order by
sku asc) inner_query) SELECT
page0_,
page1_,
page2_,
page3_,
page4_,
page5_,
page6_,
page7_,
page8_,
page9_,
page10_,
page11_,
page12_,
page13_,
page14_
FROM
query
WHERE
__hibernate_row_nr__ >= ?
AND __hibernate_row_nr__ < ?
2017-08-18 08:47:45.844 TRACE 19316 --- [nio-6193-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [BIGINT] - [4143]
2017-08-18 08:47:45.845 TRACE 19316 --- [nio-6193-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [BIGINT] - [657]
2017-08-18 08:47:45.980 WARN 19316 --- [nio-6193-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: S1093
2017-08-18 08:47:45.980 ERROR 19316 --- [nio-6193-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : The column name productKey is not valid.
2017-08-18 08:47:45.990 ERROR 19316 --- [nio-6193-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query] with root cause com.microsoft.sqlserver.jdbc.SQLServerException: The column name productKey is not valid.
这里是模型:
@Entity
@Table(name = "vw_BdmProductInfo") // A view!
public class BdmProductInfo {
@Id
private Long productKey;
private Long materialKey;
private String sku;
private BigDecimal price;
private BigDecimal listPrice;
private String priceUom;
private String casePack;
private String packageSize;
private String mfgItemNumber;
private String productName;
private String productDescription;
private String mediaDomain;
private String ImagePath;
private String imageName;
private String searchText;
protected BdmProductInfo() {}
....
这里是库:
@RestResource(path="findProductInfoByCategory")
@Query(value="SELECT p.ProductKey, p.ProductName, p.Sku, p.MaterialKey, p.MfgItemNumber, p.PackageSize, p.PriceUom, p.CasePack, p.ProductDescription, P.MediaDomain, p.ImagePath, p.ImageName, plp.Price, plp.ListPrice, p.SearchText "
+ "FROM [dbo].[BdmProductCategory] pc "
+ "INNER JOIN [dbo].BdmProduct p on pc.ProductKey = p.productKey "
+ "INNER JOIN [dbo].BdmPriceListProduct plp on plp.ProductKey = p.ProductKey "
+ "WHERE pc.CategoryKey = :categoryKey "
+ "AND pc.ProductKey > 0 "
+ "AND pc.\"Delete\" = 0 "
+ "AND plp.PriceListKey = :priceListKey \n-- #pageable\n ",
countQuery="SELECT Count(*) "
+ "FROM [dbo].[BdmProductCategory] pc "
+ "INNER JOIN [dbo].BdmProduct p on pc.ProductKey = p.productKey "
+ "INNER JOIN [dbo].BdmPriceListProduct plp on plp.ProductKey = p.ProductKey "
+ "WHERE pc.categoryKey = :categoryKey "
+ "AND pc.ProductKey > 0 "
+ "AND pc.\"Delete\" = 0 "
+ "AND plp.PriceListKey = :priceListKey ",nativeQuery=true)
Page<BdmProductInfo> findProductInfoByCategory(@Param("categoryKey") Long categoryKey, @Param("priceListKey") Long priceListKey, Pageable pageable);
看来列productKey不存在?!你可以在这里检查日志:无法执行查询]的根本原因com.microsoft.sqlserver.jdbc.SQLServerException:列名productKey无效。你确定第一个电话是成功的吗?! –
是的,第一次调用成功......但查询与第二次请求不同。第一个查询Hibernate使用Top函数,第二个使用WITH QUERY,row_nbr等....如下所示。 – Don
WITH查询AS(SELECT inner_query。*, ROW_NUMBER()OVER( ORDER BY CURRENT_TIMESTAMP )作为__hibernate_row_nr__ FROM (SELECT TOP (?)p.ProductKey作为page0_, p.ProductName如page1_, p.Sku如page2_, p.MaterialKey如page3_, p.MfgItemNumber如page4_, 第PackageSize as page5_, p.PriceUom as page6_, p.CasePack as page7_, p.ProductDescription as page8_, P.MediaDomain如page9_, p.ImagePath如page10_, – Don