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

时间:2018-05-24 05:27:59

标签: java sap hybris

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

de.hybris.platform.servicelayer.interceptor.InterceptorException:[de.hybris.platform.servicelayer.interceptor.impl.UniqueAttributesInterceptor@555528e4]:模糊的唯一键         at de.hybris.platform.servicelayer.interceptor.impl.UniqueAttributesInterceptor.onValidate(UniqueAttributesInterceptor.java:158)

我理解的是它正在发生,因为它正在尝试INSERT,如果它可以做 INSERT_UPDATE 那么问题就可以得到解决。我不想让传统模式开启,所以请为我提供一个解决方案,我可以INSERT_UPDATEModelService.save()方法。

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

3 个答案:

答案 0 :(得分:2)

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

制作新商品

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

将更改写入商品

modelService.save(product);

删除项目

modelSerivce.remove(product);

在通过其他上下文进行更改时刷新项目

modelService.refresh(product);

从数据库中检索数据

如果要更改现有项目,则需要先从数据库中获取该项目。检索现有项目有多种机会。请考虑以下情况:

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

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

编辑自定义itemtype / itemtype而不使用hybris准备服务 当hybris不提供从数据库获取项目的服务时,您需要自己实现该服务。有很多机会这样做。

<强> FlexibleSearchService

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();

<强> GenericSearchService

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(数据访问对象)。

答案 1 :(得分:2)

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

更新记录

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

要插入新条目

  • 创建模型实例并设置值

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

    obj.setId("unique");

  • 保存modelService.save(product);

答案 2 :(得分:0)

在hybris中,如果在Item.xml中定义属性时设置UNIQUE ,那么值必须是UNIQUE,否则在导入期间抛出错误

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

rnge.Cells(...)

现在您通过ImpEx导入数据

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

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

因此在制作传统模式后,Interceptor将调用SKIPP,然后将值插入数据库

  

* - Items.xml

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

通过使用ModelService,如果你将保存项目,那么拦截器将再次检查UNIQUNESS

相关问题