2012-10-12 66 views
2
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,无效的列名,已经尝试了这种设置的变化整天 我应该尝试把列(“lame_table_name.name “)休眠本地查询:无效列名称错误SQL-17006

我也尝试过使用SqlResultSetMapping(并从POJO的字段中删除了@Column)(并且指定了SqlResultSetMapping的columns属性中的所有列别名)(我们是否应该在执行查询时再次指定resultsetmapping 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", 
       [email protected](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 

回答

0

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

0

尝试使用@Column(name = "myprop")。还要注意Type/Type2/Type3必须是简单类型(通常是整数/长/字符串/日期)。

+0

是的,它们都是Long,String,Date的其中之一 –

7

我们应该在Oracle的Select列表中包含所有表列。 如果我们只保留几列。 例如, 您的表员工具有列FirstName,LastName,EmpId, ,如果您有查询。

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

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

致谢: one answer 谢谢, Rajesh。

+0

谢谢。我几乎花了整整一天的时间解决了这个问题,最终在这里得到了解决方案 –

+0

欢迎Derick。 – Rajesh