spring Boot problem, In the database, when the data is entered, why is it not saved to the linked table , why is the foreign key null?

package com.example.project2.entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "LoginUser")
public class LoginUser {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int loginID;

    private String loginEmail;
    private int loginContactNumber;
    private String loginPassword;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_reUserID", referencedColumnName = "reUserID")
    private RegisteredUser registeredUser;
}
package com.example.project2.entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "RegisteredUser")
public class RegisteredUser {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int reUserID;

    private String reName;
    private String reEmail;
    private String reUserName;
    private String rePassword;

    @OneToOne(mappedBy = "registeredUser")
    private LoginUser loginUser;
}
package com.example.project2.service;
import com.example.project2.dto.LoginUserDTO;
import com.example.project2.dto.RegisteredUserDTO;
import com.example.project2.entity.LoginUser;
import com.example.project2.entity.RegisteredUser;
import com.example.project2.repo.LoginUserRepo;
import com.example.project2.repo.RegisteredUserRepo;
import com.example.project2.utill.VarList;
import jakarta.transaction.Transactional;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional

public class ProjectService {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private RegisteredUserRepo registeredUserRepo;

    public String saveRegisteredUser(RegisteredUserDTO registeredUserDTO){
        if (registeredUserRepo.existsById(registeredUserDTO.getReUserID())){
            return VarList.RSP_DUPLICATE;
        }else {
            registeredUserRepo.save(modelMapper.map(registeredUserDTO, RegisteredUser.class));
            return VarList.RSP_SUCCESS;
        }
    }

    @Autowired
    private LoginUserRepo loginUserRepo;

    public String saveLoginUser(LoginUserDTO loginUserDTO){
        if (loginUserRepo.existsById(loginUserDTO.getLoginID())){
            return VarList.RSP_DUPLICATE;
        }else {
            loginUserRepo.save(modelMapper.map(loginUserDTO, LoginUser.class));
            return VarList.RSP_SUCCESS;
        }
    }
    
}
package com.example.project2.controller;

import com.example.project2.dto.*;
import com.example.project2.repo.LoginUserRepo;
import com.example.project2.repo.RegisteredUserRepo;
import com.example.project2.service.ProjectService;
import com.example.project2.utill.VarList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/project")
public class Controller {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ResponseDTO responseDTO;

