Grails issue with unique/save/update

試著忘記壹切 提交于 2019-12-08 11:15:19

问题


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?


回答1:


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'
    }
}


来源:https://stackoverflow.com/questions/4550277/grails-issue-with-unique-save-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!