AFTERSHOX - Tariq Ahmed on Technology :: Management :: Business
  • About Me
  • Resume
  • Contact
  • Learning List
AFTERSHOX - Tariq Ahmed on Technology :: Management :: Business
About Me
Resume
Contact
Learning List
  • About Me
  • Resume
  • Contact
  • Learning List
Groovy / Grails, Uncategorized

Grails wildcard search on two fields

So I’m still in learning mode w/Groovy & Grails, and I have basic CRUD for managing users. The user domain class has the properties that you’d expect, including a FirstName and LastName:

class User
{
  String firstName
  String lastName
}

So I wanted to create a simple right side wildcard search that would effectively be the same as this in SQL Server:

where (firstName like '#searchTerm#%' or lastName like '#searchTerm#%')

In Grails you have a wrapper around Hibernate called GORM, which provides a mechanism called Dynamic Finders.

Imagine if you had to write a collection of functions for finding and retrieving one or many records based on each property (findByFirstName(), findAllByFirstName(), findFirstNameLike(), findByAgeGreaterThan(), etc…), it could take awhile. 🙂

But with GORM, it’s as if a little elf in the middle of the night coded all night long to create these functions for you. You never see them, but they’re magically there!

There’s a whole “with criteria” mechanism and the Hibernate Query Language (HQL), but I found the simplest way was just to do this:

def userList =
User.findAllByFirstNameLikeOrLastNameLike("${params.searchTerm}%",
"${params.searchTerm}%")
11/24/2011by Tariq Ahmed

Who is this dude?

Tariq Ahmed Howdy! My name is Tariq ("Ta-Rick") Ahmed, and a Director of Software Engineering at New Relic where my time is focused on creating developer experiences through our developer websites, APIs, CLIs, SDKs, and ability to build your own custom apps on the New Relic One platform. I'm most passionate about finding amazing people, growing talent, and building amazing teams in order to accomplish meaningful breakthroughs in technology that ultimately create great user experiences.
Twitter feed is not available at the moment.

Categories

  • Agile (11)
  • Business Intelligence (1)
  • Career (7)
  • ColdFusion (7)
  • Collaboration (1)
  • Featured (5)
  • Flex and AIR (2)
  • Groovy / Grails (6)
  • I.T Systems (1)
  • Innovation (4)
  • Leadership (6)
  • Management (8)
  • Project Management (1)
  • Software Development (1)
  • Startups (2)
  • Technology (7)
  • Uncategorized (2)

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Blogroll

  • LinkedIn
  • Teletrac Navman

"This blog is all about sharing thoughts and experiences in my journey as a technology leader. From the technology itself to the processes, practices, and teams needed to make it happen."

Find more at LinkedIn.Â