Я знаю, что здесь есть похожие вопросы, но по некоторым причинам они просто не работают для меня. Здесь я даже не могу найти, какой столбец повторяется. Основная идея системы c заключается в том, что с помощью объединения в deptId (FK) я могу получить всех сотрудников в определенном отделе.
Я воспроизвел ниже соответствующий код:
Сотрудник. java
package com.tvlk.advDemo.model;
import javax.persistence.*;
import javax.swing.*;
import java.io.Serializable;
@Entity
@Table(name="employees")
public class Employee implements Serializable {
private long id;
private String name;
private String designation;
private long deptId;
private Department department;
public Employee() {
}
public Employee(String name, String designation, long deptId) {
this.name = name;
this.designation = designation;
this.deptId = deptId;
}
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "designation", nullable = false)
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
// @Column(name = "deptId", nullable = false)
public long getDeptId() {
return deptId;
}
public void setDeptId(long deptId) {
this.deptId = deptId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "deptId", nullable = false)
public Department getDepartment() {
return department;
}
public void setDepartment(Department department){
this.department = department;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", designation=" + designation + ", deptId=" + deptId
+ "]";
}
}
Вот отдел. java
package com.tvlk.advDemo.model;
import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
@Table(name="departments")
public class Department implements Serializable {
private long id;
private String deptName;
private String deptHead;
private long budget;
private List<Employee> employees;
public Department(){
}
public Department(long id, String deptName, String deptHead, long budget)
{
this.id = id;
this.deptHead = deptHead;
this.deptName = deptName;
this.budget = budget;
}
@Id
public long getId() {return id;}
public void setId(long id) {this.id = id; }
@Column(name = "name", nullable = false)
public String getDeptName(){return deptName;}
public void setDeptName(String deptName){this.deptName = deptName; }
@Column(name = "deptHead", nullable = false)
public String getDeptHead(){return deptHead; }
public void setDeptHead(String deptHead){this.deptHead = deptHead; }
@Column(name = "budget", nullable = false)
public long getBudget(){return budget;}
public void setBudget(long budget){this.budget = budget; }
@OneToMany(fetch = FetchType.LAZY, mappedBy = "id", targetEntity = Employee.class)
public List<Employee> getEmployees() {
return this.employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
Любая помощь будет оценена, я довольно новичок в Springboot и Hibernate. Обратите внимание, что я делаю REST API для операций CRUD, используя H2.