gpt4 book ai didi

grails - Grails:如何使用多列插入多对多?

转载 作者:行者123 更新时间:2023-12-02 15:21:19 25 4
gpt4 key购买 nike

authorbook和带有额外的author_books列的联接表royalty为例:

CREATE TABLE 'author_books' (
'author_id` bigint(20) NOT NULL,
'book_id' bigint(20) NOT NULL,
'royalty' int NOT NULL,
PRIMARY KEY ('author_id', 'book_id') ,
INDEX 'FK24C812F6183CFE1B' ('book_id'),
INDEX 'FK24C812F6DAE0A69B' ('author_id')

)

如果我没有db-reverse-engineer插件生成AuthorBooks类,则生成的代码将是:
class Author {
String name
static hasMany = [books: Book]
}

class Book {
String title
static hasMany = [authors: Author]
static belongsTo = [Author]
}

如下代码:
def author1 = new Author(name: 'author1').addToBooks(new Book(title: 
'book1')).save(flush: true)

会在 author表中插入一行,在 book表中插入一行,但是由于 author_books不能为null,因此无法插入到 royalty中。

但是,如果让db-reverse-engineer插件生成AuthorBooks类,则生成的代码将是:
class Author {
String name
static hasMany = [authorBookses: AuthorBooks]
}

class Book {
String title
static hasMany = [authorBookses: AuthorBooks]
}

class AuthorBooks implements Serializable {
Long authorId
Long bookId
Integer royalty
Author author
Book book

int hashCode() {
def builder = new HashCodeBuilder()
builder.append authorId
builder.append bookId
builder.toHashCode()
}

boolean equals(other) {
if (other == null) return false
def builder = new EqualsBuilder()
builder.append authorId, other.authorId
builder.append bookId, other.bookId
builder.isEquals()
}

static belongsTo = [author: Author, book: Book]

static mapping = {
author insertable: false // these insertable and updateable codes were manually added
author updateable: false // otherwise it would not run
book insertable: false
book updateable: false
id composite: ["authorId", "bookId"]
version false
}
}

在这种情况下,我无法调用 author.addToAuthorBooks(new AuthorBooks( )),因为 author_books不能将 author_idbook_id设置为 null。最后,我需要执行以下操作才能使其正常工作:
def author1 = new Author(name: 'author1').save(flush: true)
def book1 = new Book(title: 'book1').save(flush: true)
def authorbook1 = new AuthorBooks(authorId: author1.id, bookId: book1.id,
royalty: 50, author: author1, book: book1).save(flush: true)

这是我可以接受的。但是在Author和Book类中拥有hasMany关联有什么好处呢?还有更好的方法吗?理想情况下,跟随以下操作会很酷
def author1 = new Author(name: 'author1').addToBooks(book: new Book(title: 'book1'), 
royalty: 50).save(flush: true)

最佳答案

我建议您进行一些更改,特别是在您的AuthorBooks域中:

class Author {
String name
static hasMany = [authorBookses: AuthorBooks]
}

class Book {
String title
static hasMany = [authorBookses: AuthorBooks]
}

class AuthorBooks implements Serializable {
Integer royalty
Author author
Book book

int hashCode() {
def builder = new HashCodeBuilder()
builder.append author.id
builder.append book.id
builder.toHashCode()
}

boolean equals(other) {
if (other == null) return false
def builder = new EqualsBuilder()
builder.append author.id, other.author.id
builder.append book.id, other.book.id
builder.isEquals()
}

static belongsTo = [author: Author, book: Book]

static mapping = {
id composite: ["author", "book"]
version false
}
}

为书籍和作者添加其他ID是多余的。我不确定要提出的建议,但值得尝试以下方法:
def author1 = new Author(name: 'author1', authorBookses: new HashSet())
def book1 = new Book(title: 'book1', authorBookses: new HashSet())
def authorbook1 = new AuthorBooks(royalty: 50, author: author1, book: book1)

author1.authorBookses(authorbook1)
book1.authorBookses(authorbook1)

authorbook1.save(flush: true)

关于grails - Grails:如何使用多列插入多对多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35510646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com