【问题标题】:Grails hasMany associations not saving to join tableGrails hasMany关联不保存到连接表
【发布时间】:2020-08-26 08:00:35
【问题描述】:

我有一个 Grails 4.0.3 应用程序。我只是开始测试一些域映射。看来我无法创建一对多和多对多的关系。现在我正在尝试一个简单的一对多关系。

这是我的实现:

    class Author {

    String name
    static hasMany = [books : Book]
    static constraints = {
    }
}

class Book {

    String title

    static constraints = {
    }
}

我已经启动了:

def author = new Author('name':"Author")
        author.addToBooks(new Book('title':"Book1"))
        author.addToBooks(new Book('title':"Book2"))
        author.save()

我在表中有作者和书籍,但没有关系。

以下代码给出空列表。

def author = Author.get(1)
def books = author.books

不知道我错过了什么。我已经阅读了很多类似于这个问题的答案,有些人建议使用单独的连接类。但是我正在升级我现有的应用程序,并且有很多地方正在使用 addTo 语法。所以我想坚持下去。 至少,我想知道为什么这不起作用,因为这是标准实现。

我还在图片中显示了生成的连接表结构。 看来连接表的结构也不太对劲。我的理解是它应该创建一个名称为 author_books 的表,其中包含键 author_id 和 book_id。

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    您没有显示足够的上下文来确定,但我希望保存发生在有问题的上下文中,可能是因为会话没有被刷新。验证这一点的一种方法是在保存时刷新会话。

    https://github.com/jeffbrown/prabinupretirelationship查看项目。

    https://github.com/jeffbrown/prabinupretirelationship/blob/2fcf133f65309e449b408f4152f3a36fbb053a3e/grails-app/domain/prabinupretirelationship/Author.groovy

    package prabinupretirelationship
    
    class Author {
    
        String name
        static hasMany = [books : Book]
        static constraints = {
        }
    }
    

    https://github.com/jeffbrown/prabinupretirelationship/blob/2fcf133f65309e449b408f4152f3a36fbb053a3e/grails-app/domain/prabinupretirelationship/Book.groovy

    package prabinupretirelationship
    
    class Book {
    
        String title
    
        static constraints = {
        }
    }
    

    https://github.com/jeffbrown/prabinupretirelationship/blob/2fcf133f65309e449b408f4152f3a36fbb053a3e/grails-app/init/prabinupretirelationship/BootStrap.groovy

    (注意:我实际上不会这样做,但我正在尝试使用与您询问的方法接近的代码。更好的想法是将持久性逻辑移动到 GORM 数据服务中(@987654325 @) 其中事务和会话都将由 GORM 管理)。

    package prabinupretirelationship
    
    class BootStrap {
    
        def init = { servletContext ->
            Author.withTransaction {
                def author = new Author('name': "Author")
                author.addToBooks(new Book('title': "Book1"))
                author.addToBooks(new Book('title': "Book2"))
                author.save(flush: true)
            }
        }
        def destroy = {
        }
    }
    

    logSqlhttps://github.com/jeffbrown/prabinupretirelationship/blob/2fcf133f65309e449b408f4152f3a36fbb053a3e/grails-app/conf/application.yml#L106 处设置为true

    当应用程序运行时,以下 SQL 语句被发送到数据库,包括填充连接表:

    Hibernate: insert into author (id, version, name) values (null, ?, ?)
    Hibernate: insert into book (id, version, title) values (null, ?, ?)
    Hibernate: insert into book (id, version, title) values (null, ?, ?)
    Hibernate: insert into author_book (author_books_id, book_id) values (?, ?)
    Hibernate: insert into author_book (author_books_id, book_id) values (?, ?)
    

    【讨论】:

    • 仅供参考...github.com/jeffbrown/prabinupretirelationship/commit/… 的提交引入了 GORM 数据服务,这是一个更好的主意。
    • 你说得对。问题出在保存上下文中。我没有刷新会话,也没有将代码包装在事务中。如果没有用事务包装代码,它会给我错误“没有事务正在进行中”保存(刷新:真)。这就是为什么我只是在没有刷新的情况下进行了 save() 。非常感谢您的支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多