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