【问题标题】:From List to int. Unable to convert a query result从 List 到 int。无法转换查询结果
【发布时间】:2015-11-12 16:51:35
【问题描述】:

我有这个问题:

select count(*) from {Order as or join CustomerOrderStatus as os on
{or:CustomerOrderStatus}={os:pk} join OrderEntry as oe on
{or.pk}={oe.order} join PurchaseAmount as pa on
{or.pointOfSale}={pa.purchaseAmountOwner} join PurchaseAmountTimeSlice
as ts on {pa.pk}={ts.purchaseamount}} where {or:company} in
(8796093710341) and {or:pointOfSale} in (8796097413125)

我在 Java 中有这段代码来检索结果:

FlexibleSearchQuery query = new FlexibleSearchQuery(queryBuilder.toString());
List<Integer> result = new ArrayList<Integer>();
result = getFlexibleSearchService().<Integer> search(query).getResult();

我想从结果列表中获取计数的 int 值。 如果我写result.get(0),我会收到错误IllegalArgumentException

java.lang.IllegalArgumentException: invalid pks [4] - unknown typecode 0
    at de.hybris.platform.core.WrapperFactory.getCachedItems(WrapperFactory.java:304)
    at de.hybris.platform.core.LazyLoadItemList.loadPage(LazyLoadItemList.java:230)
    at de.hybris.platform.servicelayer.search.impl.LazyLoadModelList.loadPage(LazyLoadModelList.java:60)
    at de.hybris.platform.core.LazyLoadItemList.switchPage(LazyLoadItemList.java:219)
    at de.hybris.platform.core.LazyLoadItemList.switchBufferedPageNoLock(LazyLoadItemList.java:475)
    at de.hybris.platform.core.LazyLoadItemList.switchBufferedPageSynchronized(LazyLoadItemList.java:467)
    at de.hybris.platform.core.LazyLoadItemList.switchBufferedPage(LazyLoadItemList.java:462)
    at de.hybris.platform.core.LazyLoadItemList.getOrSwitchBufferedPage(LazyLoadItemList.java:453)
    at de.hybris.platform.core.LazyLoadItemList.getOrSwitchBufferedPage(LazyLoadItemList.java:433)
    at de.hybris.platform.core.LazyLoadItemList.getBuffered(LazyLoadItemList.java:111)
    at de.hybris.platform.core.LazyLoadItemList.get(LazyLoadItemList.java:97)
    at java.util.AbstractList$Itr.next(AbstractList.java:358)
    at de.hybris.platform.core.internal.BaseLazyLoadItemList$1.next(BaseLazyLoadItemList.java:180)
    at java.util.AbstractCollection.toArray(AbstractCollection.java:195)
    at java.util.Collections$UnmodifiableCollection.toArray(Collections.java:1059)

如何获取 int 值?

【问题讨论】:

  • 当您询问异常时,请始终发布异常的堆栈跟踪。

标签: java mysql arraylist hybris


【解决方案1】:

count(*)替换为count({or:Pk}),并将灵活搜索查询的结果类设置为Integer这样query.setResultClassList(Collections.singletonList(Integer.class));

【讨论】:

    【解决方案2】:

    我是这样解决的:

    private FlexibleSearchService flexibleSearchService;
    
        @Override
        public void onValidate(PurchaseAmountTimeSliceModel paramMODEL, InterceptorContext paramInterceptorContext)
            throws InterceptorException {
    
           //.....
           final StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.append("select {ts.pk}");
            queryBuilder.append(
                    " from {Order as or join CustomerOrderStatus as os on {or:CustomerOrderStatus}={os:pk} join OrderEntry as oe on {or.pk}={oe.order} ");
            queryBuilder.append(
                    "join PurchaseAmount as pa on {or.pointOfSale}={pa.purchaseAmountOwner} join PurchaseAmountTimeSlice as ts on {pa.pk}={ts.purchaseamount}} where ");
    
            if (pointOfSale != null && company != null) {
                queryBuilder.append("{or:company} in (" + company.getPk() + ") and {or:pointOfSale} in ("
                        + pointOfSale.getPk() + ")");
            }
            FlexibleSearchQuery query = new FlexibleSearchQuery(queryBuilder.toString());
            SearchResult<PurchaseAmountTimeSliceModel> result = flexibleSearchService.search(query);
    
            if (result != null) {
                if (result.getCount() >= 10) {
                    LOG.error("There's already 10 purchase amount time slices configured. You reached the limit.");
                    throw new InterceptorException(
                            "There's already 10 purchase amount time slices configured. You reached the limit.");
                }
            }
        //.....
    

    解决方案类似;)

    【讨论】:

    • " + company.getPk() + " 受到 SQL 注入攻击
    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多