Home > database >  Spring not fetching certain fields from mysql database
Spring not fetching certain fields from mysql database

Time:06-03

I am using Spring data JPA to fetch Users from my database, then I map them to a DTO before returning to my controller. However, the JPA repository is not fetching the date field and the email field.

I know my Mapper is not responsible for this as I logged the creation date of the original fetched entity, and it comes as null.

my getUser method:

@Transactional(readOnly = true)
public UserDto getUser(Long id) {
    User user = userRepository.findById(id).orElseThrow(
            () -> new TweeterException("No user with id "   id)
    );
    // For debugging purposes
    log.info("User creation date: "   user.getCreationDate());

    UserDto userDto = UserMapper.mapToDto(user);
    // For debugging purposes
    log.info("User dto creation date "   userDto.getCreationDate());
    return userDto;
}

My User entity:

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(unique = true, name = "username")
    @NotBlank
    private String username;

    @Column(name= "password")
    @NotBlank
    private String password;

    @Column(name="email")
    @Email
    private String email;

    @Column(name="profile_image_url")
    @NotBlank
    private String profileImageUrl;

    @Lob
    @Column(name="biography")
    @NotBlank
    private String biography;

    @Column(name="creation_date")
    private Instant creationDate;

    @Column(name = "enabled")
    private boolean enabled;

    @ElementCollection
    @Column(name="following")
    private Set<Integer> following = new HashSet<>();

    @ElementCollection
    @Column(name="followers")
    private Set<Integer> followers = new HashSet<>();


    public void addFollower(Integer follower) {
        this.followers.add(follower);
    }

    public void removeFollower(Integer follower) {
        this.followers.remove(follower);
    }

    public void addFollowing(Integer following) {
        this.following.add(following);
    }

    public void removeFollowing(Integer following) {
        this.following.remove(following);
    }

My UserDto:

@Data
@Builder
public class UserDto {
    private Long id;
    private String username;
    private String email;
    private String biography;
    private String profileImageUrl;
    private Instant creationDate;
    private Long followerCount;
}

I also have a Tweet entity and TweetDto, which works perfectly fine with the dates, so I'm not quite sure what is going on

CodePudding user response:

I think that may be for creationDate() field you can try to use Java's Date data-type. And also instead of @Getter and @Setter try @Data Annotation of Lombok, For email field may be try only to put the @Email Annotation. And check your Table names with the Entity.

CodePudding user response:

Use java.util.date for creationDate instead of Instant. For email please verify column name. Also add @Table annotation at class level with Db table name. Ex:

@Table("name of table in database")

This is required only if you have different table name in database and tables names are case sensitive too.

  • Related