Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
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
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top