如何解决此问题。给出投射建议但不起作用 [重复]

How to solve this issue. Giving Casting suggestion yet not working [duplicate]

提问人:Prateek Rai 提问时间:10/27/2023 最后编辑:Brian Tompsett - 汤莱恩Prateek Rai 更新时间:10/27/2023 访问量:22

问:

这是我的.在此处使用 JPA 存储库将用户保存到实体中。UserServiceImpl

public UserDTO createUser(UserDTO userdto) {

    UserEntity user = dtoToUser(userdto);
    UserEntity savedUser = userRepo.save(user);

    return userToDto(savedUser);
}

并更新用户下面的下一个:

public UserDTO updateUser(UserDTO userdto, Integer userId) {
    try {
        UserEntity user = this.userRepo.findById(userId)
                .orElseThrow(() -> new ResourceNotFoundException("User", "id", userId));

        user.setName(userdto.getName());
        user.setEmail(userdto.getEmail());
        user.setPassword(userdto.getPassword());
        user.setAbout(userdto.getAbout());

        UserEntity updateUser = this.userRepo.save(user);
        UserDTO userdto1 = this.userToDto(updateUser);

    } catch (Throwable e) {
        e.printStackTrace();
    }
    return userdto;

}

这是:UserRepo

public interface UserRepo<T> extends JpaRepository<UserEntity, Integer>{
    
}

我还创建了一个帖子实体,用户可以在其中创建多个帖子,但在 .PostServiceImpl

public PostDTO create(PostDTO postDto, Integer userId, Integer categoryId) throws Throwable {
        UserRepo user =  this.userRepo.findById(userId).
                         orElseThrow(()-> new ResourceNotFoundException("user", "userId", userId));
        CategoryRepo category =  this.categoryRepo.findById(categoryId).
                        orElseThrow(()-> new ResourceNotFoundException("category", "categoryId", categoryId));
    
    PostsEntity post = this.modelmap.map(postDto, PostsEntity.class);
    post.setImageName("default.png");
    post.setUploadDate(new Date());
    post.setCategory((Category) category);
    post.setUser((UserEntity) user);
    
    PostsEntity newPost= postRepo.save(post);
    
    return modelmap.map(newPost, PostDTO.class);
}

发布回购:

public interface PostRepo extends JpaRepository<PostsEntity, Integer> {
    
    List<PostsEntity> findByUser(UserEntity userId);
    List<PostsEntity> findByCategory(Category categoryId);
    
}

我尝试在代码中进行转换,但是当使用Postman命中API时,它无法提供数据。不过,它适用于 UserEntity 和 CategoryEntity。

PostServiceImpl

public PostDTO create(PostDTO postDto, Integer userId, Integer categoryId) throws Throwable {
        UserRepo user =  (UserRepo) this.userRepo.findById(userId).
                         orElseThrow(()-> new ResourceNotFoundException("user", "userId", userId));
        CategoryRepo category =  (CategoryRepo) this.categoryRepo.findById(categoryId).
                        orElseThrow(()-> new ResourceNotFoundException("category", "categoryId", categoryId));
    
    PostsEntity post = this.modelmap.map(postDto, PostsEntity.class);
    post.setImageName("default.png");
    post.setUploadDate(new Date());
    post.setCategory((Category) category);
    post.setUser((UserEntity) user);
    
    PostsEntity newPost= postRepo.save(post);
    
    return modelmap.map(newPost, PostDTO.class);
}

任何关于这方面的想法都会有所帮助。我该如何解决这个问题?

Java spring-boot 错误处理 转换

评论

1赞 ewokx 10/27/2023
欢迎使用 Stack Overflow。请不要发布重复的问题。您只需要发布一次。

答: 暂无答案