You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by holod <se...@gmail.com> on 2008/11/21 17:59:16 UTC
Can;t map AVG sql field to class
Goog day, I'm using openjpa 1.0.3 (Unfortunately we use IBM WebSphere App
Server, their fix-packs comes out lately) and IBM DB2 9.5
I have a query:
StringBuilder sb = new StringBuilder();
sb.append("SELECT avg(timestampdiff(256, char(current timestamp -
p.birthdaydate))) as s5 FROM Person as p");
Query query = em.createNativeQuery(sb.toString(), InformerRow.class);
InformerRow ir = (InformerRow)query.getSingleResult();
And this is my class InformerRow:
public class InformerRow implements Serializable{
private static final long serialVersionUID = 1L;
public String s1;
public long s2;
public String s3;
public long s4;
public int s5;
public InformerRow(){
super();
}
public InformerRow(int s5){
this.s5 = s5;
}
public InformerRow(Integer s5){
this.s5 = s5;
}
public InformerRow(long s4, long s2){
this.s4=s4;
this.s2=s2;
}
public InformerRow(Long s4, Long s2){
this.s4=s4;
this.s2=s2;
}
public InformerRow(Number s2){
this.s2 = s2.longValue();
}
public InformerRow(long s2){
this.s2=s2;
}
}
I've tried everything, to map avg(timestampdiff(256, char(current timestamp
- p.birthdaydate))) as long, as Number, as int nothing helps.
Unfortunately IBM WebSphere prints exceptions in Russian (absolutely crazy
idea to localize exceptions. Inventor of this "feature" is very-very ill),
so it prints something like:
"Exception raised during packing result in class InformerRow".
I see me query in trace.log, so it works fine, but JPA can't map result on
InformerRow field.
I don't know what to try?
Please, suggest me something.
--
View this message in context: http://n2.nabble.com/Can-t-map-AVG-sql-field-to-class-tp1562523p1562523.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Can;t map AVG sql field to class
Posted by holod <se...@gmail.com>.
Pinaki Poddar wrote:
>
> So try something like:
> Query query = em.createNativeQuery(sb.toString());
> Object result = query.getSingleResult();
> InformerRow ir = new InformerRow();
> ir.s5 = ((Number)result).intValue();
>
>
Wow! It works! Great!
So, please can you tell me why this didn't work:
Query query = em.CreateNativeQuery(sb.toString(), InformerRow.class);
InformerRow ir = (InformerRow)query.getsingleResult();
and my InformerRow class:
public class informerRow{
//...
//...
private long s4;
private Number s5;
public InformerRow(){
super();
}
public InformerRow(Number s5){
super();
this.s5 = s5;
}
public InformerRow(Number s4){
super();
this.s4 = s4;
}
//public getters and setters for s5 and s4
}
The same thing for s4 worked fine when query was: "select count(t.someField
from schema.table t where ...)"
I' don't understand why... :(
--
View this message in context: http://n2.nabble.com/Can-t-map-AVG-sql-field-to-class-tp1562523p1576019.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Can;t map AVG sql field to class
Posted by holod <se...@gmail.com>.
Pinaki Poddar wrote:
>
> So try something like:
> Query query = em.createNativeQuery(sb.toString());
> Object result = query.getSingleResult();
> InformerRow ir = new InformerRow();
> ir.s5 = ((Number)result).intValue();
>
I've tried to do define s5 as Number, I've created constructor
InformerRow(Number s5){
this.s5 = s5;
}
but it didn't help.
So, I'll try your solution, I'm not sure it will help.
Anyway thank you for your help!
--
View this message in context: http://n2.nabble.com/Can-t-map-AVG-sql-field-to-class-tp1562523p1575361.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Can;t map AVG sql field to class
Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
The source of the problem seems to be
> Query query = em.createNativeQuery(sb.toString(), InformerRow.class);
Your query returns s5 - a Number.
While the above code is saying that the query result is an instance of
InformerRow.
So try something like:
Query query = em.createNativeQuery(sb.toString());
Object result = query.getSingleResult();
InformerRow ir = new InformerRow();
ir.s5 = ((Number)result).intValue();
--
View this message in context: http://n2.nabble.com/Can-t-map-AVG-sql-field-to-class-tp1562523p1563189.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.