Skip to content
Advertisement

I have created a spring boot with DTO, Entity and also not able to store it

I have created a spring boot with DTO, Entity and also generating entity from dto and vice versa but the data is not getting stored in the Postgres sql when i am trying to get the data its is showing null but creation is happing First is the controller class

import com.school.Entity.StudentDetails;
import com.school.Service.StudentService;
import com.school.dto.StudentDetailDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin("*")
public class SchoolController {

    @Autowired
    StudentService studentService;

    @GetMapping("/test")
    public String test(){
        return "test";
    }
    @PostMapping("/uploadStudentData")
    public StudentDetailDto createStudentDetails(@RequestBody StudentDetailDto dto){
        return studentService.createStudentDetails(dto);
    }
    @GetMapping("/getAll")
    public List<StudentDetails> getAllStudentData(){
        return studentService.getAllStudentData();
    }
}

Now the StudentDetailDto class

import java.util.List;

public class StudentDetailDto {
    private Long id;
    private String branch;
    private List<StudentDto> student;

    //getter and setter
}

now the StudenDto class

public class StudentDto {
    private Long id;
    private String firstName;
    private String lastName;
    private List<MarksListDto> marksList;
// getter and setter
}

now the marks list class

public class MarksListDto {
    private Long id;
    private String subjectName;
    private Integer marks;
// getter and setter
}

now the SchoolDetails entity class

@Entity
public class StudentDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String branch;
    @OneToMany(targetEntity=Students.class, mappedBy="id", fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    private List<Students> student;
//getter and setter
}

Now the Student Entity class

@Entity
public class Students {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @OneToMany(targetEntity=MarksList.class, mappedBy="id", fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    private List<MarksList> marksList;
//getter and setter
}

Now the marks list entity

@Entity
public class MarksList {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String subjectName;
    private int marks;
//getter and setter
}

Repository

public interface StudentRepository extends JpaRepository<StudentDetails, Long> {
}

Now the most important that i am thinking there is something wrong is the servers

import com.school.Entity.StudentDetails;
import com.school.Repository.StudentRepository;
import com.school.dto.StudentDetailDto;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class StudentService {
    @Autowired
    StudentRepository studentRepository;

    @Autowired
    ModelMapper modelMapper;

    public StudentDetailDto createStudentDetails(StudentDetailDto dto) {
        validateDto(dto);
        StudentDetails studentDetails = generateEntityFromDto(dto);
        studentDetails = studentRepository.save(studentDetails);
        return generateDtoFromEntity(studentDetails);
    }

    private StudentDetails generateEntityFromDto(StudentDetailDto dto) {
        StudentDetails studentDetails = modelMapper.map(dto, StudentDetails.class);
        return studentDetails;
    }

    private StudentDetailDto generateDtoFromEntity(StudentDetails studentDetails){
        StudentDetailDto dto = modelMapper.map(studentDetails,StudentDetailDto.class);
        return dto;
    }

    private void validateDto(StudentDetailDto dto) {

    }

//    public List<StudentDetailDto> getAllStudentData() {
//        List<StudentDetailDto> dto = new ArrayList<>();
//         studentRepository.findAll().forEach(dto::add);
//         return dto;
//    }
//    public String createStudentDetails(StudentDetails studentDetails){
//        studentRepository.save(studentDetails);
//        return "Student details created";
//    }
//
    public List<StudentDetails> getAllStudentData() {
        return new ArrayList<>(studentRepository.findAll());

    }
}

So when i am sending the data in postman it’s showing me this it is storing null value i don’t now y can anyone help me fix this and can help me to improve the code to the best version

postman image

Advertisement

Answer

Now it is working fine,I added following changes:

Define the ModelMapper bean in your Spring configuration:

@Bean
public ModelMapper modelMapper() {
    return new ModelMapper();
}

dependency

<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.5</version>
 

Apart from above changes rest of the things are just fine,but Please optimize your code further.

Tested using postman

enter image description here enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement