Mr Kohn Doew
Mr Kohn Doew

Reputation: 277

No Persistence provider for EntityManager named persistenceUnit

I'm working on REST server and learning EJB\hibernate at the same time. When service call DAO I faced with an issue that it can not find my persistence unit.

@Stateless
public class HotelDAO {

@PersistenceContext(unitName = Constants.PERSISTENCE_UNIT)
private EntityManager em;


public List<HotelsEntity> getAll() {
// TODO complete me
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<HotelsEntity> criteria = builder.createQuery(HotelsEntity.class);
    Root<HotelsEntity> root = criteria.from(HotelsEntity.class);
    criteria.select(root);
    TypedQuery<HotelsEntity> resultQuery = em.createQuery(criteria);
    return resultQuery.getResultList();
}
}

In this case I get "Unable to retrieve EntityManagerFactory for unitName persistenceUnit"

Then I try this:

@Stateless
public class HotelDAO {

public List<HotelsEntity> getAll() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager em = emf.createEntityManager();

    // TODO complete me
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<HotelsEntity> criteria = builder.createQuery(HotelsEntity.class);
    Root<HotelsEntity> root = criteria.from(HotelsEntity.class);
    criteria.select(root);
    TypedQuery<HotelsEntity> resultQuery = em.createQuery(criteria);
    return resultQuery.getResultList();
}
}

In this case I get "No Persistence provider for EntityManager named persistenceUnit".

I checl similar issues at the stackoverflow:

Do you have any gueses?

<?xml version="1.0" encoding="UTF-8"?>

<persistence-unit name="persistenceUnit" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>jdbc/HospitalityDataSource</jta-data-source>
    <class>com.example.model.AmmenitiesEntity</class>
    <class>com.example.model.HotelPropertyEntity</class>
    <class>com.example.model.HotelsEntity</class>
    <class>com.example.model.InventoriesEntity</class>
    <class>com.example.model.ReservationEntity</class>
    <class>com.example.model.RoomEntity</class>
    <class>com.example.model.RoomPropertyEntity</class>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mydb"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="dbroot"/>
        <property name="hibernate.connection.password" value="password"/>
    </properties>
</persistence-unit>

pom.xml

 <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.5.Final</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>annotations-api</artifactId>
        <version>6.0.29</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.ejb</artifactId>
        <version>3.1</version>
    </dependency>
</dependencies>

Structure

Upvotes: 0

Views: 2332

Answers (1)

Carles Figuera
Carles Figuera

Reputation: 91

If I'm not wrong, I think persistence.xml must be in src/main/resources/META-INF/persistence.xml

Upvotes: 1

Related Questions