java - 当我们执行 Modelservice.Save() 时,Hybris 会做什么?

标签 java sap-commerce-cloud

当我使用 ModelService.save() 保存模型时,它抛出

de.hybris.platform.servicelayer.interceptor.InterceptorException: [de.hybris.platform.servicelayer.interceptor.impl.UniqueAttributesInterceptor@555528e4]:ambiguous unique keys
        at de.hybris.platform.servicelayer.interceptor.impl.UniqueAttributesInterceptor.onValidate(UniqueAttributesInterceptor.java:158)

据我了解,发生这种情况是因为它正在尝试INSERT,如果它可以INSERT_UPDATE,那么问题就可以解决。我不想启用旧模式,因此请为我提供一个解决方案,让我可以通过 ModelService.save() 方法执行 INSERT_UPDATE 操作。

如果 ModelService.save() 正在执行 INSERT_UPDATE 那么为什么会出现错误。

最佳答案

hybris 中的 ModelService 实现了与您预期不同的功能。模型服务支持:

创建新项目

ProductModel newProduct = modelService.create(ProductModel.class);

将更改写入项目

modelService.save(product);

删除项目

modelSerivce.remove(product);

当不同上下文进行更改时刷新项目

modelService.refresh(product);
<小时/>

从数据库检索数据

当您想要更改现有项目时,您需要先从数据库中获取它。有多种机会检索现有项目。考虑以下情况:

检索现有产品、用户、类别... 对于大多数标准 hybris 项目,都有可检索的服务使用 ProductService、UserService、CategoryService...现在使用 ModelService 保存对该模型所做的更改。

ProductModel product = productService.getProductForCode("myEAN");
product.setMyCustomAttribute("ABC");
modelService.save(product);

在没有 hybris 准备服务的情况下编辑自定义项目类型/项目类型 当hybris不提供从数据库获取项目的服务时,您需要自己实现该服务。有很多机会可以这样做。

灵活搜索服务

Map<String, Object> params = new HashMap<>();
params.put("id", "123");
String query = "SELECT {pk} FROM {MyCustomType} WHERE {id} LIKE ?id";
SearchResult<MyCustomTypeModel> result = flexibleSearchService.search(query, params);
List<MyCustomTypeModel> myCustomTypesWithId = result.getResult();

通用搜索服务

GenericSearchField idField = new GenericSearchField(MyCustomTypeModel._TYPECODE, MyCustomTypeModel.ID);
GenericCondition condition = GenericCondition.createConditionForValueComparison(idField, Operator.EQUAL, "123");
GenericQuery query = new GenericQuery(MyCustomTypeModel._TYPECODE, condition);
SearchResult<MyCustomTypeModel> searchResult = genericSearchService.search(query);
List<MyCustomTypeModel> myCustomTypesWithId = searchResult.getResult();

这些只是最突出的。有关更多信息,请参阅 hybris 帮助/wiki 页面。您更喜欢哪一个取决于您。两者都有优点和缺点。

建议将此数据访问功能包装在自己的类中。在数据库中搜索项目的类称为 DAO(数据访问对象)。

关于java - 当我们执行 Modelservice.Save() 时,Hybris 会做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50501605/

相关文章:

sap-commerce-cloud - 杂交 : Mark products with no super-category as inactive

java - Hybris 后台 : Configuration for image in WYSIWYG doesn't work

java - 拆分字符串但仍显示所有元素

java - 不可变对象(immutable对象)的 Hibernate 组件映射

java - ValidationException : Call to TraversableResolver. isReachable() 抛出异常

java - 在 Hybris 中的 itemtype 中使用扩展

java - BufferedImage 比较显示奇怪的行为

java - JMS连接应该什么时候开始?在它自己的线程中?

sap-commerce-cloud - Smartedit 类型错误 : Cannot read property 'siteId' of undefined

mysql - 使用灵活的搜索查询选择随机行