views:

483

answers:

2

Grails 1.1.1 Goovy 1.5.7

In a relationship such this:

Author 1 -- n Book n -- 1 Publisher

Defined in Grails:

class Author {        
  String firstName
  String lastName

  static hasMany = [books: Book]       

  static constraints = {
      books(nullable: true)
  }
}

class Book {        
  String title
  Author author
  Publisher publisher

  static constraints = {
    author(nullable: true)
    publisher(nullable: true)
  }
}

class Publisher {

  String name

  static hasMany = [books: Book]

  static constraints = {
      books(nullable: true)
  }
}

I want to load a Book with the values of Publisher and Author. When i get a Book with the query:

def book2 = Book.findAllByAuthor(author)

I get the response with the autor assosiated but the publisher only have the id and name class in the other query:

def book3 = Book.findAllByPublisher(publisher)

I retrieve me the inverse result,i have the book with the publisher data but the author only have the id and the class name.

Where is the error in the defined model ? o there is an error in the way to do the queries ?

Edit:

I need the way to retrieve the values only with the query like this:

def book2 = Book.findAllByAuthor(author, [fetch:[publisher:'eager']])

In this one I can manage the value of publisher.

Question: If publisher had a hasmany or Domain related, getting the book I'm able to read the attributes?

Thanks. Thanks.

+1  A: 

Lazy fetching is used by default with gorm associations. If you want to enable eager fetching, you can modify the ORM DSL by adding the following mappings block to your Author domain class:

static mapping = {
    books lazy:false
}

or you could change the fetch mode in the domain object by adding following code after your books relationship is defined.

static fetchMode = [books:"eager"]

Doing the same to your Publisher domain object should allow you to accomplish what you want. You do want to be careful of the consequence that you may load more data than you intend to.

Lloyd Meinholz
Thanks.But I'm interested in the other side of the question because of you are saying: You do want to be careful of the consequence that you may load more data than you intend to. - o there is an error in the way to do the queries ?I found a solution adding the parameter `fetch` to the quey : - def book2 = Book.findAllByAuthor(author, [fetch:[publisher:'eager']]))I get the publisher too.What Can I do if the publisher had any other entity that i need to retreive? adding and adding parameters.Thank you.
alcoholitro
If you didn't define the other objects that Publisher references with eager fetch mode, then you will have to iterate over the object as far as I know.
Lloyd Meinholz
I don't want to iterate over and over that's my problem. Too many acces to the DDBB.If I found something I'll post it here.Thank you for your time and quickly good answers.
alcoholitro
A: 

Shouldn't the get() method return what you are looking for? Example: def book2 = Book.get(author)

timarmandpour
It's not well formated: `def book2 = Book.get(author)`This way: `def book2 = Book.findByAuthor(author)`I've only the data from the book and the author domain, not from the Publisher.
alcoholitro