【问题标题】:Setting an UserExtra, which extends the jhipster User, into relationship to another entity将扩展 jhipster 用户的 UserExtra 设置为与另一个实体的关系
【发布时间】:2018-07-06 15:10:37
【问题描述】:

根据 Spring Data JPA Property Expressions 在 jhipster (4.14.4) 中我的 UserExtraRepository.java 中的查询

UserExtra findOneByUserLogin(String login)

应该返回一个 UserExtra-Object,它与用户 (jhi_user) 具有 1:1 的关系,其中用户由其login 字符串识别。

但是当我尝试在另一个实体资源类 (PartyFoodResource.java) 的 POST 方法中使用它时,我得到一个空指针异常:

// set UserExtra from logged in User (jhi_user) as Owner of PartyFood-Object
    String userLogin = SecurityUtils.getCurrentUserLogin().get();
    if(userLogin == ""){
        throw new IllegalArgumentException("userLogin can not be empty!");
    }
    log.debug("userLogin is '" + userLogin + "'");
    UserExtra userExtra = new UserExtra();
    userExtra = userExtraService.findOneByUserLogin(userLogin);
    log.debug("userExtra gefunden : {}", userExtra);
    partyFood.setUserExtra(userExtra);

    PartyFood result = partyFoodService.save(partyFood);

你能给我一个提示吗,我现在不明白什么?


编辑: 如果我尝试the answer from agilob,我也会得到一个空指针异常,final Optional<User> isUser = userService.getUserWithAuthorities();

如果我尝试

    String loggedInUser = SecurityUtils.getCurrentUserLogin().get();
    log.debug("user gefunden : {}", loggedInUser);

    final Optional<User> isUser = userService.getUserWithAuthoritiesByLogin(loggedInUser);

我得到一个带有登录字符串的调试日志条目,但仍然是带有userService.getUserWithAuthoritiesByLogin(loggedInUser); 的空指针异常

【问题讨论】:

    标签: java spring-data-jpa jhipster


    【解决方案1】:

    我已删除 PartyFoodResource.java 中的代码并将其粘贴到 PartyFoodServiceImpl.java 中。它现在正在与:

    /**
     * Save a partyFood.
     *
     * @param partyFood the entity to save
     * @return the persisted entity
     */
    @Override
    public PartyFood save(PartyFood partyFood) {
        log.debug("Request to save PartyFood : {}", partyFood);
    
     /**
     * Add the logged in user into a relation to newly created records of the entity PartyFood.
     * The user will be the owner of the PartyFood.
     */
    
        final Optional<User> isUser = userService.getUserWithAuthorities();
        if (!isUser.isPresent()) {
            log.debug("User is not logged in");
        }
    
        final User user = isUser.get();
    
        UserExtra userExtra = new UserExtra();
        userExtra = userExtraService.findOneByUserId(user.getId());
        log.debug("userExtra gefunden : {}", userExtra.getUser().getLogin());
        partyFood.setUserExtra(userExtra);
    
        PartyFood result = partyFoodRepository.save(partyFood);
        partyFoodSearchRepository.save(result);
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 2016-06-29
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多