Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3222844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:02:17+00:00 2026-05-17T16:02:17+00:00

As I’m a bit new to Grails, I’m wondering how I can iterate through

  • 0

As I’m a bit new to Grails, I’m wondering how I can iterate through the current data i have saved in the database to check if the information already exists.

For instance, lets say I have a domain class for Books and I create an action that automatically adds more books but I want to check if the book.title already exists so I don’t add it again.

Side note
I’m just using the default database (whatever is used when the project is set to production mode)


Edit

I’ll post my domains so it is a bit easier to understand. Instead of book.title, I changed it to where book belongs to author. So I only want the author added once but able to add many books for it. The issue happens in an action i created in the controller.

Author Domain:

class Author {

    static hasMany = [books:Book]

    String authorName
    String notes
    String age

    String toString() { authorName }


    static constraints = {
        authorName()
        notes(maxSize:500)
        age()
    }
} 

Book Domain:

class Book {

    static belongsTo = Author

    String toString() { bookNumber }

    Author bookAuthor
    String title
    String numberOfPages


    static constraints = {
        bookAuthor()
        title()
        numberOfPages()
    }
}

Book Controller (this is where I’m having issues):

class BookController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    //took out index, create, list, etc. to focus on the once that I'm concerned about


    //this action will simple read a text file and add books and authors
    def gather = {

        def parseData = new parseClient()        //parses text file line by line and puts it into a list  
        def dataHolder = parseData.information   //dataHolder will hold data from text file

        int linesOfData = dataHolder.size()      //get size to iterate and add authors & books

        linesOfData.times {
            def _author = dataHolder.author[it]  //get author - Author
            def _age = dataHolder.age[it]        //get age    - Author

            def _title = dataHolder.title[it]    //get title - Book
            def _pages = dataHolder.pages[it]    //get pages - Book

            def authorInstance                   //create new Author to add
            authorInstance = new Author()        //for some reason I have to create and save AuthorName (can't have other fields) before I can add my Book correctly
            authorInstance.setAuthorName(_author)
            authorInstance.save()

            def bookInstance
            bookInstance = new Book()           //create a new Book to add
            bookInstance.setBookAuthor(authorInstance)
            bookInstance.setTitle(_title)
            bookInstance.setNumberOfPages(_pages)
            bookInstance.save()                 //has to have access to the authorInstance to add correctly which is why i was wondering how to access the database to grab it if it existed

            authorInstance.setAge(_age)        //add whatever data is left for Author
            authorInstance.save()              //save again because cant save it with this information before I add authorInstance to the Book
        }

    }

}

Text File Content:

//You'll notice that author _Scott Davis_ is in here twice.
//I don't want to add two instances of Scott Davis but need to access it to add the book
//the unique constraint makes the value come up as not null but can't be added 

Scott Davis : Groovy Recipes
Bashar Abdul Jawad : Groovy and Grails Recipes
Fergal Dearle : Groovy for Domain-Specific Languages
Scott Davis : GIS for Web Developers: Adding 'Where' to Your Web Applications

So I’m basically looking for a way to add that information and haven’t found a way that seems to work without running into random problems.

Hope this clears my question up a bit, as the original question was a bit broad

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:02:18+00:00Added an answer on May 17, 2026 at 4:02 pm

    In this case, you can put a unique constraint on title and grails will do that for you.

    You can iterate thru the data, but you probably don’t want to load the db if you can avoid it. So you could write a custom query to select the number of books for the title, for example.

    Edit: for your updates

    1. You don’t need to use setters in your controller. Grails adds setters/getters for you dynamically at runtime. If you want to put some logic in your setters, then you can define your own and use them in that case

    2. Have you looked at the grails documentation? http://grails.org/doc/latest/

    you have a static constraints block, but you haven’t defined how you want each property to be constrained. for unique title it would be

     title(unique:true)
    

    if you want to get a list of author names, you can do

    List names = Author.executeQuery('select authorName from Author')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.