Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing User class - creating entity relationship to User entity in Microservice

Tags:

jhipster

I want to create an entity that has a Many to One relationship with JHipster's User entity.

I've used JDL-Studio to create the following entity and relationship to User which is imported into my Microservice as a .jh file using jhipster import-jdl:

entity Profile {
    name String required
    bio String
    location String
    photo ImageBlob
}

relationship ManyToOne {
    Profile{user} to User
}

angularSuffix * with mySuffix

dto * with mapstruct

Upon compiling my Micro Service I get the following errors:

Profile.java:44: error: cannot find symbol private User user; symbol: class User location: class Profile

ProfileMapper.java:12: error: cannot find symbol @Mapper(componentModel = "spring", uses = {UserMapper.class, }) symbol: class UserMapper

ProfileMapper.java:12: error: Couldn't retrieve @Mapper annotation @Mapper(componentModel = "spring", uses = {UserMapper.class, })

Lines 43-44 of Profile.java are:

@ManyToOne
private User user;

and ProfileMapper.java is as follows:

package com.moogrisoft.openseas.service.mapper;

import com.moogrisoft.openseas.domain.*;
import com.moogrisoft.openseas.service.dto.ProfileDTO;

import org.mapstruct.*;
import java.util.List;

/**
 * Mapper for the entity Profile and its DTO ProfileDTO.
 */
@Mapper(componentModel = "spring", uses = {UserMapper.class, })
public interface ProfileMapper {

    @Mapping(source = "user.id", target = "userId")
    ProfileDTO profileToProfileDTO(Profile profile);

    List<ProfileDTO> profilesToProfileDTOs(List<Profile> profiles);

    @Mapping(source = "userId", target = "user")
    Profile profileDTOToProfile(ProfileDTO profileDTO);

    List<Profile> profileDTOsToProfiles(List<ProfileDTO> profileDTOs);
    /**
     * generating the fromId for all mappers if the databaseType is sql, as the class has relationship to it might need it, instead of
     * creating a new attribute to know if the entity has any relationship from some other entity
     *
     * @param id id of the entity
     * @return the entity instance
     */

    default Profile profileFromId(Long id) {
        if (id == null) {
            return null;
        }
        Profile profile = new Profile();
        profile.setId(id);
        return profile;
    }


}

There is no UserMapper class in my project as non was generated for me.

If I create a Monolithic application then creating the entity relationship works fine, it's only a problem with a Microservice because of the missing User class and related classes.

like image 269
Mook5ter Avatar asked Apr 13 '17 13:04

Mook5ter


Video Answer


1 Answers

In a JHipster microservice architecture, the User entity is located on the gateway or UAA service.

Profile Entity

In the UAA, you can create an entity with a relationship to the user. Adding additional information such as a profile to the entity is a good use case for this. Doing the same thing in a gateway is also possible. An example UAA that creates an entity with a relationship to the user can be seen https://github.com/xetys/microxchng-workshop/tree/master/uaa

Blog Entity

Using the entity sub-generator works a little bit differently in a microservices architecture, as the front-end and the back-end codes are not located in the same application. https://jhipster.github.io/microservices-architecture/#generating_entities

For this type of entity, I recommend storing the user's ID along with the entity. You will need to pass the user ID from the client as the ID is not easily accessible from a microservice. The reason we use the user ID instead of login is that the login can be modified through the User Management pages, causing the relationships to be fail (from the old login value).

The other option is to use the userLogin, which is accessible in a microservice through SecurityUtils.java. As long as you handle that the login can change, this is another option.

like image 74
Jon Ruddell Avatar answered Sep 30 '22 15:09

Jon Ruddell