I am currently developing an application using JHipster which generates code in Spring boot, Hibernate and JPA. I use the JDL studio to create my entities. When I defined the database tables several questions came up.
Here is an example of my origin entities:
Example:
entity Customers{
number Long required,
searchKey String,
companyName String required,
companyDepartment String,
taxesID Integer,
accountNumber Long,
streetAndNumber String,
postcode Integer,
city String,
country String,
contactFirstName String,
contactLastName String,
email String,
phoneNumber String,
createdDate ZonedDateTime,
updatedDate ZonedDateTime
}
entity Drivers{
number Long required,
searchKey String,
firstName String,
lastName String,
email String,
phoneNumber String,
streetAndNumber String,
postcode Integer,
city String,
country String,
createdDate ZonedDateTime,
updatedDate ZonedDateTime
}
entity Invoice{
number Long required,
valutaDate LocalDate required,
price BigDecimal required,
discountPercentage Float,
discount BigDecimal,
locked Boolean,
cancellation Boolean,
createdDate LocalDate,
updatedDate ZonedDateTime
}
1. I noticed that the entities have a lot of overlap. I thought okay, this can also be designed with separate tables and thus gain more clarity. But I am quite unsure if this is really a better variant in terms of efficiency or clarity for frontend developers.
Here are my entities after seperating:
entity Customers{
number Long required,
searchKey String,
companyName String required,
companyDepartment String,
taxesID Integer,
accountNumber Long,
createdDate ZonedDateTime,
updatedDate ZonedDateTime
}
entity Drivers{
number Long required,
searchKey String,
createdDate ZonedDateTime,
updatedDate ZonedDateTime
}
entity Invoices{
number Long required,
valutaDate LocalDate required,
nettoPrice BigDecimal required,
discountPercentage Float,
discount BigDecimal,
locked Boolean,
cancellation Boolean,
createdDate LocalDate,
updatedDate ZonedDateTime
}
entity Persons{
salutation Salutation,
title String,
firstName String,
lastName String,
phoneNumber1 String,
phoneNumber1Note String,
phoneNumber2 String,
phoneNumber2Note String,
phoneNumber3 String,
phoneNumber3Note String,
email String pattern(/^[^@\s]+@[^@\s]+\.[^@\s]+$/),
email2 String,
}
entity Addresses{
streetAndNumber String,
postcode Integer,
city String,
country String,
note TextBlob
}
enum Salutation{
Mr,
Mrs
}
relationship OneToOne{
Customers{address(streetAndNumber)} to Addresses,
Customers{contactPerson(lastName)} to Persons,
Drivers{address(streetAndNumber)} to Addresses,
Drivers{person(lastName)} to Persons,
}
relationship OneToMany{
Customers{invoices} to Invoices{customer(companyName) required},
}
Now the tables are clearly separated which gives a certain overview. But I would have to adjust the cascade type everywhere. For example, within the customers domain the CascadeType.ALL
so that an address is created directly when a customer is created.
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(unique = true)
private Addresses address;
Is it absolutely common or what are the best practices? Are there better ways?
2. As you can see above I have added the attributes createdDate
and updatedDate
. Do you have to do this nowadays or is there a way to create them automatically? As productive database I use MySQL.
3. The Customers
entity generates the following endpoint:
http://localhost:8080/api/customers/{customer_id}
It has an OneToMany
relationship with Invoices. So a customer can have multiples invoices but an invoice can have only 1 customer.
Response on GET request:
{
"id": 3,
"number": 18156,
"searchKey": "purple overriding",
"companyName": "SCSI New Jersey",
"companyDepartment": "Credit Card Account",
"taxesID": 63002,
"accountNumber": 13292,
"createdDate": "2021-02-23T01:08:01+01:00",
"updatedDate": "2021-02-23T02:27:31+01:00",
"address": null,
"contactPerson": null,
"invoices": null
}
JHipster also generates fake data. For example, Invoices. But why are no Invoices assigned to a Customer?
I have chosen Angular as the frontend. Here, as defined above (see jdl definition), the company name is displayed for the assigned customer. But when I look at the customers entity in the UI, I don't see any way to display the invoices of a customer. I thought by Customers{invoices} to Invoices{customer(companyName) required}
I would create a bidirectional relationship. Is there a way to display the list also in the UI? ?
4. Of course I would also like to output all invoices of a certain customer. With which relationship could I generate the following API endpoint? I am n
http://localhost:8080/api/customers/{customer_id}/invoices/{invoice_id}
I'm not sure but if the resources are also interrelated in the api, is it already called HATEOAS? like in this tutorial. Is there a way to generate HATEOAS support?
5. I also miss the option to update an entity like customers.
e.g. (PUT) http://localhost:8080/api/customers/{customer_id}
with the body of the changed Customer. Is there a way to generate the PUT controllers as well?