Skip to Content
2
Former Member
Jun 23, 2015 at 04:07 PM

Multiple many-to-many relationships with the same linked items

410 Views

I've come up against an issue with the definition of Link in core-items.xml: the source, target, and language are marked as unique, but not the qualifier.

What this means in practice is that it's impossible to link the same two models through two different many-to-many relationships; in this case the qualifier will differ but the three unique attributes will not.

I've tried to redeclare the qualifier attribute to make it unique, but with no success.

At the moment it's looking like I'll have to override the TypeService#getUniqueAttributes method to add qualifier to the returned set when Link is the argument -- this seems kinda hacky, are there any better suggestions?


Background

I'm trying to create a second relationship between BaseStore and DeliveryMode, which is going to be a set of delivery modes that are inapplicable for pre-orders. My relation looks like this:

 <relation code="BaseStore2NonApplicableDeliveryModesForPreOrders" localized="false" autocreate="true" generate="true">
     <description>Links stores with the delivery modes that are not applicable for pre-orders</description>

     <deployment table="Store2NAppDlvrMd4PreOdr" typecode="10400" />
 
     <sourceElement type="BaseStore" qualifier="nonApplicableForPreOrdersInStores" cardinality="many" collectiontype="set" ordered="false">
         <description>The stores in which the delivery mode is not applicable for pre-orders</description>
     </sourceElement>

     <targetElement type="DeliveryMode" qualifier="nonApplicableDeliveryModesForPreOrders" cardinality="many" collectiontype="set" ordered="false">
         <description>The delivery modes that are not applicable for pre- and back-orders in the store</description>
     </targetElement>
 </relation>

The problem is, when I try to create instances of this relation, the UniqueAttributesInterceptor throws an exception:

 ambiguous unique keys {target=ZoneDeliveryModeModel (8796093120552), source=BaseStoreModel (8796093056989), language=null}

The qualifier needs to be unique too for this to work.


What I tried

I've tried to redeclare the qualifier attribute on the Link type in my items.xml, but I get an IllegalArgumentException from YNamespace.mergeNamespace:

 cannot merge namespace ((mycore)) into ((<merged>)) due to duplicate attribute 'Link.qualifier(java.lang.String):((mycore))::YAttributeDescriptor[mycore-items.xml:1726(AttributeTagListener)][CMP]' vs 'Link.qualifier(java.lang.String):((core))::YAttributeDescriptor[core-items.xml:1331(AttributeTagListener)][CMP]'

This is the xml I tried:

 <itemtype code="Link" autocreate="false" generate="false">
     <attributes>
          <attribute qualifier="qualifier" type="java.lang.String" autocreate="true" redeclare="true">
             <description>
                 hybris sets this field to the name of the many-to-many relation for which the link was created.
             </description>
             <persistence type="cmp" qualifier="qualifierInternal"/>
             <modifiers read="true" write="true" search="true" optional="true" unique="true"/>
         </attribute>
     </attributes>
 </itemtype>