About Question enthuware.oce-jpad.v6.2.570 :

Moderator: admin

Post Reply
sztgeza
Posts: 4
Joined: Thu Sep 04, 2014 2:32 pm
Contact:

About Question enthuware.oce-jpad.v6.2.570 :

Post by sztgeza » Sat Sep 13, 2014 1:31 pm

The first correct answer (by the solution) is:
A field of type Integer and an input parameter of type double.
Exact (e.g. int) and approximate (e.g. double) numeric values can be compared.
However:

Code: Select all

Query q = em.createQuery("SELECT i from Item i where i.id < :p_id");
q.setParameter("p_id",7.0d);
List result = q.getResultList();
throws an IllegalArgumentException:
java.lang.IllegalArgumentException: Parameter value [7.0] did not match expected type [java.lang.Integer (n/a)]
Please, could you describe in more details, how can the id (Integer) and the double parameter be compared?

admin
Site Admin
Posts: 8412
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.570 :

Post by admin » Sat Sep 13, 2014 9:06 pm

As per section 14.4 of JPA 2.0 Specification:
comparison_expression ::=
string_expression comparison_operator {string_expression | all_or_any_expression} |
boolean_expression { =|<>} {boolean_expression | all_or_any_expression} |
enum_expression { =|<>} {enum_expression | all_or_any_expression} |
datetime_expression comparison_operator
{datetime_expression | all_or_any_expression} |
entity_expression { = | <>} {entity_expression | all_or_any_expression} |
arithmetic_expression comparison_operator
{arithmetic_expression | all_or_any_expression}
The specification does not forbade comparing int and double. So it should be valid.
If you are getting an exception the implementation is not compliant.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.570 :

Post by __JJ__ » Thu Sep 20, 2018 7:30 pm

sztgeza wrote:
Sat Sep 13, 2014 1:31 pm
The first correct answer (by the solution) is:
A field of type Integer and an input parameter of type double.
Exact (e.g. int) and approximate (e.g. double) numeric values can be compared.
However:

Code: Select all

Query q = em.createQuery("SELECT i from Item i where i.id < :p_id");
q.setParameter("p_id",7.0d);
List result = q.getResultList();
throws an IllegalArgumentException:
java.lang.IllegalArgumentException: Parameter value [7.0] did not match expected type [java.lang.Integer (n/a)]
Please, could you describe in more details, how can the id (Integer) and the double parameter be compared?
Quite simply I think what is happening here is that the usual Java SE rules for comparison are in place. You can observe this from the answers to the question.
Note that Java will unbox and widen but it will not widen and box.
So you can compare an Integer to a double, because it will unbox the Integer to int and then widen it to double:

Code: Select all

    public static void main(String[] args) {
        foo(2,2.2);

    }

    static void foo(Integer i, double d) {
        if(i==d) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
The same thing happens with Float and double: it unboxes Float to float and widens it to double.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest