Hibernate criteria, how to add type casting?

45 views Asked by At

How to create this type of query with Hibernate criteria ?

SELECT * FROM books WHERE CAST(price AS TEXT) LIKE '123%'

i have table structure like this:

 username(id), first_name( character varying ), safety_id(bigint)

and i like to select all records with safety_id like '3304%'

how do i do that ?

As Hiru correctly mentioned i was looking for bigdecimal to text casting. Also i've checked link provided by Balkrishna Rawool and that pointed me to correct way.

Solution

I've created NamedQuery

 @NamedNativeQuery(name = "getCustomerArrayForParameters", query = "SELECT * FROM view_customersflat WHERE CAST (safety_id as TEXT) LIKE :safety_id ",resultClass=Customer.class )

and created criteria as usual :

 getCurrentSession().getNamedQuery("getCustomerArrayForParameters").setString("s‌​afety_id", safetyId ).list();

everything worked out. Thanks for attention.

0

There are 0 answers