skip to Main Content

After one or two months, I decided to work with Spring Boot again and tried to build a REST API. While performing CRUD operations, I faced an issue where my data was not being saved to the database. I am using PostgreSQL. I tried finding solutions but did not benefit from any of them.

application.properties

spring.application.name=Spring-Boot-Tutorial

spring.datasource.url = jdbc:postgresql://localhost:5432/test
spring.datasource.username = postgres
spring.datasource.password = root
spring.datasource.driver-class-name = org.postgresql.Driver
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = update

Sample model- Department.class

package com.ripan.springboot.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.*;

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer departmentId;

    private String departmentName;
    private String departmentAddress;
    private String departmentCode;
}

Controller

package com.ripan.springboot.controller;

import com.ripan.springboot.model.Department;
import com.ripan.springboot.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PostMapping(value = "/save")
    public Department saveDepartment(@RequestBody Department department){
        return departmentService.saveDepartment(department);
    }
}

Service

package com.ripan.springboot.service;

import com.ripan.springboot.model.Department;

public interface DepartmentService {
    public Department saveDepartment(Department department);
}

impl

package com.ripan.springboot.service;

import com.ripan.springboot.model.Department;
import com.ripan.springboot.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DepartmentServiceImpl implements DepartmentService{
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public Department saveDepartment(Department department) {
        return departmentRepository.save(department);
    }
}

Repository

package com.ripan.springboot.repository;

import com.ripan.springboot.model.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Integer> {
}

Json data for performing PostMapping

{
    "departmentName": "HR",
    "departmentAddress": "New York",
    "departmentCode": "HR123"
}

after hit this request i am getting 200 ok, with empty {} response. also in the database whatever the data i am trying to store, does not get stored.

test=# select * from department;
 department_id | department_address | department_code | department_name
---------------+--------------------+-----------------+-----------------
            52 |                    |                 |
(1 row)

this is not my first time in spring-boot, but never faced this type of issue.

2

Answers


  1. You can try below approaches:

    • Mention table name using @Table(name="") in your Department entity.

    • In the Department entity, use @Column annotation for each field, normally spring picks automatically but specifying would be better.

    • Instead of Department Entity in the @RequestBody you can use DTO as a best practice.(optional)

    Login or Signup to reply.
  2. You don’t have your columns mapped, only the id. It’s doing exactly what you have written: mapping the id to the database and nothing else.

    Try adding a @Column annotation to each field you want mapped to the database:

    @Getter
    @Setter
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    @Entity
    public class Department {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer departmentId;
    
        @Column(name = "department_name")
        private String departmentName;
    
        // ... Other fields here
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search