advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 2.3/5 | Rate this item | 12 users have rated this item.
Expertise: Advanced
Language: Java
April 10, 2009
Write a Complex Query in Hibernate
This tip shows you how to use the findByExample method in Hibernate. The method uses the SQL AND operator to log in a user given an email address and password. This code assumes you have a database table named Accountse mapped in the com.extensions.Accountse class:

/**
* Get the list with the right account
*
* @param instance
* @param c_email
* @param c_password
* @return List
* @throws Exception
*/
public List findByExampleAnd(String c_email,
String c_password)
throws java.lang.Exception
{
log.info("Finding Accountse instance by example...");
Session session = HibernateUtil.currentSession();
try {
List results =
session.createCriteria(
"com.extensions.Accountse").add(
Restrictions.and(Restrictions.eq(
"email",c_email),
Restrictions.eq("password",c_password))).list();
log.info("Find by example successful, result size: " +
results.size());
return results;
} catch (Exception re) {
log.error("Find by example failed...", re);
throw re;
} finally {
HibernateUtil.closeSession();
}
}
Leonard Anghel
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement