String Cast for int Hibernate

Asked

Viewed 1,667 times

1

Does anyone know how to cast a String for int within the query in this query, the variable "searchString"?

@Query("SELECT s FROM StoreOrder s INNER JOIN s.user u WHERE lower(u.fullname) LIKE %:searchString% OR u.totalamount=:searchString")
public Page<StoreOrder> findBySearch(@Param("searchString") String searchString, Pageable pageable);
  • Strange to use the same parameter as a filter for two different fields in the query, no?

1 answer

1

Hibernate SQL (HQL) supports CAST similar to native SQL, with the difference that the types are those of Java and not those of the database. Therefore:

u.totalamount = CAST(:searchString as Integer)
  • Hello Caffé, I tried to do it the way I suggested, but it is with the following stack: Caused by: Exception [Eclipselink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.Jpqlexception Exception Description: Problem compiling [SELECT s FROM Storeorder s INNER JOIN s.user u WHERE Lower(u.fullname) LIKE :searchString OR u.totalamount = CAST(:searchString as Integer)]. [93, 106] The state field path 'u.totalamount' cannot be resolved to a Valid type.

  • 1

    I imagine you have this error even without the CAST. Will the entity user has a field totalamount? Is this field of the Integer type? Does this field actually belong to Storeorder and should therefore be prefixed with s (s.totalamount)?

  • Yes, I am having this error even without the cast, and there is this field, type Integer, I will try to add an extra parameter in the method, but type Integer, I want to see if the error continues ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.