Chapter 5. Retrieving the data you need

 

This chapter covers

  • Generating sample data
  • Querying the database without SQL
  • Unlocking the power of advanced querying features

After a few chapters you have a fully working web application that stores and retrieves data in a relational database. This forms a great base for further development, because you can fully interact with the data model before the real application is ready. You can, for example, make changes to the data via the scaffolding UI to test a feature that you’re developing, such as a search form.

Where do you go next? You don’t want to expose users to the scaffolding UI because it doesn’t exactly provide a great user experience. You need to build a friendlier UI. Let’s start with a simple home page. What should go on that page? A good starting point is a list of Hubbub posts, and for that you need to query the database.

This chapter introduces various ways of querying a database in Grails. Once you’ve finished it, you’ll know how to fetch posts from the last 24 hours submitted by people whom the current user is following! You’ll also develop a basic search form for Hubbub that provides a basis for exploring the different types of queries.

Before we begin, it’s useful to have data in the database so that your queries can return results. A convenient way to do that is to populate the database on application startup.

5.1. Setting up the data and search form

5.2. Writing Where queries

5.3. When Where queries aren’t suitable

5.4. Summary and best practices

sitemap