'Springboot inserting objects to API database on app startup that contain references to other objects
I've got a simple API with Company and Employee model with many-to-one relationship from employee point of view. I'm having hard time trying to insert new employees to the database on app startup because Company object needs to be referenced in Employee constructor and all my attempts so far have been unsuccessful due to app failing to start due to execptions. Here is the code with the latest attempt to insert the data. I tried to add company repo to the employee service and create objects there but I dont really think that's a good idea beacuse if I werent hard coding any objects I wouldn't really need to do that.
Company model:
package com.example.companiesapi.Entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Data
@Table(name = "Company")
@AllArgsConstructor
@NoArgsConstructor
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private long id;
@Column(name = "NAME")
private String name;
}
Employee model:
package com.example.companiesapi.Entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import javax.persistence.*;
@Entity
@Data
@Table(name = "Employee")
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private long ID;
@Column(name = "NAME")
private String name;
@Column(name = "SURNAME")
private String surname;
@Column(name = "EARNINGS")
private float earnings;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "company_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Company company;
}
Employee service:
package com.example.companiesapi.Services;
import com.example.companiesapi.Entities.Company;
import com.example.companiesapi.Entities.Employee;
import com.example.companiesapi.Repository.CompanyRepo;
import com.example.companiesapi.Repository.EmployeeRepo;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
@Service
public class EmployeeService {
final
EmployeeRepo employeeRepo;
final
CompanyRepo companyRepo;
public EmployeeService(EmployeeRepo employeeRepo, CompanyRepo companyRepo) {
this.employeeRepo = employeeRepo;
this.companyRepo = companyRepo;
}
@PostConstruct
private void postConstruct(){
List<Company> companies = Arrays.asList(new Company(1, "Pollbud"),new Company(2, "Maxbud") );
companyRepo.saveAll(companies);
List<Employee> employees = Arrays.asList(new Employee(1, "Piotr", "Wcislo", 4000,companyRepo.getById(1L))
,new Employee(2, "Anna", "Nowak", 7000, companyRepo.getById(1L)));
employeeRepo.saveAll(employees);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
