简体   繁体   English

Grails GORM域类关系

[英]Grails GORM Domain class relationship

Grails 1.1.1 Goovy 1.5.7 Grails 1.1.1 Goovy 1.5.7

In a relationship such this: 在这样的关系中:

Author 1 -- n Book n -- 1 Publisher 作者1-n图书n-1出版商

Defined in Grails: 在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: 我得到了与助理关联的响应,但发布者在另一个查询中仅具有id和name类:

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. 我得到相反的结果,我有带出版商数据的书,但作者只有ID和类名。

Where is the error in the defined model ? 定义的模型中的错误在哪里? o there is an error in the way to do the queries ? o查询方式有误吗?

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? 问题:如果出版商有hasmanyDomain相关问题,那本书我能够阅读属性吗?

Thanks. 谢谢。 Thanks. 谢谢。

Lazy fetching is used by default with gorm associations. 默认情况下,懒惰获取与gorm关联一起使用。 If you want to enable eager fetching, you can modify the ORM DSL by adding the following mappings block to your Author domain class: 如果要启用急切获取,可以通过将以下映射块添加到Author域类来修改ORM DSL:

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. 对您的Publisher域对象执行相同操作将使您能够完成所需的工作。 You do want to be careful of the consequence that you may load more data than you intend to. 您确实要注意可能会加载比预期更多的数据的后果。

Shouldn't the get() method return what you are looking for? get()方法是否应该返回您要查找的内容? Example: def book2 = Book.get(author) 示例:def book2 = Book.get(作者)

You'd better use Criteria and explicitly define which relations should be loaded eagerly. 您最好使用Criteria并明确定义应该急切加载的关系。 Just mention relation in the query. 只需在查询中提及关系即可。

Example: 例:

def c = Teacher.createCriteria()

List<Teacher> results = c.list {
            subjects {
                attendees {}
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM