Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
AW: [eclipselink-users] When are Entities Equal?


I have a question about when to work with the same EntityManager and when to close it and get a new one.
The JPA examples I found where quite different, sometimes the EntityManager is created and used the whole time in the example, and sometimes for each query a new EntityManager is created (like in the TopLink Essentials examples ).
An EntityManager is not threadsafe, ok, but what if I use it sequentially?
For example:
If I have a Web Application is it better to create an EntityManager for every User Session (there wont be many), reuse it for every read operation and only create other EntityManager if I need to read something simultaneously, or is it better to create/close an EntityManager in every read/write method?

Thanks in advance, best regards,
Alex Schaefer

-------- Original-Nachricht --------
> Datum: Tue, 15 Apr 2008 20:14:32 -0400
> Von: Gordon Yorke <gordon.yorke@xxxxxxxxxx>
> An: EclipseLink User Discussions <eclipselink-users@xxxxxxxxxxx>
> Betreff: Re: [eclipselink-users] When are Entities Equal?
> Hello Tim,
>    Yes, as long as you continue to work within the same EntityManager 
> without clearing then only one instance of any object will exist within 
> that EntityManager/persistence context.  em.find(Employee.class, 
> managerId) == department.getManager().  Unless you are in an Application 
> Server you control when the EntityManager is closed or cleared. 
> Container managed transactional entity managers will get switched at the 
> end of the transaction.  Once you call clear or act on a different 
> EntityManager then the instances from the old EntityManager will not be 
> the same instances as retrieved from the new EntityManager.
> --Gordon
> Tim Hollosy wrote:
> > I've got an entity with a many to one, let's call it employees, with a
> > department.
> >
> > So my employee entity has a Department field.
> >
> > If I'm in the same Entity Manager and I do a find on an Employee to
> > get an instance of a specific Employee Entity, then later in the same
> > EM I do a select all on departments, my list of departments now
> > contains a reference to the same Department that my Employee entity
> > references.
> >
> > This is really great for doing things like databinding on drop down
> > lists, because I can bind the list of departments directly to the
> > department on my employee entity.
> >
> > However, is this safe? Can I count on these being the _same_ entities;
> > not just entities with the same field values all the time? What's
> > eclipselink doing behind the scenes?
> >
> > Thanks,
> > Tim

Gesendet von Yahoo! Mail.
Mehr Möglichkeiten, in Kontakt zu bleiben.

Back to the top