本文介绍了缺少用户类-在微服务中创建与用户实体的实体关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个与JHipster的User实体具有多对一关系的实体.

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

我已经使用JDL-Studio创建了以下与User的实体和关系,这些实体和关系使用jhipster import-jdl作为.jh文件导入到我的微服务中:

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:

ProfileMapper.java:12:错误:找不到符号 @Mapper(componentModel ="spring",使用= {UserMapper.class,})符号:类UserMapper

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

ProfileMapper.java:12:错误:无法检索@Mapper批注 @Mapper(componentModel ="spring",使用= {UserMapper.class,})

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

Profile.java的第43-44行是:

Lines 43-44 of Profile.java are:

@ManyToOne
private User user;

和ProfileMapper.java如下:

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;
    }


}

我的项目中没有UserMapper类,因为不是为我生成的.

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

如果我创建一个Monolithic应用程序,那么创建实体关系就可以了,因为缺少User类和相关类,这只是Microservice的问题.

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.

推荐答案

在JHipster微服务体系结构中,用户实体位于网关或 UAA服务.

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

个人资料实体

在UAA中,您可以创建与用户有关系的实体.为此,向实体添加诸如配置文件之类的附加信息是一个很好的用例.在网关中做同样的事情也是可能的.可以看到 https:创建与用户有关系的实体的示例UAA. //github.com/xetys/microxchng-workshop/tree/master/uaa

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

博客实体

对于这种类型的实体,我建议将用户的ID与该实体一起存储.您将需要从客户端传递用户ID,因为无法从微服务轻松访问该ID.我们使用用户ID代替登录的原因是可以通过用户管理"页面修改登录,从而导致关系失败(从旧的登录值开始).

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).

另一种选择是使用userLogin,可通过SecurityUtils.java在微服务中对其进行访问.只要您可以更改登录名,这是另一个选择.

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.

这篇关于缺少用户类-在微服务中创建与用户实体的实体关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:15