Search code examples
jquerymysqldatabasehibernatesql-like

Apply "like" on blob field with Hibernate (Mysql)


I have to build my query using an Array of Restrinction and I have to apply a like on a Blob field. If I do this, it works:

Restrictions.like("DBFieldName", object.getFieldName());

Now, I need to add the %, but if I do something like:

Restrictions.like("DBFieldName", "%" + object.getFieldName());

I get this error:

java.lang.ClassCastException: java.lang.String cannot be cast to java.sql.Blob

What should I do?

Thank you


Solution

  • you cant compare a String value to blob through Criteria because this last will try to cast the the string to Blob which is not possible (basicly java.sql.Blob is an interface)if you realy need to do such comparision, the only solution you have is to get the Blob value and create a string based on it, use this example:

    byte[] bdata = blob.getBytes(1, (int) blob.length());
    String text = new String(bdata);
    

    and do you comparision on your java Side.