Using JSF, JPA and DAO. Without Spring?

So far, I was still working with JSF and JPA without DAO. Now I would like to use DAO. But how can I initialize an EntityManager in DAO classes?

public class AdresseHome { @PersistenceContext private EntityManager entityManager; public void persist(Adresse transientInstance) { log.debug("persisting Adresse instance"); try { entityManager.persist(transientInstance); log.debug("persist successful"); } catch (RuntimeException re) { log.error("persist failed", re); throw re; } } } 

Do I use Spring or is there a solution that works without Spring?

Thank.

+5
java spring jpa dao jsf
Dec 07 '09 at 16:42
source share
3 answers

If your container does not enter you EntityManager, you can get it with:

 EntityManagerFactory factory; factory = Persistence.createEntityManagerFactory("jpatest"); EntityManager em = factory.createEntityManager(); 

Where is "jpatest" from the unit defined in your persistence.xml

+5
Dec 07 '09 at 16:56
source share

Java EE 5 does not support injection into an uncontrolled component, so without Spring you will have to use a court-driven application manager here (and therefore manage your life cycle at the application level).

Actually, Java EE 5+ really does not protect the use of the DAO template (is JPA Killed DAO really a good article about this topic) and the wrapping of the object manager that implements the Domain Store template, which does pretty much what the DAO does in the DAO doesn't make sense in my opinion.

+5
Dec 07 '09 at 16:47
source share

Another option for you is to implement your DAO as an SLSB. This way you can enter EntityManger, rather than create it. But it has its bad effects, like too many beans sessions. chains of beans, etc., which is a kind of bad partition.

0
May 9 '11 at 10:52 AM
source share



All Articles