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

时间:2012-10-12 14:26:35

标签: java hibernate jpa orm

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
  }

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

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

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

3 个答案:

答案 0 :(得分:13)

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

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

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

礼貌: one answer 谢谢, 拉杰什。

答案 1 :(得分:0)

请尝试使用@Column(name = "myprop")。另请注意,Type / Type2 / Type3必须是Simple类型(通常为Integer / Long / String / Date)。

答案 2 :(得分:0)

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