Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JSF, JPA and DAO. Without Spring?

till now i still worked with JSF and JPA without DAOs. Now i'd like to use DAOs. But how can i initialize the EntityManager in the 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;
        }
    }
}

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

Thanks.

like image 933
ich-bin-drin Avatar asked Dec 07 '09 16:12

ich-bin-drin


People also ask

What will happen if you will use @service over a DAO?

In your scenario, it has no impact on application flow whether you use @Service or @Repository, application will work as they are elligible for autowiring. But standard practice is to use @Repository for dao classes.

Is DAO same as repository?

DAO is an abstraction of data persistence. However, a repository is an abstraction of a collection of objects. DAO is a lower-level concept, closer to the storage systems. However, Repository is a higher-level concept, closer to the Domain objects.

Is DAO used in spring boot?

Java provides a comprehensive and modular framework for data access-Spring Boot, coupled with DAO-that makes integrating with different persistent layers easy. While developing an enterprise application, one of the most important aspects is to deal with the DB like connection management, transaction management etc.


1 Answers

If your container doesn't inject the EntityManager for you, you can get one with:

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

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

like image 58
nos Avatar answered Oct 07 '22 16:10

nos