Mapping a Long field in UserDTO to a Long field in the createdBy field of User using ModelMapper

The roleId from the UserDTO is getting correctly mapped to the role object’s ID in the User class but the same is not happening for createdBy, it’s showing as null

I have used ModelMapper library to reduce the code. Is there anything that I am missing here, thanks in advance

UserDTO.class

package com.merck.nlp.dto;

import com.merck.nlp.validation.CreateGroup;
import com.merck.nlp.validation.UpdateGroup;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserDTO {
    
//  @NotNull(groups = {UpdateGroup.class}, message = "ID cannot be null")
//  @Positive(groups = {UpdateGroup.class})
//  private Long id;
    
    @NotBlank(groups = {CreateGroup.class}, message = "XUID cannot be empty")
    @Pattern(groups = {CreateGroup.class, UpdateGroup.class}, regexp = "^[a-zA-Z0-9]+$", message = "XUID can only contain alphanumeric characters")
    private String xuid;
    
    @NotBlank(groups = {CreateGroup.class, UpdateGroup.class}, message = "First name cannot be empty")
    @Pattern(groups = {CreateGroup.class, UpdateGroup.class}, regexp = "^[a-zA-Z0-9]+$", message = "First name can only contain alphanumeric characters")
    @Size(groups = {CreateGroup.class, UpdateGroup.class}, min = 3, max = 30, message = "First name must be between 3 and 30 characters")
    private String firstName;
    
    @NotBlank(groups = {CreateGroup.class, UpdateGroup.class}, message = "Last name cannot be empty")
    @Pattern(groups = {CreateGroup.class, UpdateGroup.class}, regexp = "^[a-zA-Z0-9]+$", message = "Last name can only contain alphanumeric characters")
    @Size(groups = {CreateGroup.class, UpdateGroup.class}, min = 3, max = 30, message = "Last name must be between 3 and 30 characters")
    private String lastName;
    
    @Email(groups = {CreateGroup.class, UpdateGroup.class}, message = "Email should be valid")
    @NotNull(groups = {CreateGroup.class}, message = "Email cannot be null")
    private String email;
    
    private Boolean isActive;
    
    @Positive(groups = {CreateGroup.class, UpdateGroup.class}, message = "Role ID must be a positive number")
    @NotNull(groups = {CreateGroup.class, }, message = "Role ID cannot be null")
    private Long roleId;
    
    @NotNull(groups = {CreateGroup.class})
    @Positive(groups = {CreateGroup.class}, message = "Created By ID must be a positive number")
    private Long createdById;
//  
//  @NotNull(groups = {UpdateGroup.class})
//  @Positive(groups = {UpdateGroup.class}, message = "Updated By ID must be a positive number")
//  private Long updatedById;

}

UserService.class

package com.merck.nlp.service;

import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.modelmapper.spi.MatchingStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.merck.nlp.dto.UserDTO;
import com.merck.nlp.model.Role;
import com.merck.nlp.model.User;
import com.merck.nlp.repository.RoleRepository;
import com.merck.nlp.repository.UserRepository;
import com.merck.nlp.exception.RoleNotFoundException;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private ModelMapper modelMapper;

    public User createUser(UserDTO userDTO) {
        
//      modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);
        
//       modelMapper.typeMap(UserDTO.class, User.class).addMappings(mapper -> {
//           mapper.skip(User::setRole);
//             mapper.map(UserDTO::getCreatedById, (destination, value) -> destination.getCreatedBy().setId((Long)value));
//         });

        // Map DTO to User entity
//      modelMapper.
        
        User user = modelMapper.map(userDTO, User.class);
        System.out.println(user.getCreatedBy());
        modelMapper.validate();
        // Resolve Role
        Role role = null;
        if (user.getRole().getId() != null) {
            role = roleRepository.findById(user.getRole().getId())
                    .orElseThrow(() -> new RoleNotFoundException("Role not found with ID: " + user.getRole().getId()));
        } else if (user.getRole().getName() != null) {
            role = roleRepository.findByName(user.getRole().getName());
        }
        if (role == null) {
            // Handle role not found error or set a default role
        }
        user.setRole(role);

        // Save user to repository or perform other operations
        return userRepository.save(user);
    }
}

payload

    {
    "firstName": "niresh",
    "lastName": "kef",
    "email": "[email protected]",
    "xuid":"weffeferfreergerferrrergerer",
    "createdById": 1,
    "roleId": 1    
   }

User.class

package com.merck.nlp.model;

import java.sql.Timestamp;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.CreationTimestamp;

import com.merck.nlp.dto.UserDTO;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "users")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String xuid;
    
    private String firstName;
    
    private String lastName;
    
    private String email;
    
    @ColumnDefault("false")
    private Boolean isActive;
        
    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "role_id")
    private Role role;
    
    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "created_by")
    private User createdBy;
    
    @CreationTimestamp
    private Timestamp createdAt;
    
    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "updated_by")
    private User updatedBy;
    
    private Timestamp updatedAt;

    public User(Long id) {
        this.id = id;
    }

}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật