I am using Java and Sprig boot with MySQL to create an employee tracker. there are to models: Employee and Company. The Company model just has an autogenerated Id and name. The employee has first_name, last_name, address, salary, company_id, and email_id. The Employee Model belongs to Company model, and it is a oneToMany relationship (because one company has many employees. I am trying to set it up so that the Employee model is linked to the Company model. When I try and run spring boot i get this error:
Use of @OneToMany or @ManyToMany targeting an unmapped class: employeeapps.com.example.EmployeeTracker.model.Employee.company[employeeapps.com.example.EmployeeTracker.model.Company]
Here is my Employee Model:
package employeeapps.com.example.EmployeeTracker.model;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "employees")
public class Employee {
//Company is one and has many employees
@OneToMany(mappedBy = "employees")
private Set<Company> company;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long companyId;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name="address")
private String address;
@Column(name="salary")
private Double salary;
@Column(name = "email_id")
private String emailId;
//default constructor
public Employee(){
}
//create a public constructor with the appropriate parameters to be able to create a new instance of an employee
public Employee(String firstName, String lastName, String emailId, String address, Double salary){
super();
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
this.salary = salary;
this.address = address;
}
//define methods
//long data type because could be a very long numerical data
public long getId() {
return companyId;
}
public void setId(long companyId){
this.companyId = companyId;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getEmailId(){
return emailId;
}
public void setEmailId(String emailId){
this.emailId = emailId;
}
public void setAddress(String address){
this.address = address;
}
public void setSalary(Double salary){
this.salary = salary;
}
Here is my Compay Model:
package employeeapps.com.example.EmployeeTracker.model;
import javax.persistence.*;
@Table(name = "company")
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
public Company(){
}
//public constructor
public Company(String name){
this.name = name;
}
}
Here is my employee controller to preform REST API:
package employeeapps.com.example.EmployeeTracker.controller;
import employeeapps.com.example.EmployeeTracker.model.Employee;
import employeeapps.com.example.EmployeeTracker.repository.employeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/v1")
public class EmployeeController {
@Autowired
private employeeRepository employeeRepository;
//REST APIS
//get all employees method name is get all
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return employeeRepository.findAll();
}
}
CodePudding user response:
In the company model give the model @Entity
annotation above @Table
annotation.
CodePudding user response:
Add below line to your system.properties file
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
In employees class use :
@OneToMany(targetEntity = Employee.class,cascade = ALL,fetch =
FetchType.LAZY)
@JoinColumn(name = "employee_id",referencedColumnName = "company_id")
private Set<Company> company;
CodePudding user response:
You should make the company an entity because you cannot make mapping without making the model class into the entity.Use @Entity to make company class entity above @Table(name = company). As you are trying to map from employees(many) entity to company(one)entity try @ManyToOne mapping in the employees entity by making company_ id a FOREIGN KEY in employee table
@ManyToOne
@JoinColumn(name="id",referencedColumnName="company_id")
private Set<Company> company;
In the above name represents id of company in company entity and referencedColumnName represents company_id in the employee entity.