(获取文件网址)尽管数据库中有数据,但 Spring Boot 实体仍返回 null

(Getting file url)Spring Boot entity returns null despite data in the database

提问人:yjoos 提问时间:11/2/2023 最后编辑:yjoos 更新时间:11/2/2023 访问量:43

问:

我正在努力实现个人资料图片上传和显示功能。在我的服务层中,我创建了一个方法:

public UserDto getUserProfileImage(Principal principal)

但是,当我使用此方法时,用户实例似乎为 getImageUrl() 属性返回 null。这很奇怪,因为即使数据库中同时存在 nickName 和 image_url,也可以毫无问题地检索其他属性(如 nickName)。

下面是相关代码: 手头的问题是用户实例在调用 .getImageUrl() 时特别返回 null。请查看代码片段中的第 7 行和第 9 行,因为它们都会导致 null 值事件,因为数据库中有一个 url

@Transactional(readOnly = true)
    public UserDto getUserProfileImage(Principal principal){
        String email = principal.getName();

        User user = userRepository.findByEmail(email).get();
        String nick = user.getNickName();
        String url = user.getImageUrl();

        System.out.println("user -"+nick);
        System.out.println("fetch");
        System.out.println("image url -" + url); //null :(


        UserDto userDto = UserDto.builder()
                .imageUrl(user.getImageUrl())
                .build();

        return userDto;
    }

User Entity 和 UserDto 的一部分


@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@Getter
@Setter
@Entity
@Table(name = "app_user")  
public class User extends BaseTimeEntity{ 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column (name = "user_id")
    private Long id;


    @Column(name = "user_name", nullable = false)
    @Size(max = 100)
    private String userName; 

    @Column(name = "nick_name", nullable = false, unique = true)
    @Size(max = 100)
    private String nickName; 

    private String imageUrl; // uuid

    
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserDto {

    private Boolean isCreated;
    private String userName;
    private String nickName;
    private String email;
    private Role role;
    private String token;
    private String imageUrl;

}

虽然代码的其他部分按预期工作,但这些行存在问题,并为 image_url 属性返回 null。非常感谢您在解决此问题方面的帮助。

用户实体用户存储库: -user 实体很长,所以我删除了一些不必要的属性 喜欢 accessedDate

用户实体


import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@Getter
@Setter
@Entity
@Table(name = "app_user")  
public class User extends BaseTimeEntity{ 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column (name = "user_id")
    private Long id; 

    @OneToMany(mappedBy = "user")
    private List<Posts> posts = new ArrayList<>();  

    @OneToMany(mappedBy = "user")
    private List<Comments> comments = new ArrayList<>(); 

    @OneToMany(mappedBy = "user")
    private List<ScrapPosts> scrapPosts = new ArrayList<>(); 

    @OneToMany(mappedBy = "user")
    private List<UserApplyPosts> userApplyPosts = new ArrayList<>(); 

    @OneToMany(mappedBy = "user")
    private List<Notifications> notifications = new ArrayList<>(); 

    @Column(name = "nick_name", nullable = false, unique = true)
    @Size(max = 100)
    private String nickName; 

    @Column(nullable = false, unique = true) 
    @Size(max = 100)
    private String email; 

    @Column(nullable = false)
    @Size(max = 100)
    private String password; 

    private String imageUrl; // google storage uuid


    public User(Long id, String userName, String nickName, String email, String password, Role role, LocalDateTime lastAccessDate) {
        this.id = id;
        this.userName = userName;
        this.nickName = nickName;
        this.email = email;
        this.password = password;
        this.role = role;
        this.lastAccessDate = lastAccessDate;
    }

}


用户存储库

import Demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {

    Optional<User> findByEmail(String email);

    Optional<User> findByNickName(String nickName);

    List<User> findAll();
}

Spring Database spring-boot spring-data-jpa null

评论

1赞 Shrikant Havale 11/2/2023
你能分享完整的实体和UserUserRepository
0赞 yjoos 11/2/2023
我添加了 User 和 UserRepository。在服务层中,getUserProfileImage(principal) 函数检索 User,提取 URL,并利用它来创建 UserDto!谢谢:)

答: 暂无答案