0

For some time I have a problem in eclipselink mapping .

I have two classes , Acordo and ValorAcordo.

so I have

@ 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 ;
    // ***

and

@ 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;
// ***

and when I run

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

EclipseLink returns me the following error :

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)

any ideas?

I think it's a mapping problem, but found a link eclipsebug where some developers reported this error. But they had this problem with simple queries, I have this problem only when I use JOIN!

thank you

petrov
  • 11

1 Answers1

1

The query sql1 has is defined as " SELECT a.NU_ACORDO , a.NU_MOVIMENTACAO , v.NU_CONTRATO.."- it is selecting 3 fields from Acordo, not a full instance, so JPA is giving you a list of object arrays containing the 3 values for each row. If you want full entities, change the query to be "SELECT a FROM Acordo a..." Instead.

Chris
  • 20,138
  • 2
  • 29
  • 43
  • i changed to "SELECT a, v ..." because i need the values of both tables! but eclipselink generate a strange SQL... they select all columns TB_Acordo and TB_ValorAcordo but eclipselink create de column "ACORDO_NU_ACORDO"! oO that not exist! take a look! "SELECT t0.*, t1.*, t1.ACORDO_NU_ACORDO FROM JUR.TB_ACORDO t0, JUR.TB_VALOR_ACORDO t1 WHERE ((t0.NU_MOVIMENTACAO = ?) AND (t0.NU_ACORDO = t1.NU_ACORDO))" and error from Oracle "'T1' 'ACORDO_NU_ACORDO.':Invalid identifier" – petrov May 13 '14 at 13:37
  • Your question has a classCastException that is from using 3 fields. If you have other issues you want resolved, or some other underlying problem that made you design your system this way, we can't read your mind. Create a question with your actual problem and someone might be able to help. – Chris May 13 '14 at 13:55
  • In this case, your mappings do not match what you seem to have in the database. For instance, your query implies ValorAcordo has a foreign key to Acordo, but the 1:1 mapping in Acordo defaults to using a foreign key in Acordo that points to ValorAcordo which I assume doesn't exist, and the ValorAcordo->Acordo creates yet another foreign key that the error states doesn't exist rather than use the NU_ACORDO field which I assume is the intended foreign key. Try starting with a simple JPA example and then expand to your model – Chris May 13 '14 at 13:56