Best Entity Inheritance Approach

Hi. I have some questions regarding extending entities. I see this is a great feature, but still not sure what’s best approach.

Suppose I have “inventory” module. Here I have InventoryItem, InventoryCategory. I use it as app components. The InventoryItem has property of category (InventoryCategory).
Now suppose if I have another module “purchasing”. And I want to have PurchaseItem, and PurchaseCategory, which extend two previous entities. Now, does it automatically call PurchaseCategory whenever I get purchaseItem.getCategory()? And what best approach should I use on this?

What best approach I should use?


You should decide whether you need just inheritance in your data model, or you need entity extension which is available in Studio when you select the Replace parent checkbox on the entity page. These things are similar technically, at least on the data model level - in both cases you create subclasses, and in the latter case you just add the @Extends annotation. But in the case of extension the original entities don’t exist in the application anymore - they are completely replaced by the extended ones. This feature is usually used when you need to modify data model provided by an application component. A typical use case would be adding an attribute to the CUBA sec$User entity: you don’t need original sec$User anymore and work only with your app$User containing all attributes of sec$User plus ones added by you. And what is even more important, all component’s mechanisms will also work with your entity although they don’t recognize added attributes. You can also extend component’s screens and thus modify component’s functionality.

In your case, if PurchaseItem and PurchaseCategory entities “extend” InventoryItem and InventoryCategory, you will not be able create instances of InventoryItem and InventoryCategory anymore. And of course purchaseItem.getCategory() will always return PurchaseCategory.

1 Like