繁体   English   中英

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

[英]What Hybris Does when we do Modelservice.Save()?

当我使用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 准备服务的情况下编辑自定义 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();

通用搜索服务

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 帮助/维基页面。 你喜欢哪一种取决于你。 两者都有优点和缺点。

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

看起来,您正在尝试再次插入 UNIQUE 记录。

更新记录

  • 您必须首先从 db 中获取现有模型 obj(包含 pk)。 为此,您可以使用灵活的搜索, 通过示例获取模型等。
  • 现在更新你想要的属性
  • 保存modelService.save(obj)

插入新条目

  • 创建模型实例并设置值

    final MyModel obj = modelService.create(MyModel.class);

    obj.setId("unique");

  • 保存它modelService.save(product);

在 hybris 中,如果在 Item.xml 中定义属性时设置 UNIQUE ,则​​该值必须是 UNIQUE 否则在导入过程中会抛出错误

假设属性 userid 为项目定义了 UNIQUE=true,即

<attribute qualifier="userid" type="java.lang.String">
                        <description>user id of user.</description>
                        <modifiers unique="true"/>
                        <persistence type="property"/>
</attribute>

现在您通过 ImpEx 导入数据

INSERT_UPDATE EveryreplyUserAddress;userid[unique=true];street;state
;0001;delhi;delhi

如果您再次尝试使用与 0001 相同的用户 ID 进行 INSERT_UPDATE,则会抛出错误,因为在项目中您定义的用户 ID 是唯一的 重要:在导入期间 UNIQUENESS 由 UniqueAttributesInterceptor 检查。 但是您仍然可以通过将旧模式设置为 ON 来导入数据

因此,在拦截器的传统模式 ON 调用后将跳过然后值将插入到数据库中

*-Items.xml

<attribute qualifier="userid" type="java.lang.String">
                        <description>user id of user.</description>
                        <modifiers unique="true"/>
                        <persistence type="property"/>
</attribute>

通过使用 ModelService 如果您将保存项目然后再次拦截器将检查 UNIQUNESS

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM