关于Spring Data JPA 多表查询 返回自定义Vo的问题记录

这两天开了一个新项目,使用SpringBoot+SpringData,  刚做了一个小功能,都是一张表的操作没什么问题,今天设计到了两张表联查,两张表各取了几个字段,组合成了一个vo, 当我用原生sql查询时报出 “找不到转换器”,当我用JPQL查询时,报出 xxx is not mapped.。 着实浪费了些时间。  其实最大的根本原因在于, 当使用JPQL 查询时,我们 FROM 的表名就不能是实体上@Table 注解中的表名,而是对应的实体的类名,记住是类名,并且查询的字段要是 实体类的属性,而不是数据库字段。切记。下面是我的代码

package com.wisdombud.dama.quality.datasource.pojo;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.ToString;

@Entity
@Table(name = "DATA_SOURCE")
@SequenceGenerator(name = "ID_SEQ", sequenceName = "SEQ_DATA_SOURCE", allocationSize = 1)
@ToString
public class DataSourcePojo implements Serializable{

    private static final long serialVersionUID = 4658654420795590006L;
    
    private Long id;
    
    @NotBlank(message = "数据源名称不能为空!")
    private String name;
    
    @NotBlank(message = "ip地址不能为空!")
    private String ip;
    
    @NotBlank(message = "端口号不能为空!")
    private String port;
    
    @NotBlank(message = "服务名不能为空!")
    private String serviceName;
    
    @NotBlank(message = "用户名不能为空!")
    private String userName;
    
    @NotBlank(message = "密码不能为空!")
    private String password;
    private String managerBranch;
    private String leadingCadre;
    private String phone;
    private Long sortIndex;
    private String remark;
    private Long createUserId;
    private String createUserName;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
    private Date lastUpdateTime;
    private String re1;
    private String re2;
    private String re3;
    private String re4;
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_SEQ")
    @Column(name = "ID", unique = true, nullable = false, precision = 18, scale = 0)
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    
    @Column(name = "NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Column(name = "IP")
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    
    @Column(name = "PORT")
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    
    @Column(name = "SERVICE_NAME")
    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    
    @Column(name = "USER_NAME")
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    @Column(name = "PASSWORD")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    @Column(name = "MANAGER_BRANCH")
    public String getManagerBranch() {
        return managerBranch;
    }
    public void setManagerBranch(String managerBranch) {
        this.managerBranch = managerBranch;
    }
    
    @Column(name = "LEADING_CADRE")
    public String getLeadingCadre() {
        return leadingCadre;
    }
    public void setLeadingCadre(String leadingCadre) {
        this.leadingCadre = leadingCadre;
    }
    
    @Column(name = "PHONE")
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    
    @Column(name = "SORT_INDEX")
    public Long getSortIndex() {
        return sortIndex;
    }
    public void setSortIndex(Long sortIndex) {
        this.sortIndex = sortIndex;
    }
    
    @Column(name = "REMARK")
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    @Column(name = "CREATE_USER_ID")
    public Long getCreateUserId() {
        return createUserId;
    }
    public void setCreateUserId(Long createUserId) {
        this.createUserId = createUserId;
    }
    
    @Column(name = "CREATE_USER_NAME")
    public String getCreateUserName() {
        return createUserName;
    }
    public void setCreateUserName(String createUserName) {
        this.createUserName = createUserName;
    }
    
    @Column(name = "CREATE_TIME")
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    
    @Column(name = "LAST_UPDATE_TIME")
    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }
    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }
    
    @Column(name = "RE1")
    public String getRe1() {
        return re1;
    }
    public void setRe1(String re1) {
        this.re1 = re1;
    }
    
    @Column(name = "RE2")
    public String getRe2() {
        return re2;
    }
    public void setRe2(String re2) {
        this.re2 = re2;
    }
    
    @Column(name = "RE3")
    public String getRe3() {
        return re3;
    }
    public void setRe3(String re3) {
        this.re3 = re3;
    }
    
    @Column(name = "RE4")
    public String getRe4() {
        return re4;
    }
    public void setRe4(String re4) {
        this.re4 = re4;
    }
}
package com.wisdombud.dama.quality.datasource.tablerelation.pojo;

import java.io.Serializable;
import java.util.Date;

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

import lombok.ToString;

