Spring / JPA / Hibernate如何在一个存储库中执行两个实体的连接

时间:2017-05-11 17:54:09

标签: java hibernate jpa

首先我们说我们有两张桌子。一个表是 Employee 表,其中包含以下列:

EMPLOYEE:
------------------------
emp_id (int, primary key)
emp_name (varchar(125))
emp_dept (foreign key)
emp_intro (text)

另一个表是部门表,其中包含以下列:

DEPARTMENT:
-----------
dept_id (int, primary key)
dept_label (varchar(25))

以下是表值的示例

DEPARTMENT:
------------------------
dept_id    | dept_label
------------------------
 1         | Sales
------------------------
 2         | Technology
------------------------
 3         | Finance

为了使用状态标签返回员工的信息,我们需要执行JOIN:

SELECT e, d.dept_label FROM employees JOIN department d ON d.dept_id = e.emp_dept

或多表选择:

SELECT e.emp_id, e.emp_name, d.dept_label, e.emp_intro FROM employees e, department d WHERE e.emp_dept = d.dept_id

但是,在使用JPA / Hibernate时,我们需要创建两个类:

Employee.java

package com.example.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "employees")
public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "emp_id")
    private long emp_id;

    @Column(name = "emp_name")
    private String emp_name;

    @Column(name = "emp_dept")
    private Integer emp_dept;

    @Column(name = "emp_intro")
    private String emp_intro;

    public long getEmp_id() {
        return emp_id;
    }

    public void setEmp_id(long emp_id) {
        this.emp_id = emp_id;
    }

    public String getEmp_name() {
        return emp_name;
    }

    public void setEmp_name(String emp_name) {
        this.emp_name = emp_name;
    }

    public Integer getEmp_dept() {
        return emp_dept;
    }

    public void setEmp_dept(Integer emp_dept) {
        this.emp_dept = emp_dept;
    }

    public String getEmp_intro() {
        return emp_intro;
    }

    public void setEmp_intro(String emp_intro) {
        this.emp_intro = emp_intro;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

Department.java

package com.example.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "departments")
public class Department implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "dept_id")
    private long dept_id;

    @Column(name = "dept_label")
    private String dept_label;

    public long getDept_id() {
        return dept_id;
    }

    public void setDept_id(long dept_id) {
        this.dept_id = dept_id;
    }

    public String getDept_label() {
        return dept_label;
    }

    public void setDept_label(String dept_label) {
        this.dept_label = dept_label;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

然后,有存储库(DAO):

EmployeeRepository

package com.example.repository;

import.java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.example.entities.Employee;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

    @Query("select e, d.dept_label FROM Employee e JOIN Department d ON "
    + "d.dept_id = e.emp_id")
    public List<Employee> return getEmployees();

}

最后,Java控制器将分类查询绑定到应用程序的端点:

EmployeeController.java

package com.example.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.entities.Department;
import com.example.entities.Employee;
import com.example.repository.EmployeeRepository;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeRepository er;

    @RequestMapping(value = "/getEmployees")
    public @ResponseBody List<Employee> getEmployees() {
        return er.getEmployees();
    }

}

我已经测试了整个结构,只检索了Employee表中的行(即@Query(“SELECT e FROM Employee e”)),一切都按原样返回。

MY MAIN ISSUE 是如何在查询位于特定类(表)内部时返回JOIN QUERY,如果我需要Department内的内容,则是Employee吗?

我已经尝试过@JoinColumn注释并且效果不佳(也许我做错了)。

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:2)

您不必使用原始联接来执行此操作,只需使用正确的关系映射。 EmployeeDepartament之间的关系听起来像@ManyToOne@ManyToMany。 您可以employee.getDepartament()employee.departament.name=:name

查询

http://www.objectdb.com/api/java/jpa/ManyToMany

您甚至可以映射双向关系,这样您就可以从员工以及所有员工中获得贬低

PS。 @JoinColumn用于对用于连接的DB columnt进行delare,它与选择的命名策略(usualy entityname_id)创建的不同。实际关系映射是通过声明@OneToOne @OneToMany @ManyToMany完成的,但这些可以但不必与@JoinColumn一起使用。这是严格的JPA问题。

Here you have complete documentation of JPA 2.1 specification 它详细描述了如何声明关系以及@MappedSuperclass,继承策略和所有其他有用的东西。