JHIPSTER JDL: Confusion between join field and display field - jhipster

I'm confused with the JHipster documentation here
relationship (OneToMany | ManyToOne | OneToOne | ManyToMany) {
<from entity>[{<relationship name>[(<display field>)]}] to <to entity>[{<relationship name>[(<display field>)]}]+
}
<display field> is the name of the field that should show up in select
boxes (default: id),
But in another sample the same <display field> is used to join, nor dispay.
relationship OneToOne {
A{b(name)} to B{a(name)}
}
It roughly translates to SQL: JOIN B b with a.name = b.name
So if I have one bi-directional relationship with two entities with "id" and I need to display the name in both... ¿The join will be done by name and not by id?

Related

Hybris PCM product category restriction

We are implementing Hybris PCM and we would like to create some product category restrictions.
Some employees should find only the products with a specific category. For example, some employees can see only products with category "drills," while other employees can see only products with category "shoes".
How can we do this in Backoffice and PCM?
Search restrictions (personalization) can be used to achieve this:
INSERT_UPDATE SearchRestriction;code[unique=true];active;generate;restrictedType(code);principal(uid);query
;Product_restriction_1; true; false; Product; usergroup1; "{category} in ({{ select {pk} from {Category} where {code}='category1' }})"
;Product_restriction_2; true; false; Product; usergroup2; "{category} in ({{ select {pk} from {Category} where {code}='category1' }})"
Here employees belonging to usergroup1 can see products belonging to category1 and usergroup2 can see products belonging to category2 (these queries given above are just some dummy queries for quick understanding of concept).
Restriction type = product
Principal = user/ usergroup corresponding to the backoffice employee logging in
A more specific query to suit your requirement would be:
select {r.target} from { CategoryProductRelation as r join Category as c on {r.source}= {c.pk} } where {c.code}='shoes'
Hence please use an impex like:
INSERT_UPDATE SearchRestriction;code[unique=true];active;generate;restrictedType(code);principal(uid);query
;Product_restriction_shoes; true; false; Product; shoeUser; " {pk} in ({{select {r.target} from { CategoryProductRelation as r join Category as c on {r.source}= {c.pk} } where {c.code}='shoes' }}) "

Jhipster - setting up many to many relationship for user-entities

Want to create entity called Friendship and want to leverage the User entity that's part of Jhipster,
But I keep getting this invalid-relationship error (full error below).
User has friends (user entities) and vice-versa
entity UserExtended {
}
entity Friend{
status Boolean,
modified LocalDate,
created LocalDate
}
relationship OneToOne {
UserExtended{user(login)} to User
}
relationship OneToMany {
UserExtended{friends} to Friend{user}
}
relationship ManyToOne {
UserExtended{friend} to UserExtended{users}
}
entity Post {
owner UserExtended,
content String,
dateCreated LocalDate
}
entity Like {
likedBy UserExtended,
post Post,
dateCreated LocalDate
}
entity Comment {
postedBy UserExtended,
post Post,
dateCreated LocalDate
}
relationship OneToMany {
UserExtended{post} to Post{user}
}
relationship OneToMany {
Like{post} to Post{like}
}
relationship OneToMany {
Comment{post} to Post{comment}
}
Error:
Error: Can't add invalid relationship. Error: In the Many-to-One relationship from UserExtended to UserExtended, only unidirectionality is supported, you should either create a bidirectional One-to-Many relationship or remove the injected field in the destination entity instead.
Error while parsing applications and entities from the JDL Error: Can't add invalid relationship. Error: In the Many-to-One relationship from UserExtended to UserExtended, only unidirectionality is supported, you should either create a bidirectional One-to-Many relationship or remove the injected field in the destination entity instead.
Error: Can't add invalid relationship. Error: In the Many-to-One relationship from UserExtended to UserExtended, only unidirectionality is supported, you should either create a bidirectional One-to-Many relationship or remove the injected field in the destination entity instead.
You have several problems in your JDL. For example, you should not mix relationships and entities like this:
entity Post {
owner UserExtended, // <-- This is a problem
content String,
dateCreated LocalDate
}
If I have understood your requirements correctly you want to design a kind of blog and let users form friendships. JDL will not let you add relationships starting from the core entity User so you have created a UserExtended and will possibly store some extra information there.
Remember that you can design multiple relationships inside one relationship block. In fact I think it is a good practice, makes the whole JDL a bit more readable.
This should do what you need:
entity UserExtended
entity Friend {
status Boolean
modified LocalDate
created LocalDate
}
entity Post {
content String
dateCreated LocalDate
}
entity Like {
dateCreated LocalDate
}
entity Comment {
dateCreated LocalDate
}
relationship OneToOne {
UserExtended{user(login)} to User
}
relationship ManyToOne {
Post{owner} to UserExtended
Comment{postedBy} to UserExtended
Like{likedBy} to UserExtended
Friend{user} to UserExtended
}
relationship OneToMany {
UserExtended{friends} to Friend
Post{likes} to Like
Post{comments} to Comment
}
The only tricky part here is the many-to-many between two users in a relationship called Friend. You need to store some extra information about the friendship (status, modified, created) so we had to split this many-to-many into a one-to-many plus a many-to-one using the Friend entity as a join table with extra fields.
I did not change your naming scheme, which could probably be improved.
Remember to check the official documentation and optionally use JHipster Online for JDL storage and validation.
Try renaming
relationship OneToOne {
UserExtended{user(login)} to User
}
to
relationship OneToOne {
UserExtended{somethingElse(login)} to User
}

