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.
Related
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/
Let's say I have an entity Book
I want Jhipster to generate a Books Entity something like
class Books{
List<Book> books;
}
And Generate corresponding rest controllers like
Books saveAllBooks(Books books)
Books updateAllBooks(Books books)
There's no need to create a Books entity if it contains only a list of Book entities, you could just add manually the list methods to generated REST controllerBookResource.java as you don't need any additional repository.
But if Books has other fields that you want to persist in database, you could generate 2 entities with one-to-many relationship. I wouldn't recommend using plural as it could probably conflict with other generated code.
entity Book{
title String required
}
entity BookBatch{
name String required
}
// RELATIONSHIPS:
relationship ManyToOne {
Book to BookBatch
}
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 there any way to get the name of a relationship given the from and to entities?
I have a fairly dynamic piece of code that needs to Associate or Disassociate entities. In order to call these functions, I need to know the name of the relationship. I am not sure how to determine the relationship name just given the two entities.
For example, my code might need to disassociate an account with a contact. How can I determine the relationship name of "account_primary_contact"?
In the interest of answering the question despite my not understanding why you would want to do this:
foreach (OneToManyRelationshipMetadata relationshipMetaData in primaryEntityMetadata.ManyToOneRelationships)
{
if (relationshipMetaData.ReferencingEntity == relatedEntity.LogicalName)
{
string relationshipName = relationshipMetaData.SchemaName;
//Do something with your relationship?
}
}
Where primaryEntityMetadata is of type EntityMetadata and ReferencingEntity is of type Entity. Then use similar logic for checking Many to One relationships.
So I've got a Client entity that needs a relationship to a PhoneNumber entity to allow multiple phone numbers. And I've got an Employee entity that also needs a relationship to a PhoneNumber entity to allow multiple phone numbers. Should I create two separate PhoneNumber entities or can I somehow use the same entity for both?
I would create a parent entity called Person for your Client and Employee entities. The Person entity would have a relationship to the PhoneNumber entity.
Inherited entities have the same attributes and relationships as their parent entity. Of course you can add attributes and relationships to the "child"-entities as well. I omitted that in the screenshot.
Something like this:
you can configure the parent entity in the core data inspector in the right side pane.