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

Sub*_*tra 0 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)
Run Code Online (Sandbox Code Playgroud)

我的理解是它正在发生,因为它正在尝试INSERT,如果它可以执行INSERT_UPDATE那么问题就可以解决。我不想启用传统模式,因此请为我提供一个可以INSERT_UPDATE通过ModelService.save()方法完成的解决方案。

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

Joh*_*lte 6

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

创建一个新项目

ProductModel newProduct = modelService.create(ProductModel.class);
Run Code Online (Sandbox Code Playgroud)

写入对项目的更改

modelService.save(product);
Run Code Online (Sandbox Code Playgroud)

删除项目

modelSerivce.remove(product);
Run Code Online (Sandbox Code Playgroud)

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

modelService.refresh(product);
Run Code Online (Sandbox Code Playgroud)

从数据库中检索数据

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

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

ProductModel product = productService.getProductForCode("myEAN");
product.setMyCustomAttribute("ABC");
modelService.save(product);
Run Code Online (Sandbox Code Playgroud)

在没有 hybris 准备服务的情况下编辑自定义 itemtype/itemtype 当 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();
Run Code Online (Sandbox Code Playgroud)

通用搜索服务

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();
Run Code Online (Sandbox Code Playgroud)

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

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