Hybris enumtype, maptypes and relation

Enum Type in hyrbis :

We use In enum in hybris to define a set of Constant value like OrderStatus ,PaymentStatus,CreditCardType etc. These are a kind of constant value which will be use in the hybris application without any change.

For Example : 
   
 <enumtype code="DeliveryStatus" autocreate="true" generate="true" dynamic="true">
        <value code="NOTSHIPPED"/>
        <value code="PARTSHIPPED"/>
        <value code="SHIPPED"/>
    </enumtype>

    <enumtype code="ExportStatus" autocreate="true" generate="true">
        <value code="NOTEXPORTED"/>
        <value code="EXPORTED"/>
    </enumtype>

Note : There are two ways to define an enum in Hybris: static and dynamic. Please don’t be confused between dynamic attributes in attribute declarations (inside itemtype) and dynamic declarations in enumtypes both are different.

1. static enumtype :
 
If we want to declare static enum then we need to use dynamic="flase" or we can escape this because by default it's value is false. After build and system update when the enumtype will genrate then at runtime (during serverup from HMC/Backoffice) we can not add any new value in that enum.Because a real java enum will be genrate and that can not be change runtime.

Example :

<enumtype code="ExportStatus" autocreate="true" generate="true">
        <value code="NOTEXPORTED"/>
        <value code="EXPORTED"/>
    </enumtype>
2. daynamic enumtype : 

If we want to declare static enum then we need to use dynamic="true" because by default it's value is false.After build and system update a hybris enumin which we can add value at runtime.

Example :

<enumtype code="StockLevelStatus" generate="true" autocreate="true" dynamic="true">
			<description>Flag for real stock level status</description>
			<value code="inStock" />
			<value code="outOfStock" />
		</enumtype>

Map Type in hyrbis :

We know that a maptype is a collection of key-value pairs. In hybris we can see in many item declarations who use this to store-language-specific localized values.

Syntex of maptype in hybris to define a map :

<maptypes>
		<maptype code="SolrSynonymLangMapping" argumenttype="Language" returntype="SolrSynonymConfigCollection"   autocreate="true" generate="true"/>
	</maptypes>
Note : Here argumenttype==>Key and returntype==>Value , code => The unique code of the map.
Define a map and inject an item type for the required attribute:(OOTB Example)

Define a map :
<maptypes>
   <maptype code="IndexedTypeAdditionalParametersMapping" argumenttype="java.lang.String" returntype="java.lang.String" />
</maptypes>

Inject in itemtype : 

	<itemtype code="SolrIndexedType" jaloclass="de.hybris.platform.solrfacetsearch.jalo.config.SolrIndexedType"
			extends="GenericItem" autocreate="true" generate="true">
			<deployment table="SolrIndexedType" typecode="2207" />
			<attributes>
				<attribute qualifier="identifier" type="java.lang.String">
					<description>name of the Indexed Type</description>
					<modifiers initial="true" read="true" write="false" optional="false" search="true" unique="true" />
					<persistence type="property" />
				</attribute>		
				<attribute qualifier="additionalParameters" type="IndexedTypeAdditionalParametersMapping">
					<persistence type="property" />
					<description>additional parameters of indexed type</description>
				</attribute>
			</attributes>
		</itemtype>

Relations in hyrbis items.xml :

In hybris Relation are used to maintain the m:n relation between 2 tables.

Note : SAP Hybris doesn’t allow a one-one Relation. If you want to achieve this kind of relation, then just define the object as an attribute in the required model.Hybris support One to Many, Many to One and Many to many relation.

  • One to Many/Many to One :
<relation code="MediaContainer2MediaRel" localized="false" generate="true" autocreate="true">
            <sourceElement type="MediaContainer" qualifier="mediaContainer" cardinality="one">
                <modifiers read="true" write="true" search="true" optional="true"/>
            </sourceElement>
            <targetElement type="Media" qualifier="medias" cardinality="many">
                <modifiers read="true" write="true" search="true" optional="true"/>
            </targetElement>
        </relation>

In the example we can see that one to many relation likewise we can create many to one for that just need to interchange cardinality value in sourceElement and targetElement.Here mean was to MediaContainer will hold many Media and second side Media will keep the only one MediaContainer model .

After build we can find the new entry will be done in both model as below :

  • Many to Many :
	<relation code="StoresForCMSSite" generate="true" localized="false" autocreate="true">
			<deployment table="Stores4Site" typecode="1092" />
			<sourceElement qualifier="cmsSites" type="BaseSite" cardinality="many" />
			<targetElement qualifier="stores" type="BaseStore" cardinality="many" collectiontype="list" ordered="true" />
		</relation>

In the example we can see that many to many relation .What this means is that BaseSite can have many BaseStore and BaseStore can have many BaseSite.

Here we can see that <deployment table="Stores4Site" typecode="1092" /> is defined because in m:n relation a new table will be created that will hold the both table defined qualifier(attribute).

We can also see in the above example that in targetElement aslo defining collectiontype="list" ordered="true" that means under BaseSite model when the BaseStore collection will created after build, that will be a list type and that will maintain the order.

After build we can find the new entry will be done in both model as below :

Leave a comment