cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Class Associations in generated Model classes

ombako
Explorer
0 Kudos
2,084

Given the following itemtype definition:

 <itemtype code="BaseSite" autocreate="false" generate="false">
     <description>Extending BaseSite type with additional attributes.</description>
     <attributes>
         <!-- Other properties omitted -->
         <attribute qualifier="defaultPromotionGroup" type="PromotionGroup">
             <description>The default promotion group for the site.</description>
             <persistence type="property" />
         </attribute>
     </attributes>
 </itemtype>

the file BaseSiteModel.java will be generated:

 public class BaseSiteModel extends ItemModel
 {
     public final static String _TYPECODE = "BaseSite";
     /* Other properties omitted */
     public static final String DEFAULTPROMOTIONGROUP = "defaultPromotionGroup";
 
     @Accessor(qualifier = "defaultPromotionGroup", type = Accessor.Type.GETTER)
     public PromotionGroupModel getDefaultPromotionGroup()
     {
         return getPersistenceContext().getPropertyValue(DEFAULTPROMOTIONGROUP);
     }
 }    

The generated class has no property of type PromotionGroupModel, only a static final String which will be used by the getter to return an instance of type PromotionGroupModel.

The question: Is there any way to have hybris generate a real association property like the following?

private PromotionGroupModel promotionGroup;

That way the generated java class would look like:

 public class BaseSiteModel extends ItemModel
 {
     public final static String _TYPECODE = "BaseSite";
     /* Other properties omitted */
      public static final String DEFAULTPROMOTIONGROUP = "defaultPromotionGroup";
      private PromotionGroupModel promotionGroup;
 
     @Accessor(qualifier = "defaultPromotionGroup", type = Accessor.Type.GETTER)
     public PromotionGroupModel getDefaultPromotionGroup()
     {
         return promotionGroup;
     }
 }

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member624549
Participant
0 Kudos

It once was the way you ask for! Having the a real object property also has the advantage to actually see the model fields in a debugger as how it is now you don't see the fields.

The hybris models are not real pojo. You see the members of the model only from the getter/setter pair. The real values are in a hashmap in the context object. But not all of them, only the ones you have accessed.

Why hybris changed from real object members to a proprietary hash map one (or at least I) can only speculate.

ombako
Explorer
0 Kudos

Thanks for your comment. That's exactly the way I remember it and neither can I understand why the changed it. Well, it's hybris, you know...

geffchang
Active Contributor
0 Kudos

You can customize model generation, but what you are looking for is not supported.

Only the following are supported:

  • exclude an entire type (including all subtypes) from the generation

  • exclude an attribute from the Model generation process

  • specify a constructor to be generated

  • add alternative getter and setter methods for an attribute

I assume it's designed this way because it needs to get the values through the persistence context.

Reference: Models

ombako
Explorer
0 Kudos

Thank you for your answer. I can't help but only sigh in frustration that it's been implemented this way. It's thwarting the help the language syntax can offer.

Just for my own note, why not something like this?:

 @Accessor(qualifier = "defaultPromotionGroup", type = Accessor.Type.GETTER)
      public PromotionGroupModel getDefaultPromotionGroup()
      {
          if (promotionGroup == null) {
              promotionGroup = getPersistenceContext().getPropertyValue(DEFAULTPROMOTIONGROUP);
          }
          return promotionGroup;
      }
 
geffchang
Active Contributor
0 Kudos

We just have to get used to the way Hybris platform works. Anyway, if you are using Ee, you can always see the return type of the getters in the Outline view. 🙂

Also, if you think this answer has been somewhat helpful, feel free to Upvote and/or Accept the Answer. :)