返回长值的查询的Spring IncorrectResultSizeDataAccessException?JAVA
我在现有JPA存储库中添加了一个新的查询方法,该方法从OrderItem实体中检索一个id作为Long类型,如下所示:返回长值的查询的Spring IncorrectResultSizeDataAccessException?JAVA,java,spring,jpa,spring-data,Java,Spring,Jpa,Spring Data,我在现有JPA存储库中添加了一个新的查询方法,该方法从OrderItem实体中检索一个id作为Long类型,如下所示: @Transactional("order_item") public interface OrderItemRepository extends PagingAndSortingRepository<OrderItem, Integer> { ...other queries /** * Retrieves the lates
@Transactional("order_item")
public interface OrderItemRepository extends PagingAndSortingRepository<OrderItem, Integer> {
...other queries
/**
* Retrieves the latest modification Id of order item entity
*
* @param orderItemId id
* @return modificationId | null if order item is not a print product
*/
@Query("select pri.modificationId from OrderItem as oi"
+ " join oi.physicalItems as phys"
+ " join phys.printItem as pri"
+ " where oi.id = :orderItemId"
+ " order by pri.modificationId desc")
Long findLatestmodificationIdForOrderItem(@Param("orderItemId") Integer orderItemId);
}
查询主要工作,但有时会因日志中的错误而中断:
org.springframework.dao.incorrectresultsizedaataaccessexception:结果返回多个元素;嵌套异常为javax.persistence.UnuniquereSultException:结果返回多个元素
在
com.sun.proxy.$Proxy993.findLatestModificationIdForOrderItemUnknown源
在咨询了这个问题之后:
该解决方案是将方法返回类型更改为列表,但htis将违反我的约定,在很多地方需要重构
如何限制查询始终返回一个值而不中断
谢谢请尝试以下示例:
@Transactional("order_item")
public interface OrderItemRepository extends PagingAndSortingRepository<OrderItem, Integer> {
@Query("select pri.modificationId from OrderItem as oi"
+ " join oi.physicalItems as phys"
+ " join phys.printItem as pri"
+ " where oi.id = :orderItemId"
+ " order by pri.modificationId desc")
List<Long> findLatestmodificationIdForOrderItemQuery(@Param("orderItemId") Integer orderItemId);
default Long findLatestmodificationIdForOrderItem(Integer orderItemId) {
List<Long> result = this.findLatestmodificationIdForOrderItemQuery(orderItemId);
return result.get(0);
}
}
我会留下询问和合同。更改接口的实现以返回按时间排序的列表,并首先或最后返回列表中最新的列表。