Creating entity relationship using a jdl in jhipster

Viewed 79

I have the below JDL that I am using to create the jhipster application.

entity AuthClient(auth_client) {
    msisdn String required maxlength(255),
    email String required unique maxlength(255),
    password String required maxlength(255),
    lastLogin Instant,
    createdAt Instant required,
    createdBy Integer,
    updatedAt Instant,
    updatedBy Integer,
    isDeleted Boolean required,
    deletedAt Instant,
    deletedBy Integer
}

entity AuthToken(auth_token) {
    token String required maxlength(255),
    appId Integer required,
    appVersionName String maxlength(255),
    clientId Integer required,
}
entity ClientProfile(client_profile) {  
    fName String required maxlength(255),
    mName String maxlength(255),
    lName String required maxlength(255),
    gender Integer,
    clientId Integer required
}


// Relations
relationship OneToMany {
AuthClient{AuthToken(clientId)} to AuthToken{AuthClient}
}
relationship OneToOne{
    ClientProfile{AuthClient} to AuthClient{ClientProfile(clientId)},
}

// Options
service * with serviceClass
paginate * with pagination
dto * with mapstruct
filter *

However, instead of using the variable clientId as the foreign key it creates another field in the database. image showing created fields in the database

I need to use the clientId as the foreign key in this application and not the generated new field Auth Client

1 Answers

Defining a clientId field as you did has no impact on the relationship, these are two separate things.

When you define a relationship, a field is automatically created in the entity and its type is of the related class: it's a reference to an object in the java entity and a foreign key column in the database table.

By default, the foreign key column will be named as auth_client_id and the field will be named as authClient. However, the JDL syntax for relationships lets you configure the relationship name and so modify the generated names.

So, remove all the clientId fields and modify your relationships definitions as follows:

// Relations
relationship OneToMany {
    AuthClient to AuthToken{client}
}
relationship OneToOne{
    ClientProfile{client} to AuthClient
}

This way you get the foreign key column named as client_id and the field named as client.

You can then define also which field of the related entity will be used for display in the generated UI.

There is more to learn about relationships in the doc: https://www.jhipster.tech/jdl/relationships

Related