-->
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.  [ 7 posts ] 
Author Message
 Post subject: IDIOT question, but need your help
PostPosted: Tue Jul 19, 2005 12:05 am 
Newbie

Joined: Mon Jul 18, 2005 11:44 pm
Posts: 3
Hi All

this is the sql syntax:

select * from tbl_Money where col_Amount1+col_Amount2 = 100

how to achieve this in Hibernate on Persistence Object Level?
Please show me some sample code.

Thanks for you help in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 2:11 am 
Newbie

Joined: Thu Jul 14, 2005 7:50 am
Posts: 8
can you tell me what have you coded and where exactly you get a problem??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 2:32 am 
Newbie

Joined: Mon Jul 18, 2005 11:44 pm
Posts: 3
hi Priti,

I am now doing a search in a name list, first name, middle name and last name are stored in 3 separate columns of the same table, but the keywords user entered for search is the combination of those 3, for example, if use enter "harry potter" as keywords, what i want to do is to merge first name, middle name and last name together, in A+" "+B+" "+C manner, then I check wether this combination "like" %keywords%. This is easy to do if I use HQL, but to make the program structure consistent, I want to do the checking on persistence object level, use Hibernate Creteria/Restrictions.

my user persistent class is:

public class UserInfo{
private String userID;
private String firstName;
private String middleName;
Private String lastName;

public UserInfo(){
}

//getters and setters
}

I guess there will be a way to do it on the object level, but I just dun know what method I shall use.Do you have any idea? any example?
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 2:38 am 
Newbie

Joined: Tue Jul 05, 2005 11:47 pm
Posts: 15
Location: Argentina
Assuming your class is called Money, you can retrieve all the objects that pass your condition doing:

List moneyObjects = session.createQuery("from Money as money where money.amount1 + money.amount2 = 100").list();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 2:51 am 
Newbie

Joined: Mon Jul 18, 2005 11:44 pm
Posts: 3
thanks dprotas,

if I use HQL this is the right way, but can I just get the result list by narrow down the criteria? I mean something like:

Criteria criteria=session.createCriteria(UserInfo.class);
criteria.add(Restrictions.ilike(......));

this is to maintain the code consitency.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 3:54 pm 
Newbie

Joined: Tue Jul 05, 2005 11:47 pm
Posts: 15
Location: Argentina
As far as I know, the Criteria API doesn't support algebraic expressions like money.amount1 + money.amount2.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 4:34 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
skyline wrote:
thanks dprotas,

if I use HQL this is the right way, but can I just get the result list by narrow down the criteria? I mean something like:

Criteria criteria=session.createCriteria(UserInfo.class);
criteria.add(Restrictions.ilike(......));

this is to maintain the code consitency.


One thing you could do is create a column that is defined by the formula amount1 + amount2 and use that column with the Criteria API.

See the docs about defining columns using formula.


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