使用eclipselink和@namedquery进行错误转换

时间:2014-05-12 19:28:25

标签: java java-ee jpa eclipselink named-query

有一段时间我在eclipselink映射中遇到了问题。

我有两个班,Acordo和ValorAcordo。

所以我有

@ Entity
    @ Table ( name = " TB_ACORDO " , schema = " JUR " )
    @ NamedQueries ( {
    @ NamedQuery ( name = " sql1 "
    query = " SELECT a.NU_ACORDO , a.NU_MOVIMENTACAO , v.NU_CONTRATO "
    + "FROM Acordo"
    + " INNER JOIN ValorAcordo v"
    + "ON a.NU_ACORDO = v.NU_ACORDO "
    + " WHERE = a.NU_MOVIMENTACAO : nu_movimentacao " )
    } )
    public class Acordo implements Serializable {

    private static final long serialVersionUID = - 6202293050832719017L ;

    @ Id
    @ GeneratedValue ( strategy = GenerationType.IDENTITY )
    private Long NU_ACORDO ;

    private Long NU_MOVIMENTACAO ;
    private Long VR_TOTAL ;

    @ OneToOne ( fetch = FetchType.LAZY )
    private ValorAcordo valorAcordo ;
    // ***

@ Entity
@ Table ( name = " TB_VALOR_ACORDO " , schema = " JUR " )
public class ValorAcordo implements Serializable { 
private static final long serialVersionUID = 3024868010888830368L ;

@ Id
@ GeneratedValue ( strategy = GenerationType.IDENTITY )
private Long NU_VALOR_ACORDO ;
private Long NU_ACORDO ;
private Long NU_CONTRATO ;
private Long NU_PARTE ;
private Long QT_PARCELA ;
private Long VR_ACORDO ;

@ Temporal ( TemporalType.TIMESTAMP )
private Date DT_PRIMEIRA_PARCELA ;
@ Temporal ( TemporalType.TIMESTAMP )
private Date DT_ULTIMA_PARCELA ;

@ OneToOne ( fetch = FetchType.LAZY )
private acordo;
// ***

当我跑

Query q = em.createNamedQuery ( " sql1 " Acordo.class )
. setParameter ( " nu_movimentacao " 3103461 );
List <Acordo> q.getResultList list = ( ) ;

EclipseLink返回以下错误:

Exception in thread " main" java.lang.ClassCastException : [ Ljava.lang.Object ; can not be cast to br.victor.des.bean.Acordo
at br.victor.des.main.MainAcordo.main ( MainAcordo.java : 46)

任何想法?

我认为这是一个映射问题,但发现了一个链接eclipsebug,其中一些开发人员报告了这个错误。但是他们在简单查询时遇到了这个问题,我只有在使用JOIN时才会遇到这个问题!

谢谢

1 个答案:

答案 0 :(得分:0)

查询sql1的定义为&#34;选择a.NU_ACORDO,a.NU_MOVIMENTACAO,v.NU_CONTRATO ..&#34; - 它从Acordo中选择3个字段,而不是完整实例,因此JPA为您提供了包含每行3个值的对象数组列表。如果您想要完整的实体,请将查询更改为&#34; SELECT a FROM Acordo a ...&#34;代替。