3.2.2: java.lang.Double cannot be cast to java.math.BigDecimal

5,125 views
Skip to first unread message

Ghalleb

unread,
Aug 14, 2013, 2:58:29 PM8/14/13
to quer...@googlegroups.com
Hi,

After upgrading to 3.2.2 I have now the error (in my own code) :

    java.lang.Double cannot be cast to java.math.BigDecimal

This is because the returned type is not the same as before.
I think it might be related to this issue:


Is there an easy way to solve the problem? I don't want to change the type of my data, ans there is no ".bigDecimalValue()" function

Timo Westkämper

unread,
Aug 14, 2013, 3:03:33 PM8/14/13
to Querydsl on behalf of Ghalleb
Hi.

On Wed, Aug 14, 2013 at 9:58 PM, Ghalleb via Querydsl <querydsl+noreply-APn2wQeX0-G4DIG...@googlegroups.com> wrote:
Hi,

After upgrading to 3.2.2 I have now the error (in my own code) :

    java.lang.Double cannot be cast to java.math.BigDecimal

This is because the returned type is not the same as before.

Could you provide more context? What kind of query?
 
I think it might be related to this issue:


Is there an easy way to solve the problem? I don't want to change the type of my data, ans there is no ".bigDecimalValue()" function

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com


Ghalleb

unread,
Aug 14, 2013, 3:14:46 PM8/14/13
to quer...@googlegroups.com
Hi Timo,

My code is too complicated to be all written here...

In a nutshell:
I have several queries on several tables.
I make an Union with all of them and I use a final aggregation (sum() or avg()) on some columns.

Before QueryDSL used so give me BigDecimal type Object, but now I have Double Type Object.

So later I have a CastClass exception.

It was an easy fix in my own code:

        if (value != null && value instanceof Double) {
            myValue = new BigDecimal((Double) value);
        }
        else {
            myValue = (BigDecimal) value;
        }

But it seems to be kind of a regression....


Le mercredi 14 août 2013 15:03:33 UTC-4, Timo Westkämper a écrit :
Hi.

Timo Westkämper

unread,
Aug 14, 2013, 3:28:48 PM8/14/13
to Querydsl on behalf of Ghalleb
Hi.

Is the expression type you use to project your results Double or BigDecimal?

Br,
Timo

Ghalleb

unread,
Aug 14, 2013, 3:32:06 PM8/14/13
to quer...@googlegroups.com

        final List<Object[]> resultS = queryS.list(Projections.array(Object[].class,

                selectedNumberPaths.values().toArray(new SimpleExpression[selectedNumberPaths.size()])));


Le mercredi 14 août 2013 15:28:48 UTC-4, Timo Westkämper a écrit :
Hi.

Is the expression type you use to project your results Double or BigDecimal?

Br,
Timo

On Wed, Aug 14, 2013 at 10:14 PM, Ghalleb via Querydsl <querydsl+noreply-APn2wQeX0-G4DIGcv7WYJSLQBL8vAtw168CK8NZ6Bpg...@googlegroups.com> wrote:
Hi Timo,

My code is too complicated to be all written here...

In a nutshell:
I have several queries on several tables.
I make an Union with all of them and I use a final aggregation (sum() or avg()) on some columns.

Before QueryDSL used so give me BigDecimal type Object, but now I have Double Type Object.

So later I have a CastClass exception.

It was an easy fix in my own code:

        if (value != null && value instanceof Double) {
            myValue = new BigDecimal((Double) value);
        }
        else {
            myValue = (BigDecimal) value;
        }

But it seems to be kind of a regression....

Le mercredi 14 août 2013 15:03:33 UTC-4, Timo Westkämper a écrit :
Hi.

Timo Westkämper

unread,
Aug 14, 2013, 3:33:23 PM8/14/13
to Querydsl on behalf of Ghalleb
Hi.

What about the specific column that has now a different type? What expression did you use for it?

Timo

Ghalleb

unread,
Aug 14, 2013, 3:41:12 PM8/14/13
to quer...@googlegroups.com

        List<SimpleExpression<?>> queryFields = new LinkedList<SimpleExpression<?>>();;

        queryFields.add(STable.table.myfield.coalesce(new Long(0)).asNumber()

                .add(STable.table.myfield2.coalesce(new Long(0))).as("myfieldvar"));


myfield (and myfield2):

    public final NumberPath<Long> myfield = createNumber("myfield", Long.class);



Le mercredi 14 août 2013 15:33:23 UTC-4, Timo Westkämper a écrit :
Hi.

What about the specific column that has now a different type? What expression did you use for it?

Timo

