开发者

Grails issue with unique/save/update

开发者 https://www.devze.com 2023-02-01 06:52 出处:网络
I\'m having an issue with grails. I have a domain that looks like: class Book { static belongsTo = Author

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 w开发者_Go百科ant 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'
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消