Grails问题与独特/保存/更新 [英] Grails issue with unique/save/update

查看:140
本文介绍了Grails问题与独特/保存/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Grails问题。我有一个如下所示的域:

  class Book {

static belongsTo = Author

String toString(){title}

作者bookAuthor
字符串标题
字符串currentPage


静态约束= {
bookAuthor()
title(unique:true)
currentPage()
$ b}
}

主要要注意的是我有 title(unique:true)来避免两次添加同一本书。但是,这是造成问题的原因。在我创建的控制器中:

  def populate = {

def bookInstance = new Book()

def dir ='C:/currentBooks.txt'
def bookList

bookList = readFile(dir)//读取文件并将值压入bookList

int numOfBooks = bookList.size()

numOfBooks.times {

bookInstance.setBookAuthor(bookList.author [it])
bookInstance .setTitle(bookList.title [it])
bookInstance.setCurrentPage(bookList.title [it])
bookInstance.save()
}

}

我调用populate来读取文件并用新书籍填充数据库。问题是我想用新值更新它。例如,可以说这本书已经存在于数据库中,但是我已经读了更远的书,并且想要更改 currentPage ,以便数据在文件中被更改并且填充被调用,但是不会更新页面,因为标题已经存在。



有人可以解释如何用新值更新结果吗?

currentBooks.txt 中。



一旦你有一个ID,你可以尝试从数据库加载一个现有的记录。如果没有,请创建一个新的。例如:

  def dir ='C:/currentBooks.txt'
def bookList

bookList = readFile(dir)//读取文件并将值推入bookList

int numOfBooks = bookList.size()

numOfBooks.times {
$如果(!bookInstance){
bookInstance = new Book()
}

bookInstance。 setBookAuthor(bookList.author [it])
bookInstance.setTitle(bookList.title [it])
bookInstance.setCurrentPage(bookList.title [it])
bookInstance.save()

或者,您可以使用标题作为id。如上所述,这是一个坏主意,但它可以避免跟踪一个单独的id并更改 currentBooks.txt 的格式。使用Book定义如下,您可以调用 Book.get(bookList.title [it])

  class Book {

static belongsTo = Author

String toString(){title}

作者bookAuthor
字符串标题
字符串currentPage
$ b $静态约束= {
bookAuthor()
标题(唯一:true)
currentPage()
}

static mapping = {
id name:'title',generator:'assigned'
}
}


I'm having an issue with grails. I have a domain that looks like:

class Book {

    static belongsTo = Author

    String toString() { title }

    Author bookAuthor
    String title
    String currentPage


    static constraints = {
        bookAuthor()
        title(unique:true)
        currentPage()

    }
}

The main thing to note is that I have title(unique:true) to avoid from adding the same book twice. However, this is causing issues. In the controller I have created:

def populate = {

    def bookInstance = new Book()

    def dir = 'C:/currentBooks.txt'
    def bookList

    bookList = readFile(dir)  //read file and push values into bookList

    int numOfBooks = bookList.size()

    numOfBooks.times {

        bookInstance.setBookAuthor(bookList.author[it])
        bookInstance.setTitle(bookList.title[it])
        bookInstance.setCurrentPage(bookList.title[it])
        bookInstance.save()
    }

}

I call populate to read a file and populate the database with new Books. The problem is that I want to update it with new values. For instance, lets say that the book already exists in the database but I have read farther into the book and want to change the currentPage so the data is changed in the file and populate is called but doesn't update the page because the title already exists.

Can someone explain how to update the results with the new values?

解决方案

First of all, you need a key for your Book domain object. You have the title marked as unique, which suggests you want to use that to uniquely identify a Book. I'd recommend against that (what happens when two books have the same title?) and use the id grails provides by default. That means you'll have to store the id in your currentBooks.txt in addition to your other fields.

Once you've got an id, you can try loading an existing record from the database. If not, create a new one. For Example:

def dir = 'C:/currentBooks.txt'
def bookList

bookList = readFile(dir)  //read file and push values into bookList

int numOfBooks = bookList.size()

numOfBooks.times {

    def bookInstance.get(bookList.id[it])
    if (!bookInstance) {
        bookInstance = new Book()
    }

    bookInstance.setBookAuthor(bookList.author[it])
    bookInstance.setTitle(bookList.title[it])
    bookInstance.setCurrentPage(bookList.title[it])
    bookInstance.save()
}

Alternatively, you could use the title as the id. This is a bad idea as indicated above, but it saves having to keep track of a separate id and change the format of currentBooks.txt. With Book defined as below, you could call Book.get(bookList.title[it]):

class Book {

    static belongsTo = Author

    String toString() { title }

    Author bookAuthor
    String title
    String currentPage

    static constraints = {
        bookAuthor()
        title(unique:true)
        currentPage()
    }

    static mapping = {
        id name: 'title', generator: 'assigned'
    }
}

这篇关于Grails问题与独特/保存/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