On Wed, Aug 14, 2013 at 10:32 PM, Ghalleb via Querydsl <querydsl+noreply-APn2wQeX0-G4DIGcv7WYJSLQBL8vAtw168CK8NZ6Bpg...@googlegroups.com> wrote:

        final List<Object[]> resultS = queryS.list(Projections.array(Object[].class,

                selectedNumberPaths.values().toArray(new SimpleExpression[selectedNumberPaths.size()])));


Le mercredi 14 août 2013 15:28:48 UTC-4, Timo Westkämper a écrit :
Hi.

Is the expression type you use to project your results Double or BigDecimal?

Br,
Timo

Timo Westkämper

unread,
Aug 14, 2013, 3:46:00 PM8/14/13
to Querydsl on behalf of Ghalleb
Hi.

But these are all Long typed columns. Does any of those cause the error?

I really need more details to be of any help here.

Br,
Timo

Ghalleb

unread,
Aug 14, 2013, 3:53:24 PM8/14/13
to quer...@googlegroups.com
I think the aggregation cause this to return a BigDecimal / Double value.

For me right now it's not a big deal because I already fixed it by creating a new BigDecimal variable when it's a Double value, but other people might have the same problem.

Perhaps a .bigDecimalValue() function could be usefull like .longValue() or .doubleValue()



Le mercredi 14 août 2013 15:46:00 UTC-4, Timo Westkämper a écrit :
Hi.

But these are all Long typed columns. Does any of those cause the error?

I really need more details to be of any help here.

Br,
Timo

On Wed, Aug 14, 2013 at 10:41 PM, Ghalleb via Querydsl <querydsl+noreply-APn2wQeX0-G4DIGcv7WYJSLQBL8vAtw168CK8NZ6Bpg...@googlegroups.com> wrote:

        List<SimpleExpression<?>> queryFields = new LinkedList<SimpleExpression<?>>();;

        queryFields.add(STable.table.myfield.coalesce(new Long(0)).asNumber()

                .add(STable.table.myfield2.coalesce(new Long(0))).as("myfieldvar"));


myfield (and myfield2):

    public final NumberPath<Long> myfield = createNumber("myfield", Long.class);



Le mercredi 14 août 2013 15:33:23 UTC-4, Timo Westkämper a écrit :
Hi.

What about the specific column that has now a different type? What expression did you use for it?

Timo

Timo Westkämper

unread,
Aug 14, 2013, 3:55:42 PM8/14/13
to Querydsl on behalf of Ghalleb
Hi.

On Wed, Aug 14, 2013 at 10:53 PM, Ghalleb via Querydsl <querydsl+noreply-APn2wQeX0-G4DIG...@googlegroups.com> wrote:
I think the aggregation cause this to return a BigDecimal / Double value.

For me right now it's not a big deal because I already fixed it by creating a new BigDecimal variable when it's a Double value, but other people might have the same problem.

But the new Querydsl behaviour is in fact more consistent, as the return type is the same as the expression type.
 

Perhaps a .bigDecimalValue() function could be usefull like .longValue() or .doubleValue()

Could you create a ticket for that?

Br,
Timo
 

Timo Westkämper

unread,
Aug 15, 2013, 1:37:54 PM8/15/13
to quer...@googlegroups.com
Hi.

I just realized that the cleanest way to fix is to generate your Querydsl types with BigDecimal for the SQL types where you want to use BigDecimal.

Querydsl will then return those columns as BigDecimal.

The customization of the code generation is documented here http://www.querydsl.com/static/querydsl/3.2.2/reference/html/ch02s03.html#d0e1061

Br,
Timo


On Wednesday, August 14, 2013 10:55:42 PM UTC+3, Timo Westkämper wrote:
Hi.

On Wed, Aug 14, 2013 at 10:53 PM, Ghalleb via Querydsl <querydsl+noreply-APn2wQeX0-G4DIGcv7WYJSLQBL8vAtw168CK8NZ6Bpg...@googlegroups.com> wrote:
I think the aggregation cause this to return a BigDecimal / Double value.

For me right now it's not a big deal because I already fixed it by creating a new BigDecimal variable when it's a Double value, but other people might have the same problem.

But the new Querydsl behaviour is in fact more consistent, as the return type is the same as the expression type.
 

Perhaps a .bigDecimalValue() function could be usefull like .longValue() or .doubleValue()

Could you create a ticket for that?

Br,
Timo
 


Le mercredi 14 août 2013 15:46:00 UTC-4, Timo Westkämper a écrit :
Hi.

But these are all Long typed columns. Does any of those cause the error?

I really need more details to be of any help here.

Br,
Timo

To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages