Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use typcasting inside a JPQL statement?

I have two Integer columns in the database (derby and db2). I need to divide them with each other inside a JPQL.

Both columns being of type Integer return zero if remainder is a decimal number e.g 0.25 becomes 0 etc and understandably so since type is int.

In SQL I could have this for example

select CAST(column1 as decimal(6,2))/CAST(column2 as decimal(6,2))from Sometable;

but what is JPQL equivalent .

One option might be (I have not tried yet) is to have a @Transient method in the entity returning the Decimal type and doing this calculation there and pass that to JPQL but I would rather let SQL do this work.


Mysql does not require casting at database level . So Behaviour for different RDBMS is different which is fine . But what should JPQL do with out needing to use a native query to know that cast to decimal is needed for this operation.

Adding dialect <property name="openjpa.jdbc.DBDictionary" value="derby"/> did not fix it either.

Please note it is JPA1

like image 371
Shahzeb Avatar asked May 11 '12 01:05

Shahzeb


People also ask

Can we use subquery in JPQL?

A subselect is a query embedded into another query. It's a powerful feature you probably know from SQL. Unfortunately, JPQL supports it only in the WHERE clause and not in the SELECT or FROM clause. Subqueries can return one or multiple records and can use the aliases defined in the outer query.

Can we use inner join in JPQL?

JPQL provides an additional type of identification variable, a join variable, which represent a more limited iteration over specified collections of objects. In JPQL, JOIN can only appear in a FROM clause. The INNER keyword is optional (i.e. INNER JOIN is equivalent to JOIN).

Which method is used to execute a SELECT JPQL query?

Execute the query using an appropriate Query's method: getSingleResult or getResultList. In the case of an update or delete operation, though, you must use the executeUpdate method, which returns the number of entity instances updated or deleted.

Which methods should be used for pagination with JPQL?

Solution: With JPA and Hibernate, you have to set the pagination information on the Query interface and not in the query String as you know it from SQL. You can do that by calling the setFirstResult(int startPosition) and setMaxResults(int maxResults) methods.


2 Answers

You have basically three options.

  1. Use postload and let java do it .
  2. Use a third Class and use New Operator in Select statement of JPQL to call constructor and inside that calls you can manage the datatypes which will be passed to SQL but you will get more then one kind of objects back which is fine just get the right one from right array index.More on this here.
  3. Third use Native Query like Idanmo said.
like image 116
Java Ka Baby Avatar answered Oct 06 '22 01:10

Java Ka Baby


That the division operator performs integer division on integer arguments is a feature. In MySQL you have to explicitly choose div for integer division and / for floating point division, whereas in JPQL the choice is made automatically just like in Java.

So I suggest:

select (column1 * 1.0) / column2 from Sometable;
like image 40
Old Pro Avatar answered Oct 05 '22 23:10

Old Pro