welcome back
welcome back

Reputation: 29

why employee_id and department_id not update automatic in database Hibernate jpa

few days back i starting learning hibernate JPA but i am unable to find solution of given problem below

My Project consist three class employee ,phone ,department by seeing code you can easily understand what i am doing .

Main problem raise when i try to save this data into database using spring boot controller it showing null value in column . In employee table department_id is null(not automatic update using cascade.All) same in phone table employee_id is null.

I do not want update manually .is their any way so dep_id and emp_id automatic update to foreign key table .

{
"name":"CSE",
 "employees":[
     {
         "name":"Welcome",
         "age":23,
         "phones":[{"number":1234567890},{"number":1234567890}]
     },
     {
         "name":"back",
         "age":25,
         "phones":[{"number":1234567890},{"number":1234567890}]
     }
     
 ]

}

package com.example.entity;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    private int age;

    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;

    @OneToMany(mappedBy = "employee", cascade=CascadeType.ALL)
    private List<Phone> phones;

    // getters and setters...
}
package com.example.entity;

import javax.persistence.*

@Entity
public class Phone {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String number;

    @ManyToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    // getters and setters...
}
package com.example.entity;

import javax.persistence.*;
import java.util.List;

@Entity
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    @OneToMany(mappedBy = "department",cascade = CascadeType.ALL)
    private List<Employee> employees;
}
dept table  in database
id   name
1    CSE
employee table 
id  name     age department_id
1.  welcome   23.    null
2.  back.     25.    null
phone table
id  number    employee_id
1.  1234567890. null
2.  1234567890.  null
3.  1234567890   null
4.  1234567890.  null

Why employee_id and department_id not updating automatic in cascade All

Controller class
package com.example.controller;

import com.example.dao.DepRepo;
import com.example.dao.EmployeeRepo;
import com.example.dao.PhoneRepo;
import com.example.entity.Department;
import com.example.entity.Employee;
import com.example.service.FakeService;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import jakarta.inject.Inject;

@Controller("/dummy")
public class DummyController {

    @Inject
    FakeService fakeService;
    @Inject
    PhoneRepo phoneRepo;
    @Inject
    EmployeeRepo employeeRepo;
    @Inject
    DepRepo depRepo;


    @Get ("/")
    public String fun(){

        fakeService.fun();
        return "welcome back";
    }

    @Post("/add")
    public HttpResponse<?> fun(@Body Department dep){
        System.out.println(dep);
        depRepo.save(dep);
        return HttpResponse.status(HttpStatus.ACCEPTED).body("data add successfully");
    }
}

Upvotes: 2

Views: 681

Answers (1)

Guillaume
Guillaume

Reputation: 14656

Your Hibernate mapping says that the relationships are mapped by the 'many' side of the association:

@OneToMany(mappedBy = "department",cascade = CascadeType.ALL)
private List<Employee> employees;

So Hibernate looks for the value of the 'department' in the employee entity and it is null (because there's no value in the JSON data)

So try removing mappedBy to tell Hibernate that the relationship is mapped on the 'one' side

Upvotes: 0

Related Questions