On this point, I just use reflection to get the setters/getter and call the new entity's setters against the getters on my detached entity, it's pretty fast and then you can merge the new entity with the old one.
>No, you can't "refresh" an instance - only replace it. You'd have to write >code in your entity to replace all its properties with those from another >instance. -----Original Message----- From: Daryl Stultz [mailto:[email protected]] Sent: Friday, 18 September 2009 8:14 PM To: [email protected] Subject: Re: EntityManager used in multiple threads On Fri, Sep 18, 2009 at 3:47 AM, Prodoc <[email protected]> wrote: > > How would one implement the re-fetching? Suppose you have a list of entites. You click one row which gets the selected entity and calls a "doEdit" method passing in the entity: public void doEdit(Entity e) { e = getEntityManager().find(Entity.class, e.getId()); } This approach will allow lazy loading, if I want eager, I'll write a query with fetches. This approach assumes a different em for each transaction. If you are using one and only one em, e would be the same instance before and after the find(). In my case, the above also allows me to bind the entity to the GUI for editing without changing values in the entity in the list view (so the editing could be cancelled). Can this be done in a transparent > way? One would not want to have to consider 'should I re-fetch the object > or > not?' before each use of the object. > I imagine there's a way to make it pretty easy but probably not "transparent". The only thing I can come up with is e.g. check if a property is 'null' when > its getter is called and if so re-fetch the object. This, however, will > cause re-fetching being done in a lot of cases where it isn't required > since > a property could be actually 'null' just as well, thus slowing down the > application unnecessary. > As well, you'd keep replacing the entity, so if you've bound the entity to something, you can't re-fetch it (think "final Entity e..."). I have access to unloaded properties disabled so I don' accidently misinterpret null values. is there a way to refer to the object to retrieve again in the same way as using 'this' within > an object? > No, you can't "refresh" an instance - only replace it. You'd have to write code in your entity to replace all its properties with those from another instance. I now see your follow up regarding the factory. If you are still pursuing the multi-threaded em problem, you might consider wrapping the em and synchronizing access to the delegate. Not knowing how often multi-threaded access to the em will occur, it's hard to say what kind of bottleneck this will be. -- Daryl Stultz _____________________________________ 6 Degrees Software and Consulting, Inc. http://www.6degrees.com mailto:[email protected]