Sequelize hasOne through another table

I have the following three models
User
user_id
user_email
Group
group_id
group_name
GroupUser
group_user_id
user_id
group_id
How can I get group details(if it mapped with user ) while fetch User data ?
Is there any Sequelize hasOne association through another table ?
There is currently no direct way in sequelize for what you are asking
ref https://github.com/sequelize/sequelize/issues/3845
but you can do
Group.belongsTo(GroupUser)
User.belongsTo(GroupUser)
GroupUser.hasOne(Group)
GroupUser.hasOne(User)
and do nested include (but this is not very efficient)
User.findAll({include : [{model:GroupUser, include :[{model : Group}]}]})

proper way to reference documents with Mongoose

I see in all examples the suffix "_id" on a field referencing to another document.
Example:
record: {
_id : ObjectId("57f2fb5d1c6c3b0de45b170e",
artist_id: "prince" )
}
artist: {
_id: "prince"
}
Being that my artist mongo Schema has the "unique" attribute on the name field.
Is it Ok to things like below ?
record: {
_id : ObjectId("57f2fb5d1c6c3b0de45b170e",
artist: "prince" )
}
artist: {
_id : ObjectId(6eygdqzd5d1c6c3b0de45b1s0r",
name: "prince"
}
Or should you always reference directly the Id like in the first example?
if you visualize your problem in RDBMS world, there too to establish a foreign key constraint the field should be primary key in the referenced table and the same rule applies here.
now in your artist document though each document is going to contain a unique artist name but the name field itself is not key (primary key) but the ID is.
hence you have to establish the reference using the _id field.
what you can do is for ease if you want rather than relying on the mongodb generated ID field you can probably use name as the _id.

How to create an entity's relationship to itself? For example, hierarchical folders

I'm trying to create a hierarchical folder structure. Here's my Folder entity:
$ yo jhipster:entity Folder
The entity Folder is being created.
Generating field #1
? Do you want to add a field to your entity? Yes
? What is the name of your field? name
? What is the type of your field? String
? Do you want to add validation rules to your field? No
=================Folder=================
name (String)
Generating field #2
? Do you want to add a field to your entity? Yes
? What is the name of your field? parentId
? What is the type of your field? Long
? Do you want to add validation rules to your field? No
=================Folder=================
name (String)
parentId (Long)
Generating field #3
? Do you want to add a field to your entity? No
=================Folder=================
name (String)
parentId (Long)
I'm trying to map out just what I need to provide jhipster's entity generator to make it work. This is what I have so far...
Generating relationships with other entities
? Do you want to add a relationship to another entity? Yes
? What is the name of the other entity? Folder
? What is the name of the relationship? parent
? What is the type of the relationship? one-to-many
? What is the name of this relationship in the other entity? child
Am I on the right track? How do I create the child many-to-one relationship? I get a warning if I try to create it with the Folder entity. There's no way to generate it afterwards.
You can use https://jhipster.github.io/jdl-studio/ for writing as jdl for creating entities.
Please visit https://jhipster.github.io/jhipster-uml/#jdl for more information.
This is a sample JDL that has relationship to itself :
entity RankProperties {
rank Integer required,
minExp Integer required,
maxExp Integer required,
maxStamina Integer required,
maxAlly Integer required,
maxTeam Integer required
}
enum TaskMode {
NO_CONTINUE_STAGE_COUNT,
STAGE_COUNT,
STAGE_ID
}
entity Task {
taskMode TaskMode required,
value Integer required
}
relationship ManyToOne {
Task{parent} to Task
}
dto all with mapstruct
service all with serviceClass
I recommend to use jdl model.
entity A { property1 String }
relationship OneToMany {
A{sons} to A{parent}
}
The model generates a Entity java class like (some annotations ignored):
class A {
#OneToMany(mappedBy="parent")
private Set<A> sons = new HashSet<>();
#ManyToOne
private A parent;
}

Resources