使用FlexibleSearchService在数据库表中进行搜索。

huangapple go评论69阅读模式
英文:

Search in database table using FlexibleSearchService Hybris

问题

以下是要翻译的内容:

我尝试创建一个需要在数据库表中搜索项目的DAO。我在Hybris方面相当新,所以我不太清楚出了什么问题(查询还是我的代码有问题)。以下是在cmd中出现的错误信息:

SEVERE:在路径为 [] 的上下文中为servlet [DispatcherServlet] 提供服务时抛出异常 [请求处理失败;嵌套异常是 de.hybris.platform.servicelayer.search.exceptions.FlexibleSearchException:SQL搜索错误 - 启用属性 'flexible.search.exception.show.query.details' 以获取更多详细信息],根本原因如下
java.sql.SQLException:getLong() 的值无效

这是我的DAO类实现:

@Component(value = "arbRedirectHttpTypeDao")
public class ArbRedirectHttpTypeDaoImpl implements ArbRedirectHttpTypeDao {

    private static final Logger LOG = Logger.getLogger(ArbRedirectHttpTypeDaoImpl.class);

    @Autowired
    private FlexibleSearchService flexibleSearchService;

    public FlexibleSearchService getFlexibleSearchService() {
        return flexibleSearchService;
    }

    public void setFlexibleSearchService(FlexibleSearchService flexibleSearchService) {
        this.flexibleSearchService = flexibleSearchService;
    }

    @Override
    public ArbRedirectHttpTypeModel findNewUrlByOldUrl(String oldUrl) {

        final String query = "SELECT {" + ArbRedirectHttpTypeModel.NEWURL + "}"
                + " FROM {" + ArbRedirectHttpTypeModel._TYPECODE + "} WHERE {"
                + ArbRedirectHttpTypeModel.OLDURL + "}=?oldUrl";

        final FlexibleSearchQuery flexibleSearchQuery = new FlexibleSearchQuery(query);

        flexibleSearchQuery.addQueryParameter("oldUrl", oldUrl);

        final List<ArbRedirectHttpTypeModel> locationsByCode = flexibleSearchService
                .<ArbRedirectHttpTypeModel> search(flexibleSearchQuery)
                .getResult();

        LOG.info("-------------------------------------" + locationsByCode.get(0));
        if (locationsByCode != null && !locationsByCode.isEmpty()) {
            return locationsByCode.get(0);
        } else {
            return null;
        }

    }
}

这里是我尝试调用它的部分:

@Resource
private ArbRedirectHttpTypeDao arbRedirectHttpTypeDao;
ArbRedirectHttpTypeModel arbRedirectHttpTypeModel = arbRedirectHttpTypeDao.findNewUrlByOldUrl("/Aapuvdc");
英文:

I try to make a DAO what needs to search for an item in the database table. I'm pretty new in Hybris so I don't know what is wrong exactly here (query or my junk code). That is the error that appears in cmd.
> SEVERE: Servlet.service() for servlet [DispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is de.hybris.platform.servicelayer.search.exceptions.FlexibleSearchException: SQL search error - enable the property 'flexible.search.exception.show.query.details' for more details] with root cause
java.sql.SQLException: Invalid value for getLong()

That is my DAO class implementation

@Component(value = &quot;arbRedirectHttpTypeDao&quot;)
public class ArbRedirectHttpTypeDaoImpl implements ArbRedirectHttpTypeDao {

private static final Logger LOG = Logger.getLogger(ArbRedirectHttpTypeDaoImpl.class);

@Autowired
private FlexibleSearchService flexibleSearchService;

public FlexibleSearchService getFlexibleSearchService() {
    return flexibleSearchService;
}

public void setFlexibleSearchService(FlexibleSearchService flexibleSearchService) {
    this.flexibleSearchService = flexibleSearchService;
}

@Override
public ArbRedirectHttpTypeModel findNewUrlByOldUrl(String oldUrl) {

    final String query = &quot;SELECT {&quot;+ ArbRedirectHttpTypeModel.NEWURL +&quot;}&quot;
            + &quot; FROM {&quot;+ ArbRedirectHttpTypeModel._TYPECODE +&quot;} WHERE {&quot;
            + ArbRedirectHttpTypeModel.OLDURL +&quot;}=?oldUrl&quot;;

    final FlexibleSearchQuery flexibleSearchQuery = new FlexibleSearchQuery(query);

    flexibleSearchQuery.addQueryParameter(&quot;oldUrl&quot;, oldUrl);

    final List&lt;ArbRedirectHttpTypeModel&gt; locationsByCode = flexibleSearchService
            .&lt;ArbRedirectHttpTypeModel&gt; search(flexibleSearchQuery)
            .getResult();

    LOG.info(&quot;-------------------------------------&quot; + locationsByCode.get(0));
    if (locationsByCode != null &amp;&amp; !locationsByCode.isEmpty())
    {
        return locationsByCode.get(0);
    }
    else
    {
        return null;
    }

}
}

And here I try to call it

 @Resource
 private ArbRedirectHttpTypeDao arbRedirectHttpTypeDao;
ArbRedirectHttpTypeModel arbRedirectHttpTypeModel = arbRedirectHttpTypeDao.findNewUrlByOldUrl(&quot;/Aapuvdc&quot;);

答案1

得分: 1

"SELECT {" + ArbRedirectHttpTypeModel.NEWURL + "}"

在这里您正在尝试返回模型的 `NEWURL`,我假设它将是一个字符串默认情况下灵活搜索查询会返回项目的主键并将其默认转换为 hybris 模型您可以尝试重新组织查询以选择主键

"SELECT {" + ArbRedirectHttpTypeModel.PK + "}"

或者您可以保持查询不变并使用 `FlexibleSearchQuery.setResultClassList(classList);` 设置查询的返回类型例如

flexibleSearchQuery.setResultClassList(Collections.singletonList(String.class));

您也不需要将 `.search()` 强制转换为 `ArbRedirectHttpTypeModel`。您可以使用以下方式

SearchResult<String> result = flexibleSearchService.search(flexibleSearchQuery).getResult();
return result.getCount() > 0 ? result.getResult().get(0) : null;
英文:
&quot;SELECT {&quot;+ ArbRedirectHttpTypeModel.NEWURL +&quot;}&quot;

In here you're trying to return the NEWURL of the model, which I assume will be a string. Flexible search query returns the pk from the item and casts it to hybris model by default. Instead try to reorganize your query to select the pk

&quot;SELECT {&quot;+ ArbRedirectHttpTypeModel.PK +&quot;}&quot;

Or you can leave the query as it is and set the return type of the query with FlexibleSearchQuery.setResultClassList(classList); such as:

flexibleSearchQuery.setResultClassList(Collections.singletonList(String.class));

You don't have to cast .search() to ArbRedirectHttpTypeModel either. You can just go with

SearchResult&lt;String&gt; result = flexibleSearchService.search(flexibleSearchQuery).getResult();
return result.getCount() &gt; 0 ? result.getResult().get(0) : null;

答案2

得分: 1

使用"SELECT {"+ ArbRedirectHttpTypeModel.PK +"}"。它将返回对象,您可以通过getter获取所有属性。

英文:

use "SELECT {"+ ArbRedirectHttpTypeModel.PK +"}". It will return object and you can get all attribute by getter

huangapple
  • 本文由 发表于 2020年10月15日 23:37:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64375071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定