今天碰到使用jpa进行多表查询的问题,需要同时查询仓库表(bas_stock)和物品表(bas_product),然后将两个表的一些字段显示在页面上,将解决过程记录下来。
1.创建实体类SelectStockClass,包含仓库表和物品表的字段。如车间班组号,仓库班组号是仓库表的字段,物资编码物品名称是物品表的字段。实体类创建需要无参构造方法和有参构造方法,用于实体类的转换,如果缺少构造方法会报异常。java.lang.NoSuchMethodException: com.mf.entity.SelectStockClass.(java.lang.String, java.math.BigDecimal, java.lang.String, java.lang.String)
package com.mf.entity;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class SelectStockClass implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private String productGuid;//物品guid
private BigDecimal quantity;//数量
private String classgroupNumber;//车间班组号
private String class_workshopDateTime;//车间班执行时间
private String classId;//仓库班组号
private String classNowDateTime;//仓库班组执行时间
private String productId;//物资编码
private String productName;//物资名称
public SelectStockClass(){
}
public SelectStockClass(String productGuid, BigDecimal quantity, String classgroupNumber,
String class_workshopDateTime, String classId, String classNowDateTime, String productId,
String productName) {
super();
this.productGuid = productGuid;
this.quantity = quantity;
this.classgroupNumber = classgroupNumber;
this.class_workshopDateTime = class_workshopDateTime;
this.classId = classId;
this.classNowDateTime = classNowDateTime;
this.productId = productId;
this.productName = productName;
}
public String getProductGuid() {
return productGuid;
}
public void setProductGuid(String productGuid) {
this.productGuid = productGuid;
}
public BigDecimal getQuantity() {
return quantity;
}
public void setQuantity(BigDecimal quantity) {
this.quantity = quantity;
}
public String getClassgroupNumber() {
return classgroupNumber;
}
public void setClassgroupNumber(String classgroupNumber) {
this.classgroupNumber = classgroupNumber;
}
public String getClass_workshopDateTime() {
return class_workshopDateTime;
}
public void setClass_workshopDateTime(String class_workshopDateTime) {
this.class_workshopDateTime = class_workshopDateTime;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getClassNowDateTime() {
return classNowDateTime;
}
public void setClassNowDateTime(String classNowDateTime) {
this.classNowDateTime = classNowDateTime;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@Override
public String toString() {
return "SelectStockClass [productGuid=" + productGuid + ", quantity=" + quantity + ", classgroupNumber="
+ classgroupNumber + ", class_workshopDateTime=" + class_workshopDateTime + ", classId=" + classId
+ ", classNowDateTime=" + classNowDateTime + ", productId=" + productId + ", productName=" + productName
+ "]";
}
}
2.创建SelectStockClassRepository类,用于实现多表查询。代码如下,select方法的sql语句从仓库表和物品表查询信息(注意:select查询语句的字段数量顺序应当与实体类的构造函数的参数数量顺序一致),返回对象类型为List,后面在service实现类中会做实体类转换。
package com.mf.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import com.mf.entity.SelectStockClass;
public interface SelectStockClassRepository extends JpaRepository, JpaSpecificationExecutor {
@Query(value="select count(*) from bas_stock sto,bas_product pro where sto.product_guid=pro.product_guid",nativeQuery=true)
public long getcount();
@Query(value="select sto.product_guid,sto.quantity,sto.classgroup_number,sto.class_workshop_date_time,sto.class_id,sto.class_now_date_time,pro.product_id,pro.product_name from bas_stock sto,bas_product pro where sto.product_guid=pro.product_guid",nativeQuery=true)
List
3.service层:SelectStockClassServiceImpl类中调用SelectStockClassRepository的方法返回一个List
public List list() {
List
4.实体类转换方法CastEntityUtil .
package com.mf.util;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
public class CastEntityUtil {
//转换实体类
public static List castEntity(List