套用「喜欢」 Hibernate(Mysql)在Blob字段上 [英] Apply "like" on blob field with Hibernate (Mysql)

查看:71
本文介绍了套用「喜欢」 Hibernate(Mysql)在Blob字段上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Restrinction数组构建查询,并且必须在 Blob 字段上应用like. 如果执行此操作,它将起作用:

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());

我收到此错误:

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

我该怎么办?

谢谢

推荐答案

您无法通过Criteria将字符串值与blob进行比较,因为这将无法将字符串强制转换为Blob(基本上是java.sql.Blob是一个接口)如果您确实需要进行这种比较,则唯一的解决方案是获取Blob值并基于该值创建一个字符串,请使用以下示例:

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);

您在Java方面进行比较吗?

and do you comparision on your java Side.

这篇关于套用「喜欢」 Hibernate(Mysql)在Blob字段上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