title: How to create objects with multiple relationships in BootStrap.groovy init
body: <p>I want to generate some dummy content that i can test with in BootStrap.groovy, but I'm struggling to get it to work when I have more than one relationship.</p>
  
  <p>I have 3 domain classes: User, Book and Author</p>
  
  <p>A user has many books.
  An author has many books.</p>
  
  <p>I have it set up like the following:</p>
  
  <pre><code>class User {
      static hasMany = [books:Book]
  }
  class Author {
      static hasMany = [books:Book]
  }
  class Book {
      static belongsTo = [author:Author, user:User]
  }
  </code></pre>
  
  <p>I want BootStrap.groovy to create and save a few books but i cant get it to work. Heres what I have:</p>
  
  <pre><code>def init = { servletContext -&gt;
      def u1 = new User(login:"mrx", password:"p", name:"Mr X")
      def u2 = new User(login:"mry", password:"p", name:"Mr Y")
  
      def a1 = new Author(name:"Mary Shelley")
      def a2 = new Author(name:"Stephen King")
  
      def b1 = new Book(title:"Frankenstein")
      def b2 = new Book(title:"The Shining")
      def b3 = new Book(title:"Misery")
  
      a1.addToBooks(b1)
      a2.addToBooks(b2)
      a2.addToBooks(b3)
  
      u1.addToBooks(b1)
      u1.addToBooks(b3)
      u2.addToBooks(b2)
  
      a1.save()
      a2.save()
  
      u1.save()
      u2.save()
  }
  </code></pre>
  
  <p>When i view Book/index (which is the scaffold and should just display a list of all books) I dont get any errors, but no books appear in the list. What am I doing wrong?</p>