• 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

EntityManagerFactory vs EntityManager

 
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

What is the difference between injecting a PersistenceUnit to an EntityManagerFactory instance and injecting a PersistenceContext to an EntityManager instance?
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

if you inject the entity manager, it is created, propagated and closed automatically by the container. If you inject the entity manager factory you have to perform all these steps programmatically. Therefore you'll usually inject the entity manager.

Reasons for using an entity manager factory are:

1. In a multi-threaded environment you'll prefer to use thread-safe instance variables. An entity manager factory is thread-safe, while an entity manager is not.

2. You want to use an application-managed persistence context.
 
Joe Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which one to use for a Java SE application?
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without a container environment you'll have to use an EntityManagerFactory and application-managed persistence context.

The EntityManagerFactory can't be injected, but has to be created by the "Persistence" class like

EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnitName");
 
Joe Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ralph Jaus:
Without a container environment you'll have to use an EntityManagerFactory and application-managed persistence context.

The EntityManagerFactory can't be injected, but has to be created by the "Persistence" class like

EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnitName");



That"s fine, but after this do I have to say

emf.createEntityManager() in a Java SE application?
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Furthermore you have to close the EntityManagerFactory and the EntityManager when your program finishes.
[ November 11, 2008: Message edited by: Ralph Jaus ]
 
Joe Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good. Thanks for the help!
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic