Spring JPA联接两个没有三等舱的表

时间:2019-06-24 07:58:10

标签: java spring jpa spring-data-jpa jpql

有没有一种方法可以在Spring JPA中联接两个表而不使用关联类。

我有两个MySQL DB表: 员工(id,.....,department_id) 部门(id,.....) 而且我正在寻找一种仅使用员工和部门类来联接这些表的方法。

当前,我设法连接了两个表,但是使用了第三个关联类。 我当前的实现是:

员工类别:

@Entity(name = "Employee")
@Table(name = "employees")
@JsonInclude(Include.NON_NULL)
public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @JsonIgnore
    @Column(name = "employee_id")
    private Long employeeId;

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

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

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

    @Column(name = "hire_date")
    private Double hireDate;

    @Column(name = "job_id")
    private Long jobId;

    @Column(name = "salary")
    private Double salary;

    @Column(name = "commission_pct")
    private Double commissionPct;

    @Column(name = "employees")
    private Long employees;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", insertable = false, updatable = false)
    @Fetch(FetchMode.JOIN)
    private Department department;

}

部门课程:

@Entity(name = "Department")
@Table(name = "departments")
@JsonInclude(Include.NON_NULL)
public class Department implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

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

    @Column(name = "department_id")
    private long departmentId;

    @Column(name = "manager_id")
    private Double managerId;
    @Column(name = "location_id")
    private Double locationId;


}

关联类:

public class DeptEmpDto {
    private long departmentId;
    private String departmentName;
    private Double managerId;
    private Double locationId;
    private long employeeId;
    private String firstName;
    private String lastName;
    private String phoneNumber;
    private Double hireDate;
    private Long jobId;
    private Double salary;
    private Double commissionPct;

}

存储库:

public interface IEmployeeRepository extends JpaRepository<Employee, Long> {

    @Query("SELECT new com.concretepage.entity.DeptEmpDto(d.departmentId,d.departmentName,d.managerId,d.locationId,e.employeeId,e.firstName,e.lastName,e.phoneNumber,e.hireDate,e.jobId,e.salary,e.commissionPct FROM Employee e INNER JOIN Department d ON d.id = e.jobId")
    List<DeptEmpDto> fetchEmpDeptDataInnerJoin();

2 个答案:

答案 0 :(得分:0)

您可以在Employee类中使用它

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
        name = "DeptEmp",
        joinColumns = @JoinColumn(name = "emp_id",referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "dep_id",referencedColumnName = "id")
)
private Set<Departments> departments = new HashSet<>();

Look at this about JPA

答案 1 :(得分:0)

从逻辑上讲,员工不能在两个部门工作,所以您的关系是正确的

但是您可以使用from tensorflow.keras.preprocessing import image from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense, GlobalAveragePooling2D from tensorflow.keras import backend as K 注释来实现。

相关问题