dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Advanced
Apr 10, 2009

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date