-->
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.  [ 3 posts ] 
Author Message
 Post subject: Criteria seach with restrictions from several related tables
PostPosted: Mon Dec 13, 2010 12:06 pm 
Newbie

Joined: Mon Dec 13, 2010 10:17 am
Posts: 2
Hello

I'm having a problem making the right restrictions for my database seach using Hibernate. I'm having two tables being Location and Keyword, and i want to seach for locations in an area that also holds 1 or more of my keywords, and store those locations in a list.

Is it possible to make a "Criteria crit = session.createCriteria(Location.class);" that somehow also includes "Criteria crit = session.createCriteria(Keyword.class);

Location and Keyword is related through another another class named Company.

appreciating the help.

Code:
public Vector<Location> getCoordinatesOfLocations(Double xMin, Double xMax, Double yMin, Double yMax, ArrayList<String> keywords)
    {
       Vector<Location> list = new Vector<Location>();
      
       xMin  = (coordinates.getLngMin());
       xMax  = (coordinates.getLngMax());
       yMin  = (coordinates.getLatMin());
       yMax  = (coordinates.getLatMax());
      
       Session session = null;
          try {
           
            SessionFactory sessionFactory = new Configuration().configure()
                .buildSessionFactory();
            session = sessionFactory.openSession();
            //Criteria Query Example
            Criteria crit = session.createCriteria(Location.class);   
           
      
            crit.add(Restrictions.between("x", new BigDecimal(xMin), new BigDecimal(xMax))); //Between condition
            crit.add(Restrictions.between("y", new BigDecimal(yMin), new BigDecimal(yMax))); //Between condition
           
           
          crit.add(Restrictions.or(Restrictions.like("keyword", "%"+keywords.get(0)+"%"),
                     Restrictions.or(Restrictions.like("keyword", "%"+keywords.get(1)+"%"), //currently only taking 3 keywords.
                           (Restrictions.like("keyword", "%"+keywords.get(2)+"%")))));
           
           
            List locations = crit.list();
         
            for(Iterator it = locations.iterator();it.hasNext();){
                Location location = (Location) it.next();
                list.add(location);
            }
           
            session.close();
          } catch (Exception e) {
            System.out.println(e.getMessage());
          } finally {
          }   
         
         
       return list;
    }


Top
 Profile  
 
 Post subject: Re: Criteria seach with restrictions from several related tables
PostPosted: Tue Dec 14, 2010 6:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
You can traverse associations by chaining createCriteria("propertyName"). Have a look at the online documentation.


Top
 Profile  
 
 Post subject: Re: Criteria seach with restrictions from several related tables
PostPosted: Tue Dec 14, 2010 12:03 pm 
Newbie

Joined: Mon Dec 13, 2010 10:17 am
Posts: 2
I'm not quite sure what you mean. Could you give a quick example?

As i understand it i can write something like Criteria crit = session.createCriteria(Location.class).createCriteria("propertyName of the Keyword class"

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.