【发布时间】:2020-01-13 08:32:06
【问题描述】:
我知道这里有类似的问题,但出于某种原因,它们对我不起作用。在这里,我什至找不到重复哪一列。该系统的基本思想是使用deptId(FK)上的join,我可以获得某个部门的所有员工。
下面我转载了相关代码:
Employee.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
+ "]";
}
}
这里是 Department.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 很陌生。请注意,我正在使用 H2 为 CRUD 操作制作 REST API。
【问题讨论】:
标签: java hibernate spring-boot jpa spring-data-jpa