reset password
Author Message
lmann2
Posts: 156
Posted 12:14 Feb 12, 2015 |

Hey All,

  I have question.  If I, for example, have a model that includes Library and Book and I go to add new book from the controller that passes the book (I also have a libararyDao and bookDao wired in the controller) to a method in BookDao that uses merge to add a new entry to the book table.  Now, in the same operation I need to update library_books which has the columns library_id and book_id.  I'm confused about the right way to do this...I have two ideas.

I think the right way is to use libraryDao like so libraryDao,getLibrary(1).addBook(newBook); , but it this also seems like it should be a transaction.  So, the other option I think is to write a transactional method in libraryDao that updates libarary_books by passes the newBook object and running some queries.  Did you guys have this problem?  What do you think?  sincerely confused sad 

plakhan
Posts: 37
Posted 12:30 Feb 12, 2015 |
lmann2 wrote:

Hey All,

  I have question.  If I, for example, have a model that includes Library and Book and I go to add new book from the controller that passes the book (I also have a libararyDao and bookDao wired in the controller) to a method in BookDao that uses merge to add a new entry to the book table.  Now, in the same operation I need to update library_books which has the columns library_id and book_id.  I'm confused about the right way to do this...I have two ideas.

I think the right way is to use libraryDao like so libraryDao,getLibrary(1).addBook(newBook); , but it this also seems like it should be a transaction.  So, the other option I think is to write a transactional method in libraryDao that updates libarary_books by passes the newBook object and running some queries.  Did you guys have this problem?  What do you think?  sincerely confused sad 

After adding the new book to the library, you need to save the library object using the transactional method. That should work.

lmann2
Posts: 156
Posted 13:54 Feb 12, 2015 |

Still not sure I understand that because your not adding a new Library object via the merge operation in LibraryDao.  You're adding to a attribute of library in a list.  So, I guess something like creating a book object with the attributes of the lastest book in the LibraryDao method and then creating a Library object with the corresponding id.  Then adding through ...setBooks()?  But how does the merge operation function in that scenario? 

Edit:  So, the process should be to create a library object in the controller (with the desired library to update) and update the object's fields with the book object .  Create a method in the dao to merge this library object.  Everything else is automatic.  I'm not sure this is the best way to do this, but it functions and makes sense to me.  Hope this helps someone. 

Last edited by lmann2 at 15:06 Feb 12, 2015.