首先讓我們假設我們有兩個表。一個表是員工表具有以下的列:Spring/JPA/Hibernate如何在一個倉庫中執行兩個實體的聯接
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;
}
}
出發ment.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」))並且所有內容都按原樣返回。
我的主要問題是如何在查詢位於特定類(表)的內部時返回JOIN QUERY,如果我需要Department內部的內容,則爲Employee?
我已經嘗試@JoinColumn註解,並沒有工作(也許我做錯了)。
任何想法?謝謝。
面嚮對象語言允許對象引用形成對象之間的關係。你沒有這些。一個關係不是很長,恰好代表某個其他類的某個對象,它應該是一個對象引用!這是在OO級別,甚至不在JPA級別 –