Problem using the Hibernate ... Expression.in Criteria .....Please Help Out !!!
I had posted this earlier too, but I think it got lost in deluge of
Great Java Messages !!
Any Hibernate Master, Please help out .... !!!!
I have the following entries in Database table
mysql> select email, firstname, lastname from contact;
+-----------------+-----------+----------+
| email | firstname | lastname |
+-----------------+-----------+----------+
| rahulATsaini.com | rahul | saini |
| rohitATsaini.com | rohit | saini |
+-----------------+-----------+----------+
Through front end I am getting both the above email ids (as search
text "rahulATsaini.com, rohitATsaini.com" )
and I have to display firstname and lastname for these.
My Hibernate 3 code to search (using Hibernate Criteria and
Expression.in() and display this is as below:
=============================================
// get search string as req parameter, tokenize it by ,
// create a simple String[] contactsArr and assign these email
ids to its elements
String sContacts = req.getParameter("SearchString").trim();
StringTokenizer emailsTok = new StringTokenizer(sContacts, ",");
int totalTokens = emailsTok.countTokens();
String[] contactsArr = new String[totalTokens];
int i = 0;
while(emailsTok.hasMoreTokens()){
contactsArr[i] = emailsTok.nextToken().trim();
i++;
}
if(sContacts != null){
List result = null;
try{
Session sess = UtilHelper.getSessionFactory().openSession();
Transaction newTrans = sess.beginTransaction();
Criteria crit = sess.createCriteria(Contact.class);
result = crit.add(Expression.in("Email", contactsArr)).list();
if(result != null){
Iterator iter = result.iterator();
while(iter.hasNext()){
Contact contact = (Contact)iter.next();
pw.println(contact.getFirstName() + "," +
contact.getLastName());
}
}
newTrans.commit();
sess.close();
}
==============================================
However, instead of printing:
rahul,saini
rohit,saini
it is printing:
rahul,saini
rahul,saini
==============================================
HQL generated is :
Hibernate: select this_.ID as ID0_0_, this_.FIRSTNAME as
FIRSTNAME0_0_, this_.LA
STNAME as LASTNAME0_0_, this_.EMAIL as EMAIL0_0_, this_.DOB as
DOB0_0_, this_.PH
_LANDLINE as PH6_0_0_, this_.PH_MOBILE as PH7_0_0_, this_.CA_STREET
as
CA8_0_0_,
this_.CA_CITY as CA9_0_0_, this_.CA_COUNTRY as CA10_0_0_,
this_.CA_PINCODE as C
A11_0_0_, this_.PA_STREET as PA12_0_0_, this_.PA_CITY as PA13_0_0_,
this_.PA_COU
NTRY as PA14_0_0_, this_.PA_PINCODE as PA15_0_0_ from CONTACT this_
where this_.
EMAIL in (?, ?)
Where is it wrong ?