Grails GORM 域类关系

发布于 2024-08-12 21:34:10 字数 1140 浏览 6 评论 0原文

Grails 1.1.1 Goovy 1.5.7

在这样的关系中:

作者 1 -- n 书 n -- 1 出版商

在 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)
  }
}

我想加载一本带有出版商和作者值的书。 当我收到带有查询的书籍时:

def book2 = Book.findAllByAuthor(author)

我得到与作者相关联的响应,但出版商在另一个查询中只有 id 和名称类:

def book3 = Book.findAllByPublisher(publisher)

我检索到相反的结果,我有带有出版商数据但作者的书只有id和类名。

定义的模型错误在哪里? o 查询方式有错误?

编辑:

我需要一种仅使用如下查询来检索值的方法:

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

在这个查询中,我可以管理发布者的值。

问题:如果出版商有一个 hasmanyDomain 相关内容,那么我可以读取该书的属性吗?

谢谢。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缪败 2024-08-19 21:34:10

默认情况下,延迟获取与 gorm 关联一起使用。如果要启用急切获取,您可以通过将以下映射块添加到您的 Author 域类来修改 ORM DSL:

static mapping = {
    books lazy:false
}

或者您可以通过在定义图书关系后添加以下代码来更改域对象中的获取模式。

static fetchMode = [books:"eager"]

对您的发布者域对象执行相同的操作应该可以让您完成您想要的任务。您确实要小心可能会加载比预期更多的数据的后果。

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.

温柔一刀 2024-08-19 21:34:10

get() 方法不应该返回您正在寻找的内容吗?
示例: def book2 = Book.get(author)

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

还不是爱你 2024-08-19 21:34:10

您最好使用 Criteria 并明确定义应立即加载哪些关系。只需在查询中提及关系即可。

例子:

def c = Teacher.createCriteria()

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

You'd better use Criteria and explicitly define which relations should be loaded eagerly. Just mention relation in the query.

Example:

def c = Teacher.createCriteria()

List<Teacher> results = c.list {
            subjects {
                attendees {}
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文