How to create one to many relationship using Liferay service builder - liferay

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.

Related

How to extend JHipster JDL

We would like to extend JDL to represent some additional GUI aspects in the generated frontend. E.g.:
show additional attributes from joined entities in the table
show custom defined attribute instead of entity ID in dropdowns
show detail list in parent entity form
etc.
Is there any way to extend JDL and use it in some custom blueprint? Or should we use some different approach?
In relation to the second item, there is a feature in the JDL relationship definition which helps:
relationship (OneToMany | ManyToOne | OneToOne | ManyToMany) {
<from entity>[{<relationship name>[(<display field>)]}] to <to entity>[{<relationship name>[(<display field>)]}]+
}
If you define one "display field" for the relationship end, JH will generate a drop-down using that field instead of the "id".
In relation to the third item, I am also interested in that feature. So, please let us know if you find a solution.

Update the Jhipster User entity

I use jhipster and I would like to modify the User entity and add fields and relationships.
I use jhipster entity user and this command is not good.
How can I do it?
User is not a JHipster entity, the generator does not manage it. You must edit the code manually or add a related entity where you put additional fields, see doc: https://www.jhipster.tech/tips/022_tip_registering_user_with_additional_information.html
If you encounter a problem where you need to alter the User entity, Its recommend not doing that. Modifying this default entity might break your app depending on the nature of the changes.
Instead, there are other available solutions like:
creating an entity composed of the User entity
extending the User entity
Using composition
by using OneToOne relation like this
entity ApplicationUser {
additionalField Integer min(42) max(42)
}
relationship OneToOne {
ApplicationUser{internalUser} to User
}
Or
Using inheritance
This solution does the same thing as the previous one, but isn’t as straightforward as the first one because you need to:
create a new entity by hand,
adapt the code to make it use this new entity,
potentially manage yourself the database migration to persist this new entity (depending on the nature of the changes).
More info: https://www.jhipster.tech/user-entity/

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

Hybris - ItemType of Relation concept

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.

In JHipster how to create entity with relationship with User?

I need to create my own entities using the JHipster using the command "yo jhipster:entity myEntity" that have many-to-one relationship with the User entity that comes by default in JHipster.
I have tried unsuccessfully to create in the wizard a relationship with the entity "user" and the field "login" but it is not working.
What is the good way to do this with JHipster? Or do I have to create the entity without JHipster tool (but I need the CRUD!).
Thanks,
Yann
Just an update - jhipster 2.5.0 was released a few days ago adding support for this. Created this answer since the formatting in comments make i pretty hard to read.
When creating a relation for your entity simply answer the questions like this
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? user
? What is the name of the relationship? owner
? What is the type of the relationship? many-to-one
? When you display this relationship with AngularJS, which field from 'user' do you want to use? login
Possible values on how to display the relation could be: id, login, first_name, last_name, email
See https://jhipster.github.io/2015/03/01/jhipster-release-2.5.0.html for moreinformation
Just to add to the correct answer by #stoffer, if you're using the jdl it will look like:
relationship ManyToOne {
Owner{user(email)} to User{owner(name)}
}
at a good sample in an official document is say :
entity Blog {
name String required minlength(3),
handle String required minlength(2)
}
relationship ManyToOne {
Blog{user(login)} to User
}
jdl-samples/blog.jh
If you are using the 1.x version, this wasn't made to work, so basically you should do it by hand, without the generator (but it is definitely doable).
For the 2.x version, we have refactored the User object to have an ID field -> this should make this a lot easier, but as this is not released yet, we don't have a feedback at the moment.
I have come into this issue and solved it by using a simple one-to-one relationship first that will extend the user entity, because it is simple and manageable easily and can make the extended entity be the owner of the relationship, then you can create the many-to-one relationship with your entity, like this example:
entity MyEntity {
MyField Type
...
}
entity ApplicationUser {
additionalField Type
...
}
relationship OneToOne {
ApplicationUser{internalUser} to User
}
relationship ManyToOne {
MyEntity{appUser} to ApplicationUser{myEntities}
}
For more details and approach refer to this jHipster page.

Resources