Update the Jhipster User entity - jhipster

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/

Related

What is the type should the create method of the repository return?

I am trying to implement the CQRS pattern.
Following the CQRS pattern, the Command should return nothing, as the void type in Java.
The create() method in the repository is the command in my opinion and it should return nothing following the pattern.
But I am struggling to build the function which needs to create and use the new entity.
For example, I have to build a function assign new child category. In this feature, the client can either send the parent category id or new child details to the server.
The parent category id is an easy case, but the problem is in the new child details case.
I have to create a new category base on the details first, then use that entity to assign the parent id to it, and finally save it. Currently, I am not able to get the newly created entity, because the create() method returns nothing.
And I can't create a new entity, assign the parent id and save it, because the entity can't be created without an id and the id is only assigned by the ORM I am using.
What should I do to solve this problem? Does the method create() of the repository return an entity other than void violate the CQRS pattern?
It depends on scenario & business needs. All the design patterns, approaches, etc. just give you an idea of possible solutions to common problems. There is no single good principle to follow that works for all the scenarios across different business problems. If after executing the command, each time you need to execute some logic based on the result - then return that result to avoid unnecessary calls. It all depends on the problem you try to solve & trade-offs you are ready to make.
For more info you can check this question.
Following the CQRS pattern, the Command should return nothing, as the void type in Java.
I don't know where this is coming from, it's been years that people believe this, but it is not true.
For entities creation, or you tell the storage the id for the new thing to store, or it creates it for you and you can get it (or the entire entity) as a return, it doesn't violate anything.
Returning an entity doesn't violate CQRS Pattern.
In your Command Model add this:
public record CreateCommand : IRequest<ModelVm>
Which ModelVm is the model you want to return.
And in your CommandHandler class add this code :
public class CreateCommandHandler : IRequestHandler<CreateCommand, ModelVm>
And instead of returning Unit.Value You should return ModelVm in CreateCommandHandler.

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.

Customizing users and roles using identity in asp.net mvc 5

I have sample project for identity customization using
Install-Package Microsoft.AspNet.Identity.Samples -pre
command. But, for this project I have a general ApplicationUser class representing all the users of my application. What if I have different categories of users. For example, I may have Teacher and Student entities and data representing both the entities will be different. How can I customize my application to store data for both the entities having all the features of ApplicationUser?
One way that I think is inheriting both the classes from ApplicationUser and then doing appropriate changes in IdentityConfig.csand defining Controllers for each of them. Is there any other efficient way of doing this?
What if I want to use the built-in authentication and authorization features but using database first workflow?
First, you want to know how to create "types" of users. The way you would do that is exactly how you expected: inherit from ApplicationUser. By default, this will result in a single "users" table with an additional Discriminator column. This column will store the class type that was persisted, i.e. "Teacher", "Student", or "ApplicationUser", and EF will utilize this information to new up the right class for each particular record.
One thing to note with this, though, is that you need to be aware of how UserManager works, namely that it's a generic class (UserManager<TUser>). The default AccountController implementation you have from the sample defines a UserManager property on the controller which is an instance of UserManager<ApplicationUser>. If you use this instance with something like Teacher, it will be upcast to ApplicationUser. In particular if you were to do something like UserManager.Create(teacher), it will actually save an ApplicationUser, instead (the Discriminator column's value will be "ApplicationUser", rather than "Teacher"). If you need to work with the derived user types, you'll need to create separate instances of UserManager<Teacher> and UserManager<Student> for that purpose.
Next, you want to know if you can use a "database first workflow". To answer that, we need to define exactly what that means. EF has what it calls "Database First" which employs EDMX to represent your database entities. This in particular is incompatible with Identity. However, despite the name, what EF calls "Code First", can work with an existing database just as well as create a new one. In other words, yes, you can use an existing database, if you prefer, but no you cannot use "Database First", in the EF-sense. For more information about using an existing database with Code First, see my post.

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