/**
 * 表关系实体
 * @author qiaoyutao
 * @Date: 2019年7月4日 上午11:40:10
 */
@Entity
@Table(name = "TABLE_RELATION")
@SequenceGenerator(name = "ID_SEQ", sequenceName = "SEQ_TABLE_RELATION", allocationSize = 1)
@ToString
public class TableRelationPojo implements Serializable{

    private static final long serialVersionUID = -9058973373224769393L;
    
    private Long id;
    private Long dataSourceId;
    private String tableName;
    private String columnName;
    private String relationTableName;
    private String relationColumnName;
    private String remark;
    private Long createUserId;
    private String createUserName;
    private Date createTime;
    private Date lastUpdateTime;
    private String re1;
    private String re2;
    private String re3;
    private String re4;
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_SEQ")
    @Column(name = "ID", unique = true, nullable = false, precision = 18, scale = 0)
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    
    @Column(name = "DATA_SOURCE_ID")
    public Long getDataSourceId() {
        return dataSourceId;
    }
    public void setDataSourceId(Long dataSourceId) {
        this.dataSourceId = dataSourceId;
    }
    
    @Column(name = "TABLE_NAME")
    public String getTableName() {
        return tableName;
    }
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
    
    @Column(name = "COLUMN_NAME")
    public String getColumnName() {
        return columnName;
    }
    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }
    
    @Column(name = "RELATION_TABLE_NAME")
    public String getRelationTableName() {
        return relationTableName;
    }
    public void setRelationTableName(String relationTableName) {
        this.relationTableName = relationTableName;
    }
    
    @Column(name = "RELATION_COLUMN_NAME")
    public String getRelationColumnName() {
        return relationColumnName;
    }
    public void setRelationColumnName(String relationColumnName) {
        this.relationColumnName = relationColumnName;
    }
    
    @Column(name = "REMARK")
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    @Column(name = "CREATE_USER_ID")
    public Long getCreateUserId() {
        return createUserId;
    }
    public void setCreateUserId(Long createUserId) {
        this.createUserId = createUserId;
    }
    
    @Column(name = "CREATE_USER_NAME")
    public String getCreateUserName() {
        return createUserName;
    }
    public void setCreateUserName(String createUserName) {
        this.createUserName = createUserName;
    }
    
    @Column(name = "CREATE_TIME")
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    
    @Column(name = "LAST_UPDATE_TIME")
    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }
    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }
    
    @Column(name = "RE1")
    public String getRe1() {
        return re1;
    }
    public void setRe1(String re1) {
        this.re1 = re1;
    }
    
    @Column(name = "RE2")
    public String getRe2() {
        return re2;
    }
    public void setRe2(String re2) {
        this.re2 = re2;
    }
    
    @Column(name = "RE3")
    public String getRe3() {
        return re3;
    }
    public void setRe3(String re3) {
        this.re3 = re3;
    }
    
    @Column(name = "RE4")
    public String getRe4() {
        return re4;
    }
    public void setRe4(String re4) {
        this.re4 = re4;
    }
}

下面是我的dao层,重点

package com.wisdombud.dama.quality.datasource.tablerelation.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import com.wisdombud.dama.quality.datasource.tablerelation.pojo.TableRelationPojo;
import com.wisdombud.dama.quality.datasource.tablerelation.vo.TableRelationGridVo;

public interface TableRelationDao extends JpaRepository, JpaSpecificationExecutor{
    
    // 这里使用JPQL来写的, 区别就是 new 一个vo名,最好带上全路径,并且vo中要有有参构造器,构造器签名要与查询的字段一致, 查询的属性都是实体类中的属性名,不是数据库的字段名,这点切记。
    @Query(value = "SELECT new com.wisdombud.dama.quality.datasource.tablerelation.vo.TableRelationGridVo(re.id, "
            + "ds.name, re.tableName, re.columnName, "
            + "re.relationTableName, re.relationColumnName, "
            + "re.createTime) "
            + "FROM TableRelationPojo re left join DataSourcePojo ds on re.dataSourceId = ds.id")
    public Page page(Pageable pageable);
    
}

希望能够帮到跟我一样因为这一个,加班到9点的你。  转载请注明出处

转载于:https://www.cnblogs.com/qiaoyutao/p/11134794.html

你可能感兴趣的:(关于Spring Data JPA 多表查询 返回自定义Vo的问题记录)