【问题标题】:getting error 'Invalid Column' in JPA while Fetching data using Join tables使用联接表获取数据时在 JPA 中出现错误“无效列”
【发布时间】:2021-09-15 18:13:16
【问题描述】:

我在运行我的应用程序时收到 Invalid Column name 错误。以下是附加的日志和实体以及 repo 类。

当我运行 Select * 查询时它可以工作,但当我选择某些列时它不起作用。

select * from reorg_view_x x join resource_hierarchy rh on (rh.resource_id=x.faoi_rid)where not exists (select 1 from resource_hierarchy where resource_name =x.t_aoi)and x.region= :region and trunc (x.reorg_dt) = :reorgDt

我不能使用 Select * 因为我只需要一些列名用于我的应用程序。请只使用 JPA。

错误日志

Hibernate: select distinct x.t_aoi resourceName, x.faoi_prid parentResourceId, rh.time_zone timeZone, rh.language language, to_date(created_dttm) from reorg_view_x x join resource_hierarchy rh on (rh.resource_id=x.faoi_rid)where not exists (select 1 from resource_hierarchy where resource_name =x.t_aoi)and x.region= ? and trunc (x.reorg_dt) = ?
{"logType":"DEBUG","logLevel":"WARN","logTimestamp":"2021-09-15T12:58:11.696-05:00","logger":"org.hibernate.engine.jdbc.spi.SqlExceptionHelper","label":"SQL Error: 17006, SQLState: 99999","runtime":{"instance":"unknown","clusterName":"unknown","namespace":"unknown","image":"unknown","platformIdentifier":"AJSC7_JERSEY"},"application":{"deploymentUnitName":"com.att.dmp.ReorgResourceCreatorMs","motsApplicationAcronym":"MOTS_ID"}}
{"logType":"DEBUG","logLevel":"ERROR","logTimestamp":"2021-09-15T12:58:11.697-05:00","logger":"org.hibernate.engine.jdbc.spi.SqlExceptionHelper","label":"Invalid column name","runtime":{"instance":"unknown","clusterName":"unknown","namespace":"unknown","image":"unknown","platformIdentifier":"AJSC7_JERSEY"},"application":{"deploymentUnitName":"com.att.dmp.ReorgResourceCreatorMs","motsApplicationAcronym":"MOTS_ID"}}
{"logType":"DEBUG","logLevel":"ERROR","logTimestamp":"2021-09-15T12:58:11.773-05:00","logger":"com.att.dmp.service.RestServiceImpl","label":"Exception in createAoi service:","runtime":{"instance":"unknown","clusterName":"unknown","namespace":"unknown","image":"unknown","platformIdentifier":"AJSC7_JERSEY"},"application":{"deploymentUnitName":"com.att.dmp.ReorgResourceCreatorMs","motsApplicationAcronym":"MOTS_ID"}}

我有实体:ResourceHierarchy 但没有 reorg_view_x



import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="resource_hierarchy")
public class ResourceHierarchy  {
    
      @Id
      @Column(name="RESOURCE_ID", nullable = false)
      private String resourceID;
      
      @Column(name="RESOURCE_NAME", nullable = false)
      private String resourceName;
      
      @Column(name="PARENT_RESOURCE_ID", nullable = false)
      private String parentResourceID;
      
      @Column(name="RESOURCE_TYPE", nullable = false)
      private String resourceType;
      
      @Column(name="TIME_ZONE", nullable = false)
      private String timeZone;
      
      @Column(name="LANGUAGE", nullable = false)
      private String language;
      
      @Column(name="EMAIL_ADDRESS")
      private String emailAddress;
      
      @Column(name="ORGANIZATION")
      private String organization;
      
      @Column(name="CREATED_BY")
      private String createdBy;
      
      @Column(name="CREATED_DTTM", nullable = false)
      private Date createdDttm;
      
      @Column(name="LAST_UPDATED_BY")
      private String lastUpdatedBy;
      
      @Column(name="LAST_UPDATED_DTTM")
      private Date lastUpdatedDttm;
      
      @Column(name="ACTIVE_FLAG")
      private String activeFlag;
      
      @Column(name="CAPACITY_TYPE")
      private String capacityType;
      
      @Column(name="DWA_FLAG")
      private String dwaFlag;

    public String getResourceID() {
        return resourceID;
    }

    public void setResourceID(String resourceID) {
        this.resourceID = resourceID;
    }

