aspose file tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EntityManagerFactory vs EntityManager Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Reply Bookmark "EntityManagerFactory vs EntityManager" Watch "EntityManagerFactory vs EntityManager" New topic
Author

EntityManagerFactory vs EntityManager

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

Guys,

What is the difference between injecting a PersistenceUnit to an EntityManagerFactory instance and injecting a PersistenceContext to an EntityManager instance?


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
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.


SCJP 5 (98%) - SCBCD 5 (98%)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

Which one to use for a Java SE application?
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
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

Joined: Sep 26, 2006
Posts: 8795

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

Joined: Apr 27, 2008
Posts: 342
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

Joined: Sep 26, 2006
Posts: 8795

Good. Thanks for the help!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: EntityManagerFactory vs EntityManager
 
Similar Threads
Question about thread safe of EntityManager
deploying multiple ejb module
Dependecy injection EJB3
spring wont inject the entitymanager
Difference in @PersistenceUnit and @PersistenseContext