Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I am getting below error while creating a spring maven rest webservice project. I am new with springboot, could someone help me with this. The error is:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.example.spring.backend.model.Employee.firstName; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.example.spring.backend.model.Employee.firstName] with root cause
I created a database using MySQL Workbench and defined a table in it to store employee details. Here is my code:
EmployeeController.java
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
private EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
super();
this.employeeService = employeeService;
// build create employee REST API
@PostMapping()
public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee){
return new ResponseEntity<Employee>(employeeService.saveEmployee(employee), HttpStatus.CREATED);
Employee.java
@Data
@Entity
@Table(name="employees")
public class Employee {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
EmployeeRepository.java
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
EmployeeService.java
public interface EmployeeService {
Employee saveEmployee(Employee employee);
EmployeeServiceImpl.java
@Service
public class EmployeeServiceImpl implements EmployeeService{
private EmployeeRepository employeeRepository;
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
super();
this.employeeRepository = employeeRepository;
@Override
public Employee saveEmployee(Employee employee) {
return employeeRepository.save(employee);
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/employee_management_system?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
# Hibernate properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# create, create-drop
spring.jpa.hibernate.ddl-auto=update
While sending a post request using postman, I am getting Internal Server Error 500.
**Input**
"firstName": "abc",
"lastName": "abc",
"email": "abc@gmail.com"
**Result:**
"timestamp": "2022-10-26T09:48:51.322+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/api/employees"
I am taking reference from a youtube video but in the video, the code works but not in my case.
Please advise what is wrong in my code. Thanks in advance.
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.