0

Is there any way to override equals() and hashCode() methods for types from *-items.xml ? For example:

 <itemtype code="FaqParagraph" extends="GenericItem" autocreate="true" generate="true"
                  jaloclass="com.training.core.jalo.FaqParagraph">
            <deployment table="FaqParagraph" typecode="20004"/>
            <attributes>
                <attribute qualifier="code" type="java.lang.String">
                    <modifiers initial="true" write="false" optional="false"/>
                    <persistence type="property"/>
                </attribute>
            </attributes>
 </itemtype>

Is there any way to override the equals() and hashCode() for FaqParagraphModel ?

user1234SI.
  • 1,812
  • 1
  • 8
  • 22

1 Answers1

1

If your question is about generated **Model instances, there is no possibility to have a custom implementation for hashCode and equals. Please note that the "generated" implementation considers two instances as equals if they have the same PK and are in the same tenant.

Hamdi Douss
  • 1,033
  • 1
  • 8
  • 17
  • Yes, it was about the generated model instances. I know they're *equal* (please note the italic font) if they have the same PK, but I was wondering why a `Set` works correctly if the `equals()` and `hashCode()` aren't overridden. Thank you for your answer, maybe you can help me with this question too :) – user1234SI. Feb 10 '20 at 16:09
  • What do you exactly mean by `equals()` "works correctly"? Do you mean set elements are considered equal if they have the same `code` attribute? – Hamdi Douss Feb 10 '20 at 19:43
  • I mean that the `Set` collection in order to work correctly on objects, they need to override the `equals()` and `hashCode()` methods, but still, the generated model doesn't override them and the `Set` works as expected. – user1234SI. Feb 10 '20 at 20:40
  • In java, every instance of every class has an implementation of equals and hashcode. The basic implementation (defined in `Object` class) considers two objects equal if they are exactly the same (i.e: the two variables are assigned to the same object). For Hybris entities, the implementation is in AbstractItem, it makes equality over the PK. – Hamdi Douss Feb 10 '20 at 22:26
  • Oh, ok, I understand now. Thank you very much ! :) – user1234SI. Feb 11 '20 at 05:56