【问题标题】:How to set Default Value for a Map attribute of an item in Items.xml Hybris?如何在 Items.xml Hybris 中为项目的 Map 属性设置默认值?
【发布时间】:2020-11-28 13:07:48
【问题描述】:

我需要用 map 的默认值初始化一个 item 的 maptyped 属性。

假设我们已经定义了一个地图类型

<maptype code="dummyMap" argumenttype="java.lang.String" returntype="java.lang.String" autocreate="true" generate="false" />

我们已经将 itemtype 声明为

<itemtype code="dummyItem" autocreate="true" ...>
  <attributes>
    <attribute qualifier="dummyAttribute" type="dummyMap">
        <defaultvalue>???</defaultvalue>  <<<<<========= How should we initialize ?????
    </attribute>
  </attributes>
</itemtype>

作为枚举类型属性的类似示例,我们将默认值定义为

<defaultvalue>em().getEnumerationValue("dummyEnum","dummyEnum_Value")</defaultvalue>

我们如何对 Maptyped 属性应用相同的属性。请让我知道如何使用地图值初始化属性。

【问题讨论】:

    标签: attributes default-value hybris items


    【解决方案1】:

    使用最新版本的 Hybris,您可以尝试传递 java.util.Collections.singletonMap,例如

    <defaultvalue>java.util.Collections.singletonMap("one", java.math.BigDecimal.ONE)</defaultvalue>
    

    当我用Hybris v1811(如下图)进行测试时,

    <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="items.xsd">
    
        <maptypes>
            <maptype code="DummyMap"
                     argumenttype="java.lang.String"
                     returntype="java.math.BigInteger"
                     autocreate="true"
                     generate="false"/>
        </maptypes>
        <itemtypes>
            <itemtype code="DummyItem" autocreate="true">
                <deployment table="DummyItem" typecode="30001" />
                <attributes>
                    <attribute qualifier="uname" type="java.lang.String">
                        <modifiers read="true" write="true" search="true" initial="true" optional="false"/>
                        <defaultvalue>"Hello"</defaultvalue>
                        <persistence type="property"></persistence>
                    </attribute>
                    <attribute qualifier="dummyAttribute" type="DummyMap">
                        <modifiers read="true" write="true" search="true" initial="true" optional="false"/>
                        <defaultvalue>java.util.Collections.singletonMap("one", java.math.BigDecimal.ONE)</defaultvalue>
                        <persistence type="property"></persistence>
                    </attribute>
                </attributes>
            </itemtype>
        </itemtypes>
    </items>
    

    后台DummyItemXML表示显示:

    <itemtype code="DummyItem" extends="GenericItem" jaloclass="org.training.jalo.DummyItem" generate="true" singleton="false" jaloonly="false" autocreate="true">
        <deployment table="dummyitem" typecode="30001"/>
        <attributes>
            <attribute generate="true" autocreate="true" qualifier="dummyAttribute" type="DummyMap"><!-- could not export defaultvalue '{one=1}' -->
    
                <persistence type="property" qualifier=""/>
                <modifiers read="true" write="true" search="true" encrypted="false" optional="false" removable="true" initial="true" unique="false" private="false" partof="false"/>
            </attribute>
            <attribute generate="true" autocreate="true" qualifier="uname" type="java.lang.String">
                <defaultvalue>
    new java.lang.String( "Hello" )
                </defaultvalue>
                <persistence type="property" qualifier=""/>
                <modifiers read="true" write="true" search="true" encrypted="false" optional="false" removable="true" initial="true" unique="false" private="false" partof="false"/>
            </attribute>
        </attributes>
    </itemtype>
    

    如您所见,它能够将new java.lang.String( "Hello" ) 作为属性uname 的默认值传递,但对于dummyAttribute 属性,它显示&lt;!-- could not export defaultvalue '{one=1}' --&gt;

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2011-02-17
      • 2013-05-21
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多