-->
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.  [ 6 posts ] 
Author Message
 Post subject: Cast in Criteria
PostPosted: Sat Jun 02, 2012 5:30 pm 
Newbie

Joined: Sat Jun 02, 2012 12:48 pm
Posts: 4
Using Criteria, is it possible to cast integer to string?

Code:
public List<User> listByCriteria(String text) {
       
        text = addPercentSign(text);
        Criteria criteria = session.createCriteria(User.class)
                                   .add(Restrictions.disjunction()
                                                    .add(Restrictions.ilike("login", texto))
                                                    .add(Restrictions.ilike("name", texto))
                                                    .add(Restrictions.ilike("lastname", texto))
                                                    .add(Restrictions.ilike("userId", texto)); //need to cast userId to String
        return criterio.list();
    }


How this can be done?


Top
 Profile  
 
 Post subject: Re: Cast in Criteria
PostPosted: Mon Jun 04, 2012 8:36 pm 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
I'm confused as to what you're asking.

String.valueOf(texto) or texto.toString()


Top
 Profile  
 
 Post subject: Re: Cast in Criteria
PostPosted: Mon Jun 04, 2012 9:00 pm 
Newbie

Joined: Sat Jun 02, 2012 12:48 pm
Posts: 4
No.
UserId is a property on my class, and it is a Integer.
So I cannot use:
Code:
.add(Restrictions.ilike("userId", texto));

because LIKE function is only for Strings.
I need to tell the Criteria to cast the "userId" to String when its querying the database (SQLServer).


Top
 Profile  
 
 Post subject: Re: Cast in Criteria
PostPosted: Tue Jun 05, 2012 9:05 am 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
The method ilike accepts an Object. It will do a toString() on the object for you. What is the exception you're getting?


Top
 Profile  
 
 Post subject: Re: Cast in Criteria
PostPosted: Tue Jun 05, 2012 9:46 am 
Newbie

Joined: Sat Jun 02, 2012 12:48 pm
Posts: 4
That's not the case. I don't need to convert the String text, I need to convert the database column userId.
I have to tell criteria/Restrictions to, when its generates the sql query, it use CAST(userId as varchar), like so:
Code:
SELECT ... CAST(userId as varchar) as UserId, lastname ... FROM Users ... WHERE ...


Look at the code:
Code:
public List<User> listByCriteria(String text) {
       
        text = addPercentSign(text);
        Criteria criteria = session.createCriteria(User.class)
                                   .add(Restrictions.disjunction()
                                                    .add(Restrictions.ilike("lastname", text))
                                                    .add(Restrictions.ilike("userId", text));
        return criterio.list();
    }

I do not need to convert text because it is a String already, but userId it is not!


Top
 Profile  
 
 Post subject: Re: Cast in Criteria
PostPosted: Fri Feb 28, 2014 9:17 am 
Newbie

Joined: Fri Feb 28, 2014 8:59 am
Posts: 1
i i'm facing exact the same problem, did you solve it?


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