Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

hashCode for EntityManager

Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, hopefully an easier question ...

If i am using the PersistenceContext or PersistenceUnit annotation (perhaps there are 2 different answers to the question) to get an EntityManager (and I am talking javax.persistence.EnitityManager) should there ever be an equivalence on the hashCode method for the EntityManager.

That is let us say I grab entity manager and do operation # 1 then some time later I grab entity manager and do operation #2 is hashCode equal for each entity manager. I have been assuming that I am using the same entity manager and not creating a new one each time when grabbing it with the above scheme.

Greg Werner
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have partially answered my own question, but guys please if you have anything to add ...

If in fact you are still using the same Entity Manager, you do get equal hashCode values. I guess what I don't understand is when is the same EntityManager used and when the EntityManager is reused and when it is created. So let me show a code snippet:

@Transactional(propagation=Propagation.REQUIRED, read_only = false)
private EntityManager getEntityManager() {
if ((entityManager == null) || (!entityManager.isOpen())) {
entityManager = entityFactoryManager.createEntityManager()
// line to give us edit privileges to data base intentionally omitted
return entityManager;

I have logged hashCode values (not shown) in this method and maybe 6 or 7 consecutive calls show the same hashCode and reuse of the existing entityManager then all of the sudden a new entityManager is created. It is the what happens behind the scenes to these Entity Manager objects that has me a bit on edge. My instinct would be never to close the entity manager, but some high level process either Spring Transactions or javax.persistence seems to think otherwise.
Are you here to take over the surface world? Because this tiny ad will stop you!
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic