Hibernate + MSSQL +全文搜索通过包含SQLFunctionTemplate [英] Hibernate + MSSQL + Fulltext Search via Contains SQLFunctionTemplate

查看:525
本文介绍了Hibernate + MSSQL +全文搜索通过包含SQLFunctionTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在客户表上设置了全文索引,以便能够通过他们的信息快速搜索客户。



我必须创建一个定制的hibernate方言来简化映射。
并做一些时髦的东西来让hibernate工作
Hibernate + MSSQL +全文搜索通过包含



我的自定义方言有一个看起来像这样的函数

  registerFunction(contains,new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN,CONTAINS(?1,?2)AND 1)); 

这使得查询成为可能,如下所示:

(c.name,:term)= true


$的Customer c中的

  b $ b 

现在我的问题是,为了能够返回parial比赛,我需要引用该术语并添加一个*



所以原始查询将是

  select * from customer c where CONTAINS(c.name,''mycust *''); 

我尝试用引号和明星包装SQLFunctionTemplate,并尝试在调用站点引用,但没有工作。



有关如何创建上述原始查询的sql函数的任何建议?

解决方案

只需使用Hibernates标准API和sqlRestriction,然后您可以将任何字符串传递给您想要的查询。例如。 searchValue = myCust *等...
$ b $ pre $ Restrictions.sqlRestriction(CONTAINS(name,?),searchValue,StandardBasicTypes.STRING )


I've setup a fulltext index on my customer table to be able to quickly search for customers via their info.

I had to create a custom hibernate dialect to simplify the mapping. and do some funky stuff to get hibernate to work Hibernate + MSSQL + Fulltext Search via Contains

My custom dialect has a function that looks like this

registerFunction("contains", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "CONTAINS(?1, ?2) AND 1"));

This makes it possible to do queries as follows

from Customer c where contains(c.name, :term) = true

My problem now is that to be able to return parial matches I need to quote the term and add a *

So the raw query would be

select * from customer c where CONTAINS(c.name, '"mycust*"');

I've tried wrapping the SQLFunctionTemplate with quotes and a star, and tried quoting at the call site, but neither work.

Any suggestions on how to create a sql function that does the raw query above?

解决方案

Just use Hibernates criteria API and sqlRestriction then you can pass any string to the query u want. E.g. searchValue=myCust* etc...

   Restrictions.sqlRestriction(" CONTAINS(name, ?)", searchValue, StandardBasicTypes.STRING)

这篇关于Hibernate + MSSQL +全文搜索通过包含SQLFunctionTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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