    public String getResourceName() {
        return resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    public String getParentResourceID() {
        return parentResourceID;
    }

    public void setParentResourceID(String parentResourceID) {
        this.parentResourceID = parentResourceID;
    }

    public String getResourceType() {
        return resourceType;
    }

    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }

    public String getTimeZone() {
        return timeZone;
    }

    public void setTimeZone(String timeZone) {
        this.timeZone = timeZone;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedDttm() {
        return createdDttm;
    }

    public void setCreatedDttm(Date createdDttm) {
        this.createdDttm = createdDttm;
    }

    public String getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(String lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    public Date getLastUpdatedDttm() {
        return lastUpdatedDttm;
    }

    public void setLastUpdatedDttm(Date lastUpdatedDttm) {
        this.lastUpdatedDttm = lastUpdatedDttm;
    }

    public String getActiveFlag() {
        return activeFlag;
    }

    public void setActiveFlag(String activeFlag) {
        this.activeFlag = activeFlag;
    }

    public String getCapacityType() {
        return capacityType;
    }

    public void setCapacityType(String capacityType) {
        this.capacityType = capacityType;
    }

    public String getDwaFlag() {
        return dwaFlag;
    }

    public void setDwaFlag(String dwaFlag) {
        this.dwaFlag = dwaFlag;
    }

    @Override
    public String toString() {
        return "ResourceHierarchy [resourceID=" + resourceID + ", resourceName=" + resourceName + ", parentResourceID="
                + parentResourceID + ", resourceType=" + resourceType + ", timeZone=" + timeZone + ", language="
                + language + ", emailAddress=" + emailAddress + ", organization=" + organization + ", createdBy="
                + createdBy + ", createdDttm=" + createdDttm + ", lastUpdatedBy=" + lastUpdatedBy + ", lastUpdatedDttm="
                + lastUpdatedDttm + ", activeFlag=" + activeFlag + ", capacityType=" + capacityType + ", dwaFlag="
                + dwaFlag + "]";
    }
      
      
      

}

我有 ResourceHierarchyRepository 像这样



import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.att.dmp.entity.ResourceHierarchy;

@Repository
public interface ResourceHierarchyRepository extends  JpaRepository<ResourceHierarchy, String> {

    
    @Query(value="select distinct x.t_aoi resourceName, x.faoi_prid parentResourceId, rh.time_zone timeZone, rh.language language, to_date(created_dttm) from reorg_view_x x join resource_hierarchy rh on (rh.resource_id=x.faoi_rid)where not exists (select 1 from resource_hierarchy where resource_name =x.t_aoi)and x.region= :region and trunc (x.reorg_dt) = :reorgDt", nativeQuery=true)      
    List<ResourceHierarchy> findAOI( @Param("reorgDt") String reorgDt,@Param("region") String region);
    

    
}

我是否还需要创建 reorg_view_x 实体,然后在 reorgViewX 存储库中获取记录,而不是在 ResourceHierarchy 中进行?但我认为还有其他一些简单的解决方案。

【问题讨论】:

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


    【解决方案1】:

    JPA 无法将少数选定的列直接转换为实体。现在你有两个选择:

    1. 如果您可以在JPQL 中编写您的查询,那么您可以使用构造函数来构建您的具有少数选定字段的对象。下面是最小的例子:

      @Query("SELECT new ResourceHierarchy(fieldOne, fieldTwo) FROM ResourceHierarchy WHERE ... ")
      public List<ResourceHierarchy> method(String params, ..);
      
    2. 如果要坚持使用native query,则必须将存储库方法的返回类型更改为List&lt;Map&lt;String, Object&gt;&gt;

    【讨论】:

    • 我这样做了` List> rh = resourceHierarchyRepository.findAOI(reorgDate,region);` 现在我得到的输出是org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@8a3c16e 而不是记录。
    • 我从这里调用 repo List&lt;Map&lt;String, Object&gt;&gt; rh = resourceHierarchyRepository.findAOI(reorgDate,region); rh.forEach(System.out::println);
    • 您不能直接打印Map。调试那个Map的内容。
    • Map,您将能够使用alias 作为键访问字段。
    • 执行此操作后,我能够获取记录,但它不允许我保存。我该如何解决这个问题?
    猜你喜欢
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2017-01-28
    • 1970-01-01
    • 2021-07-02
    • 2019-08-24
    • 1970-01-01
    相关资源
    最近更新 更多