【问题标题】:Not able to update my newly added item in collection to database using grails and JPA无法使用 grails 和 JPA 将集合中新添加的项目更新到数据库
【发布时间】:2013-04-23 10:09:31
【问题描述】:

我在长期更新 grails 中的一个域(图书馆)时遇到问题,我尝试在添加新书后更新我的图书馆,但添加的书没有得到保存。

I have mapping of bookCollection in my domain Class Library as below:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "Library")
    Collection<Book> bookCollection;


I am trying to add a new book to my Library(Initially there is no book in  bookColletion):

Library library = Library.get(1)
Book book=Book.get(1)
library.bookCollection.add(book)

and update it:
library.save(flush:true,failOnError: true)

Library is got saved but  my book is not.

I am using grails and JPA.

谢谢

【问题讨论】:

    标签: java hibernate grails jpa grails-orm


    【解决方案1】:

    您没有设置关系的另一端:librarybook

    Library library = Library.get(1)
    Book book=Book.get(1)
    library.bookCollection.add(book)
    book.library = library; // <--- sets the other side of the relation
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多