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.  [ 3 posts ] 
Author Message
 Post subject: WHERE 1=fnCheckSomething(...)
PostPosted: Wed May 06, 2009 10:48 am 
Beginner
Beginner

Joined: Mon Jan 05, 2009 7:08 am
Posts: 24
hello

How can I add Criteria to retrieve an entity with a WHERE clause which involves a function call?

Code:
select * from Person WHERE ... AND 1=fnCheckSomething(Person.Id, OwnerId)


btw, for our app, fnCheckSomething is only implemented for Oracle and M$SQL. For MySQL, because it can't do recursive function calls, it's in form of a stored proc which makes it even more difficult. At the momet, we've resorted to hand crafted stored proc because "fnCheckSomething" is crucial and it interferes with paging/sort order as well.

Note that input to func includes:
a. Person.Id --- person is the entity we're trying to retrieve
b. OwnerId --- input param to "Stored Proc" (or FUNCTION) which contains this SELECT statement.
So, there're two sorts of param...

I suppose I need something which resembles: (1) Named query (with dynamic param, see above a and b) + (2) ICriteria which can add a *dynamic* Named-query somehow.

Thanks


Top
 Profile  
 
 Post subject: Re: WHERE 1=fnCheckSomething(...)
PostPosted: Mon May 11, 2009 10:12 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You could use Expression.SQL(...) in your criteria.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: WHERE 1=fnCheckSomething(...)
PostPosted: Mon May 11, 2009 9:35 pm 
Beginner
Beginner

Joined: Mon Jan 05, 2009 7:08 am
Posts: 24
Thanks this is exactly what I needed (Although for MySQL there's no such function in where clause for me to call unfortunately ... MySQL does not support recursively function call)

Quoting REF here in case if someone else need it:
https://www.hibernate.org/hib_docs/nhib ... teria.html

There are quite a range of built-in criterion types (Expression subclasses), but one that is especially useful lets you specify SQL directly.

Code:
// Create a string parameter for the SqlString below
        Parameter paramName = new Parameter("someName", new StringSqlType());

        IList cats = sess.CreateCriteria(typeof(Cat))
            .Add( Expression.Sql(
                new SqlString( new object[] {
                    "lower({alias}.Name) like lower(",
                    paramName,
                    ")" } ),
                "Fritz%",
                NHibernateUtil.String )
            .List();

The {alias} placeholder with be replaced by the row alias of the queried entity.


Thanks again


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.