Thursday, 12 May 2016

Understanding Hybris Data Models

Data Models are defined in the <extension>-items.xml file of each extension. Essentially data entities (called "items" in hybris) are defined with itemtype elements, whereas relations between items are defined with relation elements.

Below is the sample <extension>-items.xml.

            xsi:noNamespaceSchemaLocation="items.xsd">
    
    <itemtypes>
        <itemtype code="Stadium" generate="true" autocreate="true">
            <deployment table="CuppyTrailStadium" typecode="10123" />
            <attributes>
                <attribute qualifier="code" type="java.lang.String" >
                    <persistence type="property"/>
                    <modifiers optional="false" unique="true"/>
                </attribute>
                <attribute qualifier="capacity" type="java.lang.Integer">
                    <description>Capacity</description>
                    <persistence type="property" />
                </attribute>
            </attributes>
        </itemtype>
    </itemtypes>
</items>


itemtype code=Stadium generate=true autocreate=true
  • defines a new type Stadium which implicitly extends from GenericItem
  • autocreate=true indicates that Stadium is a new type
  • generate=true creates required sourcecode (not the model class) for the type

deployment table=CuppyTrailStadium typecode=10123 
  • specifies how the new type is mapped to the database
  • table defines the database table
  • typecode is used internally e.g. to create item pks. Typecodes between 0 and 10000 are reserved by hybris
attribute qualifier=code type=java.lang.String
  • creates a new attribute code by using the atomic type java.lang.String
persistence type=property
  • Defines how item are stored. property reflects the normal persistent behaviour

modifiers ...
  • advanced settings like read and write access

Relations

The below sample code shows the relation we define in xml


<relations>
    <relation code="StadiumMatchRelation" localized="false" generate="true" autocreate="true">
       <sourceElement type="Stadium" qualifier="stadium" cardinality="one" />
       <targetElement type="Match" qualifier="matches" cardinality="many"/>
    </relation>
</relations> 

  • Define a new relationship StadiumMatchRelation between Stadium and Match using the relation tag
  • Add the above element immediately  before the itemtypes element
  •  The relation is a one-to-many relation. which means that the mapping will be done by an additional column on the many side, i.e. the table Match

  Enumtypes

  The following element before the relations element in any <extension>-items.xml

<enumtypes>
        <enumtype code="StadiumType" autocreate="true" generate="true" dynamic="false">
            <value code="openair"/>
            <value code="enclosed"/>
        </enumtype>
        <enumtype code="StadiumAccess" autocreate="true" generate="true" dynamic="true">
            <value code="road"/>
            <value code="rail"/>
            <value code="plane"/>
        </enumtype>
</enumtypes>


We can also define the default values for an attribute


<attribute type="StadiumType" qualifier="StadiumType">

    <persistence type="property"/>
    <defaultvalue>em().getEnumerationValue("StadiumType","openair")</defaultvalue>
</attribute>

In Simple words if we related this with JAVA, then we can say that Type in Hybris is same as class in Java and Item in Hybris is same as Object in Java


No comments:

Post a Comment