通过代码在我的动态枚举中添加新值会出错,

时间:2019-01-09 15:37:04

标签: hybris

我有一个动态的枚举ManufacturerName,我必须添加一个新值(一个新的制造商)。我试过了,但是抛出错误。

 public void createNewManufacturerEnum(final String code, final String name)
{
    try
    {
        final HybrisEnumValue manufacturer =ManufacturerName.valueOf(code);     
        enumerationService.setEnumerationName(manufacturer, name, Locale.GERMAN);
        enumerationService.setEnumerationName(manufacturer, name, Locale.ENGLISH);
...

错误是:

Caused by: de.hybris.platform.servicelayer.exceptions.ModelSavingException: [com.cancom.core.manufacturer.CCManufNameInterceptor@907afc5]: unexpected validator error: missing persistent item for enum value 0000505008
    at de.hybris.platform.servicelayer.internal.model.impl.wrapper.ModelWrapper.invokeValidateInterceptors(ModelWrapper.java:311) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.wrapper.ModelWrapper.validate(ModelWrapper.java:239) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.extractor.impl.DefaultModelExtractor.process(DefaultModelExtractor.java:58) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.performPersistenceOperations(DefaultModelService.java:711) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.saveAllInternal(DefaultModelService.java:706) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.saveAll(DefaultModelService.java:686) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.saveAll(DefaultModelService.java:648) ~[coreserver.jar:?]
    at com.hybris.backoffice.cockpitng.dataaccess.facades.object.DefaultPlatformObjectFacadeStrategy.save(DefaultPlatformObjectFacadeStrategy.java:137) ~[classes/:?]
    ... 92 more
Caused by: java.lang.IllegalStateException: missing persistent item for enum value 0000505008
    at de.hybris.platform.servicelayer.internal.converter.impl.EnumValueModelConverter.getSource(EnumValueModelConverter.java:154) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.getSource(DefaultModelService.java:431) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.type.impl.DefaultTypeService.getEnumerationValue(DefaultTypeService.java:330) ~[coreserver.jar:?]
    at de.hybris.platform.enumeration.impl.DefaultEnumerationService.setEnumerationName(DefaultEnumerationService.java:180) ~[coreserver.jar:?]
    at com.cancom.core.manufacturer.dao.impl.CCManufNameDaoImpl.createNewManufacturerEnum(CCManufNameDaoImpl.java:73) ~[classes/:?]
    at com.cancom.core.manufacturer.services.impl.CCManufNameServiceImpl.createNewEnum(CCManufNameServiceImpl.java:37) ~[classes/:?]
    at com.cancom.core.manufacturer.CCManufNameInterceptor.onValidate(CCManufNameInterceptor.java:66) ~[classes/:?]
    at com.cancom.core.manufacturer.CCManufNameInterceptor.onValidate(CCManufNameInterceptor.java:1) ~[classes/:?]
    at de.hybris.platform.servicelayer.internal.model.impl.wrapper.ModelWrapper.invokeValidateInterceptors(ModelWrapper.java:291) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.wrapper.ModelWrapper.validate(ModelWrapper.java:239) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.extractor.impl.DefaultModelExtractor.process(DefaultModelExtractor.java:58) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.performPersistenceOperations(DefaultModelService.java:711) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.saveAllInternal(DefaultModelService.java:706) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.saveAll(DefaultModelService.java:686) ~[coreserver.jar:?]
    at de.hybris.platform.servicelayer.internal.model.impl.DefaultModelService.saveAll(DefaultModelService.java:648) ~[coreserver.jar:?]
    at com.hybris.backoffice.cockpitng.dataaccess.facades.object.DefaultPlatformObjectFacadeStrategy.save(DefaultPlatformObjectFacadeStrategy.java:137) ~[classes/:?]
    ... 92 more

我做错了什么?谢谢!

4 个答案:

答案 0 :(得分:1)

我尝试了这个,看来它可行。

try
    {
        final HybrisEnumValue manufacturer = ManufacturerName.valueOf(code);
        modelService.save(manufacturer);
        enumerationService.setEnumerationName(manufacturer, name, Locale.GERMAN);
        enumerationService.setEnumerationName(manufacturer, name, Locale.ENGLISH);
    }

答案 1 :(得分:1)

为OOTB动态枚举de.hybris.platform.basecommerce.enums.StockLevelStatus提供的示例。

final EnumerationValueModel newStockLevelStatus = modelService.create(StockLevelStatus._TYPECODE);
newStockLevelStatus.setCode("OVERRUN");
newStockLevelStatus.setName("Overrun");
modelService.save(newStockLevelStatus);

答案 2 :(得分:1)

您找到了解决方案。这是杂种的例子。请注意,调用setEnumerationName后无需保存。它已经包含对modelService.save的调用。

这是来自EnumerationServiceDemoTest中的SAP hybris的测试中的复制/粘贴

private EnumerationValueModel testEVModel;
//Create the new enum value
testEVModel = modelService.create(OrderStatus.CREATED.getType());
modelService.save(testEVModel);

//find the enumeration for given type and code
final HybrisEnumValue matchingEnumeration = enumerationService.getEnumerationValue([your enumerationCode],
            [your valueCode]);

//setEnumerationName for the given Enum
enumerationService.setEnumerationName(matchingEnumeration, "testName");

//get the name and check if correct
String enumName = enumerationService.getEnumerationName(matchingEnumeration);
assertEquals("testName", enumName);

//setEnumerationName for the given Enum again with german language and check it
enumerationService.setEnumerationName(matchingEnumeration, "neuer_name", Locale.GERMAN);

enumName = enumerationService.getEnumerationName(matchingEnumeration, Locale.GERMAN);
assertEquals("neuer_name", enumName);

答案 3 :(得分:0)

尝试在激活“更新正在运行的系统”选项的情况下运行更新。