in spring boot how to insert Data to joined table after joining two tables

440 Views Asked by At

i have created a spring boot project with two tables Employee and Department which i want to join it by many employee and can have one department i have made the @onetomany and @manytoone in the respective tables i get the column in Employee Table to add the department id as a foreign key in the Employee table but the problem is when i use the controller to add the table with its required column data the the foreign key column in my case which is "departmentId" cannot be added using the regular spring jpa

Department Table

@Entity
@Table(name = "department")
@Setter
@Getter
public class Department  {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "name")
    private String name;
    
    @Column(name = "description")
    private String description;

    
    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL,orphanRemoval = true ,fetch = FetchType.LAZY,mappedBy = "departmentId")
    private Set<Employee> employee = new HashSet<Employee>(0);
    
    
}

This is the Employee table which uses department id as a foreign key in it 

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


@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name")
private String name;

@Column(name = "email")
private String email;

@Column(name = "address")
private String address;

@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL)//ManyToOne since many Employee can have one Department 
@JoinColumn(name ="departmentId"  )
private Department departmentId;

}`
2

There are 2 best solutions below

0
Isakots On BEST ANSWER

the problem is when i use the controller to add the table with its required column data the the foreign key column in my case which is "departmentId" cannot be added using the regular spring jpa

Why is it cannot be added? Please provide some code, how do you try to add a Department entity to Employee entity or how do you try to save these entites and connections. You should provide the Controller endpoints as well. Anyway I can provide some hints based on your description.

  1. with @ManyToOne relation, you can logically connect an entity to another one. therefore you must provide the whole entity not just a foreign key. If the necessary Department entity is already persisted, you can query it from database with findById() or getReferenceById() methods
  2. if your Controller endpoint receives the full entity object (either Employee or Department) the connection will be ignored by Jackson during deserialization, since you annotated them with @JsonIgnore
0
johnnyutts On

It should be as simple as fetching the Department from the DB using the department id and setting the department into the Employee entity before persisting.

Also, I would advised you to rename the departmentId field in the Employee entity to department for clarity.

@Service
public class EmployeeService {

    @Autowired
    private EntityManager em;

    @Transactional
    public void addEmployee(Employee employee, Long departmentId) {
        Department department = em.find(Department.class, departmentId);
        employee.setDepartment(department);
        em.persist(employee);
    }
}