英文:
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 = "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;
}
}
}
And here I try to call it
@Resource
private ArbRedirectHttpTypeDao arbRedirectHttpTypeDao;
ArbRedirectHttpTypeModel arbRedirectHttpTypeModel = arbRedirectHttpTypeDao.findNewUrlByOldUrl("/Aapuvdc");
答案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;
英文:
"SELECT {"+ ArbRedirectHttpTypeModel.NEWURL +"}"
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
"SELECT {"+ ArbRedirectHttpTypeModel.PK +"}"
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<String> result = flexibleSearchService.search(flexibleSearchQuery).getResult();
return result.getCount() > 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论