Search code examples
one-to-manyenumerationsap-commerce-cloudrelation

How to make relation between enumtype and itemtype in Hybris


I have an enumtype :

<items>
 <enumtypes>
  <enumtype code="FuelEnumeration" generate="true" autocreate="true" dynamic="true"> 
   <value code="diesel"></value>
   <value code="gasoline"></value> 
   <value code="ethanol"></value>
 </enumtype> 
</enumtypes>

Here is the itemtype :

<itemtypes> 
<itemtype code="Car" extends="Product" autocreate="true" generate="true">
 <attributes> ... <attribute qualifier="fuel" type="FuelEnumeration"> 
<description>Fuel for this car</description> 
<persistence type="property"></persistence>
 </attribute>

I want to create a One to Many relation between enumtype and itemtype so that one car can have many FuelEnumeration, How can I do this?


Solution

  • If you really wanted to do one-to-many using relation, below is how you do it.

        <relation code="Car2FuelRel" generate="true" localized="false" autocreate="true">
            <sourceElement qualifier="car" type="Car" cardinality="one">
                <modifiers read="true" write="true" search="true" optional="true" />
            </sourceElement>
            <targetElement qualifier="fuel" type="FuelEnumeration" cardinality="many" collectiontype="set">
                <modifiers read="true" write="true" search="true" optional="true" />
            </targetElement>
        </relation>
    

    However, this is incorrect, because one-to-many means that a Car can have many fuel enum, but a fuel enum can belong to only one car. It's probably better to do to this as many-to-many. Just change the cardinality of sourceElement to many.

    Optionally, you can also use a collection as Raushan described. However, collection makes it difficult to search if a Car has a certain fuel value. Hybris recommends to use relations, instead of collections.

    SEE: