I have this UML:
entity Profile {
creationDate Instant required
bio String maxlength(7500)
}
entity Grupo {
creationDate Instant required
groupname String minlength(2) maxlength(100) required
image ImageBlob
isActive Boolean
}
// RELATIONSHIPS:
relationship OneToOne {
Profile{grupo} to Grupo{profile}
}
relationship OneToMany {
User{grupo} to Grupo{user(id) required}
}
// DTO for all
dto * with mapstruct
// Set pagination options
paginate all with pagination
// Set service options to all except few
service all with serviceImpl
and when I run it with yo jhipster:import-jdl it gives the following error:
IllegalAssociationException: Relationships from User entity is not supported in the declaration between User and Grupo.
Error jhipster:import-jdl ./src/main/scripts/raro.jh
Is it NOT allowed to have a OneToMany relationship with the User Entity? The thing is that OneToOne relationships works fine.
Because when I change the relationship to ManyToOne, it works
entity Profile {
creationDate Instant required
bio String maxlength(7500)
}
entity Groups {
creationDate Instant required
groupname String minlength(2) maxlength(100) required
image ImageBlob
isActive Boolean
}
// RELATIONSHIPS:
relationship OneToOne {
Profile{groups(groupname)} to Groups{profile}
}
// relationship OneToMany {
// User{groups} to Groups{user(id) required}
// }
relationship ManyToOne {
Groups{user(id) required} to User{groups}
}
// DTO for all
dto * with mapstruct
// Set pagination options
paginate all with pagination
// Set service options to all except few
service all with serviceImpl
Why? Am I doing something worng in the first example?
Thanks
JHipster does not allow OneToMany relationships to the user by default. If you need this, you will have to manually change the code.
Please note that the User entity, which is handled by JHipster, is specific. You can do:
many-to-one relationships to this entity (a Car can have a many-to-one relationship to a User)
many-to-many and one-to-one relationships to the User entity, but the other entity must be the owner of the relationship (a Team can have a many-to-many relationship to User, but only the team can add/remove users, and a user cannot add/remove a team)
https://www.jhipster.tech/managing-relationships/
Related
how can I use a constructor on an Entity that has properties with #ManyToOne decorator and their types are from another Entity but only with the primary key.
For example:
#Entity()
class User {
constructor(
idUser: number,
idExtraData: number
){
this.id = idUser;
// this is going to give an error because it requires an instance of ExtraData.
// but I would like to pass only the id
// and have an instance similar when I use findOne without populating anything
this.extraData = idExtraData;
}
#PrimaryKey()
id: number;
#ManyToOne()
extraData: ExtraData;
}
The instance of the entity I want should be similar to the instance returned by findOne without populating anything.
You can use Reference.createNakedFromPK()
#Entity()
export class Book {
#ManyToOne(() => Author)
author: Author;
constructor(authorId: number) {
this.author = Reference.createNakedFromPK(Author, authorId);
}
}
This instance will be either replaced with the managed entity during flush if there is one, or it will be merged to the EM otherwise.
(the docs mention how to do this with reference wrapper only, but its the same approach)
https://mikro-orm.io/docs/entity-references#assigning-to-reference-properties
I've generated a ManyToMany relationship between two entities but I need to add a extra field in the join table
#ManyToMany
#JoinTable(name = "cliente_modulo",
joinColumns = #JoinColumn(name = "cliente_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "modulo_id", referencedColumnName = "id"))
private Set<Modulo> modulos = new HashSet<>();
In this way is added a new table in database but is not created a new entity.
How to generate a ManyToMany relationship that adds a entity with extra field?
Instead of a ManyToMany relation you can setup a OneToMany between Cliente and ClienteModulo and a ManyToOne between ClienteModulo and Modulo.
This should generate the join table as before but also the model entity and everything else you need.
Something like:
entity Cliente{}
entity ClienteModulo{}
entity Modulo{}
relationship OneToMany {
Cliente{clienteModulos} to ClienteModulo{cliente}
}
relationship ManyToOne {
ClienteModulo{modulo} to Modulo{clienteModulos}
}
Add the properties you need to ClienteModulo.
I want to create a "friend" relationship between User using an entity "Friend".
I try this, but it isn't work.
entity Friend {
status Boolean,
modified LocalDate,
created LocalDate
}
relationship ManyToMany {
Friend{user(login)} to User,
User to Friend{user(login)}
}
How i can do that ?
Thanks
You cannot create relationship with User entity in JDL
A workaround is to create another entity and use a one-to-one relationship like this
entity Friend {
status Boolean,
modified LocalDate,
created LocalDate
}
entity UserExtended {
...
}
relationship OneToOne {
UserExtended to User
}
relationship ManyToMany {
Friend{userExtended(login)} to UserExtended,
UserExtended to Friend{userExtended(login)}
}
You may want to consider creating the relationship with the User directly in the generated code.
Found it :
entity UserExtra {
.....
}
entity Friend{
status Boolean,
modified LocalDate,
created LocalDate
}
relationship OneToOne {
UserExtended{user(login)} to User
}
relationship OneToMany {
UserExtended{friends} to Friend{user}
}
relationship ManyToOne {
UserExtended{friend} to UserExtended{users}
}
Let's say that you create a JHipster app for a Blog with Posts using a JDL script like this and you want to have a BlogDTO that shows the Posts within it (and a BlogDTO that shows the Comments each Post has):
entity Blog {
creationDate Instant required
title String minlength(2) maxlength(100) required
}
entity Post {
creationDate Instant required
headline String minlength(2) maxlength(100) required
bodytext String minlength(2) maxlength(1000) required
image ImageBlob
}
entity Comment {
creationDate Instant required
commentText String minlength(2) maxlength(1000) required
}
// RELATIONSHIPS:
relationship OneToMany {
Blog to Post{blog required}
Post{comment} to Comment{post(headline) required}
}
// Set pagination options
paginate all with pagination
// DTOs for all
dto * with mapstruct
// Set service options to all except few
service all with serviceClass
// Filtering
filter *
Jhipster will create your Blog, Post and Comment entities with their DTOs and makes the assumption that you do not want to populate the Blog with the Posts or the Posts with the comments, so your BlogMapper will look like this:
#Mapper(componentModel = "spring", uses = {})
public interface BlogMapper extends EntityMapper<BlogDTO, Blog> {
#Mapping(target = "posts", ignore = true)
Blog toEntity(BlogDTO blogDTO);
default Blog fromId(Long id) {
if (id == null) {
return null;
}
Blog blog = new Blog();
blog.setId(id);
return blog;
}
}
with a BlogDTO like this:
public class BlogDTO implements Serializable {
private Long id;
#NotNull
private Instant creationDate;
#NotNull
#Size(min = 2, max = 100)
private String title;
//GETTERS, SETTERS, HASHCODE, EQUALS & TOSTRING
Can anybody help to modify the code so the BlogDTO will show the Posts (and the PostDTO will show the Comments). Thanks
PD: Because I changed the Annotation to include the PostMapper class
#Mapper(componentModel = "spring", uses = {PostMapper.class})
And the #Mapping(target = "posts", ignore = false) to FALSE but it does not work. The API example (Swagger) look fine, but then the PostDTO is null (even when the data is there).
Add a Set<PostDTO> posts; to your BlogDTO and a Set<CommentDTO> comments; to your PostDTO. Also add getters and setters for those fields in the DTO files. Then in your mappers, make sure that the BlogMapper uses PostMapper and that the PostMapper uses CommentMapper.
You may also need to configure the caching annotations on the posts field in Blog.java and the comments field in Post.java to fit your use-case. With NONSTRICT_READ_WRITE, there can be a delay in updating the cache, resulting in stale data returned by the API.
Let's say you have a Jhipster app that has a Profile and needs to register which Profiles follows other profiles with 2 attributes: one for the user that is following (user Profile) and another for the followed user (followed Profile). Something like:
entity Profile {
creationDate Instant required
}
entity Follows {
creationDate Instant
}
relationship OneToMany {
Profile{follows(user)} to Follows{profile(id)}
Profile{follows(followed)} to Follows{profile(id)}
}
The problem with this is that the Follows.java has 2 identical atributes even when the names are different follows(user) & follows(followed):
#ManyToOne
private Profile profile;
... instead of...
#ManyToOne
private Profile user;
#ManyToOne
private Profile followed;
Thanks.
You are using the same relationship name for both relationships, but each relationship name needs to be different or else the fields conflict.
relationship (OneToMany | ManyToOne | OneToOne | ManyToMany) {
<from entity>[{<relationship name>[(<display field>)]}] to <to entity>[{<relationship name>[(<display field>)]}]
}
JDL Relationship Declaration Docs
In the case of your JDL sample, I changed the display field from user/followed to id as those fields do not exist on the entity. The important change is that the relationship names are unique.
relationship OneToMany {
Profile{followed(id)} to Follows{followed(id)}
Profile{following(id)} to Follows{following(id)}
}
entity Profile {
creationDate Instant required
}
entity Follows {
creationDate Instant
}
relationship ManyToOne{
Follows{user} to Profile
Follows{followed} to Profile
}
Supposing you want relation between Rooms and Patient
You can have these entities
entity NmsPatient {
photo ImageBlob
}
entity NmsRoom {
code String required,
name String required
}
entity NmsPatientRoom {
begin ZonedDateTime,
end ZonedDateTime
}
And then
relationship ManyToOne {
NmsPatientRoom{patient} to NmsPatient,
NmsPatientRoom{room} to NmsRoom
}