【问题标题】:Spring JPA Join two tables without third classSpring JPA 在没有第三类的情况下加入两个表
【发布时间】:2019-11-06 00:01:45
【问题描述】:

有没有办法在 Spring JPA 中连接两个表而不使用关联类。

我有两个 MySQL 数据库表: 员工(id,.....,department_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();

【问题讨论】:

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


【解决方案1】:

你可以在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

【讨论】:

  • 我正在寻找一种在存储库类中执行此操作的方法,并且只能使用员工类。例如使用这样的东西:@Query("SELECT new com.concretepage.entity.Employee(employeeId,firstName,lastName,salary) FROM Employee") 但也添加部门字段
  • 当您使用我的选项时,您将为您的员工班级添加一个新文件。之后,您的 ORM 将更新您在数据库中的类。之后,您可以通过选择 Employee 来获取部门
【解决方案2】:

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

但是您可以使用 @ManyToMany 注释来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2021-10-27
    • 2020-05-12
    • 2014-12-04
    • 1970-01-01
    • 2021-03-28
    • 2018-04-11
    相关资源
    最近更新 更多