What is the difference between ‘generate’ and ‘autocraete’ elements of an attribute in items.xml and why are they used?

The attribute elements 'generate' and 'autocreate' plays major role in the below two scenarios:
  • While we create subtypes and add attributes to the subtype.
  • While we add new attribute to an existing type directly.
Case 1: Creating a Subtype
autocreate -
The first definition of a type ( or subtype of a type ) should set the 'autocreate' flag to 'true'.
A 'true' value lets the hybris Commerce Suite create a new database entry for this type at initialization/update process.
e.g.
<itemtype code="MyProduct" autocreate="true" generate="true" extends="Product" jaloclass="org.training.jalo.MyProduct">

    <attributes>

        ...

    </attributes>

</itemtype>
 Here setting the ‘autocreate’ modifier to false will cause a build failure.
generate -
Setting the generate modifier to true will result in Java class files being generated for this type (additional details).
Setting the generate modifier to false will result in no Java class file being generated for this type.
Having no Java class file available will mean that we will not be able to implement custom business logic (such as getter, setter methods) for the type. And we will have to make use of the super type’s business logic implementation.
Case 2: Creating a Subtype
autocreate -
In this scenario as the type basically exists already, we need to set the 'autocreate' modifier for the type definition to 'false'.
e.g.
<itemtype code="Product" autocreate="false" generate="false">

    <attributes>

        <attribute qualifier="oldPrice" type="java.lang.Double" generate="true">

            <persistence type="property"/>

            <modifiers read="true" write="true" optional="true"/>

        </attribute>

    </attributes>

</itemtype>
Setting the autocreate modifier to true will result in a build failure.
generate - The value of the generate modifier is ignored in these scenario.

No comments:

Post a Comment