This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode for EntityManager

 
Greg Werner
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
 
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic