A
A
Alexey Larionov2014-11-26 21:25:03
Hibernate
Alexey Larionov, 2014-11-26 21:25:03

How to compose a Full-Text HQL query containing SQL contains?

It is necessary to search through a web service in a full-text field. HQL does not support the Contains and ContainsTable SQL predicates.
Approximately this SQL query is converted to HQL:

select * from test_docs where Contains(xxtext,'Boaz')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Larionov, 2014-12-17
@lari4

The problem was solved like this.
Create a SQL function:

USE [dbName]
GO
/****** Object:  UserDefinedFunction [dbo].[funcFullText]    Script Date: 11/27/2014 1:12:11 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
ALTER FUNCTION [dbo].[funcFullText] 
(
     -- Add the parameters for the function here
     @query varchar (250),  @ID varchar (45)
)
RETURNS BIT
AS
BEGIN
     -- Declare the return variable here
     DECLARE @Result BIT
     DECLARE @SQL INT
     select @SQL = count(*) from test_docs where Contains(xxtext,@query) and SYSROWID = @ID
     IF @SQL > 0 SET @Result = 1    ELSE SET @Result = 0
     -- Return the result of the function
     RETURN @Result
END

Inside the function, it executes an SQL query containing Contains.
Next, we form an HQL query and call the web service method:
hql = " from test_docs where dbo.funcFullText('" + txtSqlQuery.Text + "',SYSROWID) = true";
saWsPropertyValueArray[] result = archiveService.search(token, hql);

V
Vladimir Smirnov, 2014-11-28
@bobzer

Hibernate works great with SQL, do this:

Query query = session.createSQLQuery("select * from test_docs where Contains(xxtext,'Boaz')")
.addEntity(TestDoc.class);

If you work through EntityManager, then there is createNativeQuery ()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question