问题
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?
回答1:
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)
来源:https://stackoverflow.com/questions/37751008/hibernate-mssql-fulltext-search-via-contains-sqlfunctiontemplate