简体   繁体   English

Hibernate 本机查询:无效的列名错误 SQL-17006

[英]Hibernate native query : Invalid Column Name Error SQL-17006

package com.abc.def.model;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Embeddable;
import javax.persistence.IdClass;
import java.util.Date;
import java.io.Serializable;



@NamedNativeQuery(name="getMetadata",query="
                  select a.name alias1,a.fullname alias2,
                         b.name alias3,b.age alias4,
                         c.height alias5,c.something alias6,
                         d.otherthing alias7
                  from lame_table_name a,
                       lame_table_name_2 b
                  where a.id = b.id
                     and b.id = c.id 
                     and c.id = d.id 
                     and d.id = :namedparameter
                  order by a.index,b.index
               ",
            resultClass=MetadataModel.class)


  @Entity
  @IdClass(SomeIdClass.class)

  public class MetadataModel{

  @Id @Column("alias1")
  private Type alias1property;

  @Id @Column("alias2")
  private Type2 alias2property;

  @Column("alias3")
  private Type3 alias3property;

  //getters and setters
  }

  @Embeddable
  class SomeIdClass implements Serializable{

  //serialVersionUID line

  @Id @Column("alias1")
  private Type alias1property;

  @Id @Column("alias2")
  private Type2 alias2property;

  //getter and setters
  }

The error is SQL-17006, Invalid Column Name, have been trying out variations of this setup the whole day Should I try putting Column("lame_table_name.name")错误是 SQL-17006,无效的列名,整天都在尝试此设置的变体我应该尝试放置 Column("lame_table_name.name")

I also tried using SqlResultSetMapping (and removed @Column from fields of POJO) (and specifying all the column aliases in the columns attribute of SqlResultSetMapping) (are we supposed to specify the resultsetmapping again when executing the query via the setResultSetMapping method of the SQLQuery interface?)我还尝试使用SqlResultSetMapping(并从POJO的字段中删除@Column)(并在SqlResultSetMapping的columns属性中指定所有列别名)(我们是否应该在通过SQLQuery接口的setResultSetMapping方法执行查询时再次指定resultsetmapping ?)

package com.abc.def.model;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Embeddable;
import javax.persistence.IdClass;
import java.util.Date;
import java.io.Serializable;
//other imports for the SqlResultSetMapping



@NamedNativeQuery(name="getMetadata",query="
                  select a.name alias1,a.fullname alias2,
                         b.name alias3,b.age alias4,
                         c.height alias5,c.something alias6,
                         d.otherthing alias7
                  from lame_table_name a,
                       lame_table_name_2 b
                  where a.id = b.id
                     and b.id = c.id 
                     and c.id = d.id 
                     and d.id = :namedparameter
                  order by a.index,b.index
               ",
            resultSetMapping="metaDataMapping")


@SqlResultSetMapping(name="metaDataMapping",
              entities=@EntityResult(entityClass=MetadataModel.class,
                fields = {@FieldResult(name="alias1Property",column="alias1")
                           //so on
                      }

                 )
            )

  @Entity
  @IdClass(SomeIdClass.class)

  public class MetadataModel{


  private Type alias1property;


  private Type2 alias2property;


  private Type3 alias3property;

  //getters and setters
  }

  //composite class, exactly as above

We should have all the table columns in the Select list for oracle.. If we keep only few columns. 我们应该在oracle的Select列表中包含所有表列。如果我们只保留几列。 Eg, your table Employee has columns FirstName,LastName, EmpId, and if you have query like . 例如,您的表Employee具有FirstName,LastName,EmpId列,如果您有查询类似。

session.createSQLQuery("Select FirstName from Employee");

the above query won't work. 以上查询将无法正常工作。 it will throw Invalid column error Exception. 它将抛出无效列错误异常。 So better put all the columns in Select clause for Oracle. 因此,最好将所有列放在Oracle的Select子句中。

Courtesy : one answer Thanks, Rajesh. 礼貌: 一个答案谢谢,Rajesh。

Try @Column(name = "myprop") instead. 请尝试@Column(name = "myprop") Also note that Type/Type2/Type3 must be Simple types (Integer/Long/String/Date usually). 另请注意,Type / Type2 / Type3必须是Simple类型(通常为Integer / Long / String / Date)。

好吧,早些时候我试图在resultsetmapping中指定列和实体属性,所以我尝试删除实体映射,保留columns属性,并调用aliastobean结果转换器,加上编写setter来接受BigDecimal而不是Long(从那以后)它是一个Oracle DB),解决了这个问题......

In my case, my I needed to give an inner-select body a name, as it was mysteriously truncating away a CASE column.就我而言,我需要为内部选择主体命名,因为它神秘地截断了 CASE 列。

The following did not work - for some reason COLUMN_B gets mysteriously removed:以下不起作用 - 由于某种原因 COLUMN_B 被神秘地删除:

SELECT
  COLUMN_A,
  CASE WHEN ... THEN ... ELSE ... END as COLUMN_B,
  COLUMN_C
FROM (
   ...
)

Then following fixed the problem:然后以下解决了问题:

SELECT
  RES.COLUMN_A,
  CASE WHEN ... THEN ... ELSE ... END as COLUMN_B,
  RES.COLUMN_C
FROM (
   ...
) RES

The whole SQL is only dealing with one table.整个 SQL 只处理一张表。

Java code: Java代码:

// The "sql" variable is the SQL statement shown above.
javax.persistence.Query res = myEntityManager.createNativeQuery(sql, SomeClass.class);

🙄 🙄

I'm using Spring Boot 2.4.3 with JPA and Oracle 12c.我正在使用带有 JPA 和 Oracle 12c 的 Spring Boot 2.4.3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM