First time with Hibernate Criteria Builder… makes me feel like a stud


Im currently in the process of building web app with Grails and came to a point where the grails dynamic methods wouldnt solve my searching problem… so today I got to play with Hibernate Criteria Builders. The other options included HQL and SQL embedded in Groovy… however, the Criteria Builder seemed fairly straight forward and with a little help from a buddy of mine, I got it working.

Once working… wow. I still cant get over how easy it was to perform queries and send data to a view.

For example, lets create 3 domains: Animal, Habitat and Type, where Animal has a Type and a Habitat.

Type

class Type{

     String description

     static belongsTo = [Animal]

     def String toString(){
             description
     }
}

habitat


class Habitat {

     String description

     static belongsTo = [Animal]

     def String toString(){
          description
     }
}

animal

class Animal {

     String description
     Habitat habitat
     Type type

     def String toString(){
          description
     }
}

Now for some dummy data

Type

Description
mammal
amphibian
reptile
fish
birds

Habitat

Description
ocean
desert
fresh water
mountain

Now lets use the data above and create some Animals

Animal

Description Type Habitat
hippo mammal desert
otter mammal fresh water
horny toad reptile desert
northern pike fish fresh water
shark fish ocean
elephant mammal desert
whale mammal ocean
mountain lion mammal mountain

Now, lets say you would like to retrieve all Animals that are Type:mammal. This is done very easily with Grails

def mammalType = Type.findByDescription('mammal')
def mammalList = Animal.findAllByType(mammalType.id)

Results:

  • hippo
  • otter
  • whale
  • elephant
  • mountain lion

If you have spent 5 minutes with Grails, your probably rolling your eyes saying “yeah, I learned that after 2 minutes with Grails… so what”. Well now lets say you want to make a result list that contained all Animals that live in Water. Doing a search by Type will not work this time… in walks Criteria Builder

Setting a Criteria Builder up is pretty straight forward.  First define the Criteria:

def waterCriteria = Animal.createCriteria()

Now you can perform a search Criteria against the Animal domain.  We are going to do a search on the Animal domain looking for Animals with a Habitat description of ‘ocean’ and ‘fresh water’.

def waterAnimalList = waterCriteria{
     habitat{
          or{
               eq("description", "ocean")
               eq("description", "fresh water")
             }
     }
}

Results:

  • otter
  • northern pike
  • shark
  • whale

VIOLA!  That simple!  If this is not a big deal to you… f off, however, if your like me and still learning and enjoy working with Grails, then your ok by my books.  Using the Criteria Builder has been like magic to me.  Of course,  Grails offers other options like HQL and SQL.   Lets do another hypothetical.

Lets say you want to get a list of Animals that live in Water and are Mammals.   You will need to perform the search on the Animal domain looking for Animals with a Habitat description of ‘ocean’ and ‘fresh water’ as well as on Type of ‘mammal’.  This is how this is done:

def waterMammalCriteria = Animal.createCriteria()
def waterMammalList =  waterMammalCriteria {
     and{
          habitat{
               or{
                    eq("description", "ocean")
                    eq("description", "fresh water")
                  }
          }
          type{
               eq("description", "mammal")
          }
}

Results:

  • otter
  • whale

Thats it!  Hope this helps at least one person.   Criteria Builder has worked well for me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s