3

I got a Entity with a Integer

@Entity(name=customer)
public Customer {
    ...
    @Column
    private int number;
    ...
    //getter,setter
}

Now I want to cast this Integer in a query to compare it with an other value.

I tried this Query:

"SELECT c FROM customer c WHERE CAST(c.number AS TEXT) LIKE '1%'"

But it doesn't work.

2
  • try this SELECT c FROM customer c WHERE CAST(c.number AS varchar(5)) LIKE '1%' Commented Jul 1, 2012 at 0:22
  • 1
    @EJP JPQL may be more accurate tag Commented Jul 1, 2012 at 0:29

3 Answers 3

3

This works in some of my code using Hibernate:

SELECT c FROM customer c WHERE STR(c.number) LIKE '1%'

In general, this is what the Hibernate docs (14.10 Expressions) say on casting:

str() for converting numeric or temporal values to a readable string

cast(... as ...), where the second argument is the name of a Hibernate type, and extract(... from ...) if ANSI cast() and extract() is supported by the underlying database

Sign up to request clarification or add additional context in comments.

2 Comments

ejbql is a bit different from jpql and hql. I get the same exception: Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing the query [SELECT c FROM customer c WHERE STR(c.number) LIKE '1%'], line 1, column 31: unexpected token [(]. –
@Istvan Devai Man I was looking for this whole day! Thank you.
3

Since EJB3 EJBQL has been (almost) replaced by JPQL. In EJBQL and according to http://docs.oracle.com/cd/E11035_01/kodo41/full/html/ejb3_langref.html in JPQL as well there is no functionality to CAST a property of an entity.

So like I already told there are two options left:

  1. Use a native Query.
  2. Add special cast methods to the entities.

Comments

0

You need to specify the column you're selecting from table alias c, and since EJBQL doesn't support a cast function, pass a string into the query instead of text. (This effectively allows your program to do the cast before it gets to EJBQL.)

The example below is in SQL Server, but should give you the idea:

 declare @numberText as varchar(50)
 set @numberText = '1'

 SELECT c.CustomerNumber FROM customer c 
 WHERE c.CustomerNumber  LIKE @numbertext + '%'

So instead of private int number use private string numberText.

NOTE: I edited this answer after OP confirmed EJBQL does not support a CAST function.

10 Comments

Sorry I make a big mistake it's not jpql, it's ejb-ql.
Try my query. It should work. I just edited it to include your column and table. (I originally posted my answer that was based on the table I tested it on.)
Unfortunely, it doesn't. Same exception: Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing the query [SELECT c.number FROM customer c WHERE CAST(c.number AS TEXT) LIKE '1%'], line 1, column 45: unexpected token [(].
Hmmm... can you try the query without the where clause to see if it works? Does johntotetwoo's updated query work?
No, johntotetwoo's solution doesn't work also. Sure if I'm not using the CAST function, everything works fine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.