Need help: Trying to load data from DB, edit it, then save.
brandonsimpson Apr 13, 2009 10:49 PMIt sounds easy enough, and I thought it would be until I started trying to implement it. The nature of my page is that it is highly interactive, the user can click on Yes/No buttons to selectively hide/show other parts of a really large page. This functionality uses ICEFaces so the state and rendering is handled on the server...this is generally working well, but what this also means is that the page is going through many requests/responses in between the time that it is initially loaded and when the user ultimately saves the data.
Due to the fact that the page is interactive with multiple requests/responses typically taking place, I figured that the action component I'm using to keep track of all the data should be in PAGE scope.
I set up a page action to execute a method on my action component when the page is navigated which loads the entity I want to edit from the database. It only does this loading once.
All of this works fine...the show/hide functionality works great (with no javascript thanks to ICEFaces) and the state is tracked properly on the page. However, the problem occurs when I click the save button. The entity manager complains that the entity is detached.
When I thought about the way Seam handles the EntityManager and transactions, I figure that despite using PAGE scope for my component, I'm getting a new EntityManager on each request and thus when I go to save after editing I'm trying to save to a different EntityManager than the one I loaded the entity from resulting in the detached instance. Is this correct? So if that is correct, what is the best way to deal with this? I tried to mess around with merge, admittedly I'm new to persistence, and from what I've read that should be avoided. Is it bad practice to scope the EntityManager to PAGE scope or is this even possible with Seam controlling transactions and managing the EntityManager?
PAGE scope seemed correct to me, because all of this work is being done on a single page, but to make it work until I figure out the "right" way, I decided to scope the action component to CONVERSATION, and start a long running conversation on page load and set the EntityManager to manual flush. Everything works fine after that, but I can't help feel its a bit overkill for just editing some data on a single page.
So is there a best practice that my noobiness has completely missed? Thanks in advance for any help...I'm finding that reading about Seam and using it in a real project are quite difference! =)
Due to the fact that the page is interactive with multiple requests/responses typically taking place, I figured that the action component I'm using to keep track of all the data should be in PAGE scope.
I set up a page action to execute a method on my action component when the page is navigated which loads the entity I want to edit from the database. It only does this loading once.
All of this works fine...the show/hide functionality works great (with no javascript thanks to ICEFaces) and the state is tracked properly on the page. However, the problem occurs when I click the save button. The entity manager complains that the entity is detached.
When I thought about the way Seam handles the EntityManager and transactions, I figure that despite using PAGE scope for my component, I'm getting a new EntityManager on each request and thus when I go to save after editing I'm trying to save to a different EntityManager than the one I loaded the entity from resulting in the detached instance. Is this correct? So if that is correct, what is the best way to deal with this? I tried to mess around with merge, admittedly I'm new to persistence, and from what I've read that should be avoided. Is it bad practice to scope the EntityManager to PAGE scope or is this even possible with Seam controlling transactions and managing the EntityManager?
PAGE scope seemed correct to me, because all of this work is being done on a single page, but to make it work until I figure out the "right" way, I decided to scope the action component to CONVERSATION, and start a long running conversation on page load and set the EntityManager to manual flush. Everything works fine after that, but I can't help feel its a bit overkill for just editing some data on a single page.
So is there a best practice that my noobiness has completely missed? Thanks in advance for any help...I'm finding that reading about Seam and using it in a real project are quite difference! =)