    @PostMapping(value = "/saveEmployee")
    public ResponseEntity saveEmployee(@RequestBody EmployeeDTO employeeDTO){
        try {
            String res= projectService.saveEmployee(employeeDTO);
            if (res.equals("00")){
                responseDTO.setCode(VarList.RSP_SUCCESS);
                responseDTO.setMassage("Success");
                responseDTO.setContemt(employeeDTO);
                return new ResponseEntity(responseDTO, HttpStatus.ACCEPTED);

            }else if(res.equals("06")){
                responseDTO.setCode(VarList.RSP_DUPLICATE);
                responseDTO.setMassage("Employee registered");
                responseDTO.setContemt(employeeDTO);
                return new ResponseEntity(responseDTO, HttpStatus.BAD_REQUEST);
            }else {
                responseDTO.setCode(VarList.RSP_FAIL);
                responseDTO.setMassage("Error");
                responseDTO.setContemt(null);
                return new ResponseEntity(responseDTO, HttpStatus.BAD_REQUEST);
            }
        }catch (Exception ex){
            responseDTO.setCode(VarList.RSP_ERROR);
            responseDTO.setMassage("Success");
            responseDTO.setContemt(null);
            return new ResponseEntity(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    
    @Autowired
    private AdminRepo adminRepo;

    @PostMapping(value = "/saveAdmin")
    public ResponseEntity saveAdmin(@RequestBody AdminDTO adminDTO){
        try {
            String res = projectService.saveAdmin(adminDTO);
            if(res.equals("00")){
                responseDTO.setCode(VarList.RSP_SUCCESS);
                responseDTO.setMassage("succes");
                responseDTO.setContemt(adminDTO);
                return new ResponseEntity(responseDTO, HttpStatus.ACCEPTED);

            } else if (res.equals("06")) {
                responseDTO.setCode(VarList.RSP_DUPLICATE);
                responseDTO.setMassage("Employee registerd");
                responseDTO.setContemt(adminDTO);
                return new ResponseEntity(responseDTO, HttpStatus.BAD_REQUEST);

            }else {
                responseDTO.setCode(VarList.RSP_FAIL);
                responseDTO.setMassage("Error");
                responseDTO.setContemt(adminDTO);
                return new ResponseEntity(responseDTO, HttpStatus.BAD_REQUEST);
            }
        }catch (Exception ex){
            responseDTO.setCode(VarList.RSP_ERROR);
            responseDTO.setMassage("Success");
            responseDTO.setContemt(adminDTO);
            return new ResponseEntity(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
   

    @Autowired
    private RegisteredUserRepo registeredUserRepo;

    @PostMapping(value = "/saveRegisteredUser")
    public ResponseEntity saveRegisteredUser(@RequestBody RegisteredUserDTO registeredUserDTO){
        try {
            String res = projectService.saveRegisteredUser(registeredUserDTO);
            if(res.equals("00")){
                responseDTO.setCode(VarList.RSP_SUCCESS);
                responseDTO.setMassage("succes");
                responseDTO.setContemt(registeredUserDTO);
                return new ResponseEntity(responseDTO, HttpStatus.ACCEPTED);

            } else if (res.equals("06")) {
                responseDTO.setCode(VarList.RSP_DUPLICATE);
                responseDTO.setMassage("RegisteredUser registerd");
                responseDTO.setContemt(registeredUserDTO);
                return new ResponseEntity(responseDTO, HttpStatus.BAD_REQUEST);

            }else {
                responseDTO.setCode(VarList.RSP_FAIL);
                responseDTO.setMassage("Error");
                responseDTO.setContemt(registeredUserDTO);
                return new ResponseEntity(responseDTO, HttpStatus.BAD_REQUEST);
            }
        }catch (Exception ex){
            responseDTO.setCode(VarList.RSP_ERROR);
            responseDTO.setMassage("Success");
            responseDTO.setContemt(registeredUserDTO);
            return new ResponseEntity(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
  
    @Autowired
    private LoginUserRepo loginUserRepo;

    @PostMapping(value = "/saveLoginUser")
    public ResponseEntity saveLoginUser(@RequestBody LoginUserDTO loginUserDTO){
        try {
            String res = projectService.saveLoginUser(loginUserDTO);
            if(res.equals("00")){
                responseDTO.setCode(VarList.RSP_SUCCESS);
                responseDTO.setMassage("succes");
                responseDTO.setContemt(loginUserDTO);
                return new ResponseEntity(responseDTO, HttpStatus.ACCEPTED);

            } else if (res.equals("06")) {
                responseDTO.setCode(VarList.RSP_DUPLICATE);
                responseDTO.setMassage("LoginUser registerd");
                responseDTO.setContemt(loginUserDTO);
                return new ResponseEntity(responseDTO, HttpStatus.BAD_REQUEST);

            }else {
                responseDTO.setCode(VarList.RSP_FAIL);
                responseDTO.setMassage("Error");
                responseDTO.setContemt(loginUserDTO);
                return new ResponseEntity(responseDTO, HttpStatus.BAD_REQUEST);
            }
        }catch (Exception ex){
            responseDTO.setCode(VarList.RSP_ERROR);
            responseDTO.setMassage("Success");
            responseDTO.setContemt(loginUserDTO);
            return new ResponseEntity(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

I make two tables named, LoginUser and RegisteredUser. then connected two tables with a foreign key. then I created save methods for above class. Then I called saveLOginUser method in postman using an Api. Then I entered a dataset for the two tables, but data is only saved in the LoginUser table and not in the RegisteredUser table, also the primary key is null. Why data is not saved in the RegisterUser table.

New contributor

sunith p pathirana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

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