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

cee*_*_el 4 java orm hibernate jpa

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
  }
Run Code Online (Sandbox Code Playgroud)

错误是SQL-17006,无效的列名,一整天都在试用这个设置的变体我应该尝试放置Column("lame_table_name.name")

我也尝试使用SqlResultSetMapping(并从POJO的字段中删除@Column)(并在SqlResultSetMapping的columns属性中指定所有列别名)(我们应该在通过SQLQuery接口的setResultSetMapping方法执行查询时再次指定结果集映射) ?)

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
Run Code Online (Sandbox Code Playgroud)

小智 15

我们应该在oracle的Select列表中包含所有表列.如果我们只保留几列.例如,您的表Employee具有FirstName,LastName,EmpId列,如果您有查询类似.

session.createSQLQuery("Select FirstName from Employee");
Run Code Online (Sandbox Code Playgroud)

以上查询将无法正常工作.它将抛出无效列错误异常.因此,最好将所有列放在Oracle的Select子句中.

礼貌: 一个答案 谢谢,Rajesh.


cee*_*_el 0

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