-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Using SQL Contains
PostPosted: Thu Mar 27, 2008 7:49 pm 
Newbie

Joined: Thu Dec 14, 2006 7:08 pm
Posts: 11
When I want to query for an object based on the value of a property. I do something like,

ICriteria Query = Session.CreateCriteria(GetType(Customer))

Query.Add(New Expression.LikeExpression("LastName", "Doe")

Return Query.List(Of Customer)()

I'm running SQL Server 2000 and would like to leverage the full-text indexes I have setup which requires me to use the t-SQL Contains function. Suggestions?

Cheers,
Aeden


Top
 Profile  
 
 Post subject: Using SQL Contains
PostPosted: Fri Mar 28, 2008 3:47 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Aeden,

You can add an arbitrary SQL expressions using:
Code:
Query.Add(Expression.Sql("CONTAINS(LAST_NAME_COLUMN, 'Doe'"));


Bear in mind that the SQL expression needs the column names as they appear in the database - not the object property names.

Does that help?

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 1:14 pm 
Newbie

Joined: Thu Dec 14, 2006 7:08 pm
Posts: 11
That helps a little. There's no class called Expression.Sql, but I do see one name Expression.SqlCriterion.

Any ideas on the slightly more complicated situation where I want all orders placed by a customer with the last name of Doe. Usually I would do something like,

ICriteria Query = Session.CreateCriteria(GetType(Order))

Query.Add(New Expression.LikeExpression("Customer.LastName", "Doe")

Return Query.List(Of Order)()

Cheers,
Aeden


Top
 Profile  
 
 Post subject: Using SQL Contains
PostPosted: Fri Mar 28, 2008 1:40 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Aeden,

The syntax for Expression.Sql is not a class ... it's a factory method that creates an ICriterion for you (making the interface more fluent).

There are two ways you can create the criteria you are talking about. Either using aliases:
Code:
Return
    Session
        .CreateCriteria(GetType(Order))
        .CreateAlias("Customer", "cust")
        .Add(Expression.Like("cust.LastName", "Doe%"))
        .List<Of Order>()


Or using a second ICriteria:
Code:
Return
    Session
        .CreateCriteria(GetType(Order))
            .CreateCriteria("Customer")
            .Add(Expression.Like("LastName", "Doe%"))
            .List<Of Order>()


Regards,
Richard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.