Hybris - ItemType of Relation concept - sap-commerce-cloud

Lately I have come across instances where people have itemtypes of a relation. Could someone please explain me the concept of this structure?
Also I have generated a relation, but am unable to find getters/setters for this. I believe, this is because no classes are generated for a relation.
For a specific project requirement I had to add a collection of relations to another Model, but could not find the setters and getters generated even for the collection, I think because the elementtype is a relation. I also checked the extensionmanager, but no getters and setters were generated there either.
Could someone please let me know if with such a structure I could getters and setters for collection of relations?
Thanks,
Farhan

First you should use Relation instead of Collection whenever it's possible. In hybris Collection stores the values as CSV in one field, hence it's limited by the max size of a field and it may be truncated.
Then for Relation, you won't have a Java model generated, but only a specific DB table.
The getter and setter will actually be generated in the target and source model depending on your cardinality.
If we take an example - EmailMessage2ToAddressesRel
<relation code="EmailMessage2ToAddressesRel" .... >
.
.
.
<sourceElement type="EmailMessage" qualifier="toMessages"
cardinality="many" collectiontype="list" />
<targetElement type="EmailAddress" qualifier="toAddresses"
cardinality="many" collectiontype="list" />
</relation>
It map many toMessages as source to many toAddresses as target.
If you look at EmailMessageModel and EmailAddressModel you will find respectively the getter and setter for toAddresses and toMessages.

Related

Liferay 7 : Many-To-Many relationship in service.xml

I want to know if it is possible to have Many to many relationships in service.xml in liferay 7.1.
I have my first entity : Service, that is linked to one or many "Zone" entity. So is it possible to do this in service.xml or i must create a relationship table (but not optimized for search and filter) ?
Thanks for your answers
Regards

How to create one to many relationship using Liferay service builder

I need to define an one-to-many relationship between my custom entity and a Liferay default entity DDMFormInstance. So one MyCustomEntity could have many DDMFormInstance's. How can I do it?
you can define collection style columns; furthermore you will need a mapping-table between MyCustomEntity and DDMFormInstance and also remember that service-builder is not an ORM tool, primarily. so there will be a time when out-of-the-box functionalities will end.
<column entity="User" mapping-table="Users_Groups" name="users" type="Collection" />
see more in https://github.com/liferay/liferay-portal/blob/master/portal-impl/src/com/liferay/portal/service.xml
André's answer works by repurposing a many-to-many relationship as a one-to-many relationship. The challenge here is that DDMFormInstance is an out of the box Liferay entity, you cannot add a foreign key to it for the one-to-many relationship. André's answer is the easiest solution. You define a many-to-many relationship in the service.xml using mapping-table, then make it behave as one-to-many relationship in your MyCustomEntityLocalServiceImpl class.

How to order/sort my collections / relationships?

I've just started using Jhipster for a simple project with a very simple datamodel (so far).
I have a question regarding the generated code for the one-to-many relationship:
Is it possible to generate with List instead of a Set, so I can have my child-items ordered?
If no, what is the best solution to solve my problem? I see 2 ways:
Change the generated code manually to use a List and then use liquibase (mvn liquibase:diff) to update my database ?
Have an attribute on the child-item to handle the order ?
What is the best way to handle the "problem" ?
Best regards
Martin Elkkjær
You can use the Spring #OrderBy annotation to sort your sets by the child entity. See http://www.objectdb.com/api/java/jpa/OrderBy
#Entity
public class Person {
...
#OrderBy("zipcode.zip, zipcode.plusFour")
public Set<Address> getResidences() {...};
...
}
I'd also recommend the following blog that explains how Sets/Lists differ for Hibernate and JPA: https://vladmihalcea.com/hibernate-facts-favoring-sets-vs-bags/ (where I found the answer originally)

How to create references to different model XMI's in EMF

I have an EMF diagram that contains 2 classes. Person and Group. I am trying to persist these 2 classes into 2 different XMI files. It has to look a bit like this
Group.xmi
<XMI>
<Group>
<Person id="1"/>
...
...
</Group>
...
</XMI>
Person.xmi
<XMI>
<Person id="1">
<Name>
</Person
<XMI>
I know that it is possible... but I can't recreate it... I can only get XMI with the Persons embedded in the Group.
Any tips/hints/tutorials... on this?
Found the answer.
First you need to make sure that every instance has it's own unique ID... this can be accomplished by setting the ID in the constructor with EcoreUtil:
setId(EcoreUtil.generateUUID());
After that you need to make sure that the person is first created in the XMI file and then save the Group. Just saving the group without saving the person first will result in containing your person in this group and this group only.

XSD with Includes to EMF model

I have some quite big model, described in a number of xsd schemas. Schemas are referencing each other by includes. For example, A schema contains a 'someEnum' enumeration. B schema want to use that enum, so it include's the A schema. The problem is, when i import my XSD to eCore model's EMF are not only generates such an enum twice, but it aslo generates all the elements in B schema in model for A schema. I'm feeling that i'm doing something wrong, but can't figure out what exactly. Any help are very appreciated!
If you did select one xsd at a time in the EMF Project Wizard, then you should start with the xsd, that doesn't depend on anything. After the import of the first xsd finished, the EMF Project Explorer offers you to select referenced models during the import of the next xsd. Then you have to select the appropriate ones.
You are also able to import multiple xsds at once with the EMF Project Wizard. Then the references should be set correctly, if the namespaces of the xsds refer to each other.

Resources