Add #ElementCollection of enum in JHipsterEntity - jhipster

I have an entity A to which I want to add a list of an enum type so I add in the JPA entitys code
#ElementCollection
#Enumerated(EnumType.STRING)
#Column(name = "tags")
private List<Tag> tags;
and run the liquibase:diff goal which according to my understanding should add necessary db and ui changes, but when I build and run my app the updates are not applied in db nor the UI.
How could I achieve this in a JHipster?
Thank you

Related

jhipster updating .jhipster/entities.json from java entities

When I need to update an entity in jhipster just run jhipster entity MyEntity and apply new changes as desired to both: the entity and the associated changelog. So far so good. But what if I want the inverse result: defining the new fields/relations in the entity class and propagate those changes through the changelog and the frontend entity?
In this case, for instance:
#Column(name = "name")
#NotNull
#Pattern(regexp = "[a-zA-Z0-9]")
private String name;
According to what I have read, If I already have the name field but I want to add the above validations I have to add them into the proper liquibase changelog first and then in my java entity? Is that the only way?
It would be great to use this workflow, however, by my understanding it is not possible.
A possible solution would be to first regenerate the entity using jhipster cli or any other jhipster method. Once this step is completed, you could edit the entity in Java, adding complex validations or even refining the entity's relationship.
After any entity modification you must update Liquibase's changelog. You could do it manually or you could run ./gradlew liquibaseDiffChangeLog to generate a changelog containing all changes applied over the database. Don't forget to apply the generated changelog to the main changelog (src/main/resources/liquibase/master.xml).
Cheers!

How to create #Transient properties in JHipster?

I was thinking about how to create a Proposal object like this in JHipster: so a User can create a Proposal and other users can vote for it.
entity Proposal {
proposalText String minlength(2) maxlength(100) required
proposalVotes Integer
}
entity Vote {
numberOfPoints Integer
}
relationship ManyToOne {
Vote{proposal(id) required} to Proposal{vote}
Proposal{user(id) required} to User{proposal}
Vote{user(id) required} to User{vote}
}
In Spring I would create that #Transient proposalVotes Integer and the Controller would go and find all the Votes that a Proposal has and add them together to get to the result to be sent to the frontend. That property would not be stored in the database.
If I use JHipster and I add a proposalVotes property, the result would be saved in the database and could be changed in the dialogs(... and I do not like the result), so my question is:
What is the best practice in JHipster when you need a property that is calculated everytime his object is called?
Think of the number of comments in a Blog with Posts, if it is more familiar.
Where do you calculate the result: I would do it in the ProposalResource, but I’m not sure and I haven’t seen any use case like this in the examples, but it looks like a common case.
Thanks a lot
PD: If there is any example in Github, that could be great!
Actually if you are generating entities using JDL(Jhipster domain language) then you wont get any option to make field Transient as JDL is database design mechanism and Transient fields are not going to be placed in DB.
Solution is that after importing JDL to our app you can add Transient fields in your entity class.

Is it possible to tell jdl or jhipster to use Integer as id type?

For now all newly created entities get Long id type. I would like to use Integer instead.
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Would like to have Integer here when I create a new entity.
No it's not possible, you must edit generated entities and Liquibase migrations. As this is a trivial change, we don't implement it, see policy #2 in http://www.jhipster.tech/policies/

Why authority is so complex?

I cannot understand why Authority is so complex in JHipster? What's the reason for creating another entity with only one field name? Furthermore it has a csv file that uploads a template to the database with Liquibase.
There was an idea for me to create an enum (see this answer):
#Entity
#Table
public enum Authority {
USER, ADMIN;
#Id
private String title = "ROLE_" + name();
}
Then I decided that this is not necessary too. It can be an enum without annotations and just a Set (or even an EnumSet):
private Set<Authority> authorities = new HashSet<>();
Did I miss something? Maybe it's useful for JPA?
I wanted to create an issue on GitHub but I think it's a better place.
GrantedAuthority is defined by Spring Security and default implementation SimpleGrantedAuthority has only a role name but it can be extended to have more attributes so I guess that JHipster's entity enables this possibility.

Designing DTO's and DO service layer

I'm designing a new large scale application which needs to be as flexible as possible.
I chose designing mainly with DDD..
My question is about transferring DTO object's back to DO objects in my service layer.
i.e:
This is my domain object mapped to the DB (using ORM)
public class Cat
{
int ID {get; set;}
string Name {get; set;}
string BloodType {get; set;}
string Color {get; set;}
void Run(){...}
void Purr() {...}
}
Methods and some properties are only needed for server actions.
That's why I designed another, data transfer object for this cat type:
public class CatDTO
{
int ID {get; set;}
string Name {get; set;}
}
In the middle, I'll set up an object mapper to translate my DO's to DTO's (and vice versa).
When a client would like to update a cat's name he will call a service like this
public void UpdateCat(CatDTO cat)
{
// What will happen here?
Cat serverCat = Mapper.GetCat(CatDTO);
CatDao.SaveOrUpdate(serverCat);
}
When the mapper is translating the DTO object back to DO it will have to hit the DB in order to fill the missing properties of the Cat object (blood type, etc')
Needles to say this action is absurd but without filling the empty properties the rest of the server side cannot work with the Cat object because it relies on those missing properties (even if i just try to update the data in the DB, My ORM will update the bloodtype field as an empty string!)
I searched for this problem and couldn't find any explenation on the web (or at least someone who is bothered with the issue as I do)
Am I designing it the wrong way? Maybe I missed something in my DDD?
Thanks, Pavel.
The usual workflow for this use case is: retrieve mapped domain object by ID, apply updates specified by the DTO, commit unit of work. What you refer to as the DAO is normally called a repository in DDD. The code should look more like:
public void UpdateCat(CatDTO catDto)
{
Cat cat = this.catRepository.Get(cat.ID);
cat.Name = catDto.Name;
this.catRepository.Commit();
}
The Commit step can come in a variety of ways. It can either be an explicit save, or the unit of work can be committed outside of the UpdateCat method. This workflow applies to all related scenarios as well. Generally, domain behavior involves retrieving the appropriate entity, invoking some behavior on that entity and then committing the resulting changes to the database.
Also, DTOs shouldn't directly map back into existing entities. Instead, it is better to think of them as representing changes to be applied to existing entities and the code should reflect this. This is in part because an existing entity is "owned" by the repository and the repository is responsible for reconstitution, not a DTO mapper.

Resources