Apply “like” on blob field with Hibernate (Mysql)

寵の児 提交于 2019-12-25 07:29:56

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/37609862/apply-like-on-blob-field-with-hibernate-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!