2014-04-29 65 views
0

我正在使用REST服務器並同時學習EJB \ hibernate。當服務調用DAO時,我遇到了無法找到我的持久性單元的問題。否EntityManager的持久性提供者名爲persistenceUnit

@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(); 
} 
} 

在這種情況下,我得到 「無法檢索到的EntityManagerFactory爲的unitName persistenceUnit」

那我試試這個:

@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(); 
} 
} 

在這種情況下,我得到「沒有找到EntityManager的命名persistenceUnit持久性提供者」。

我在計算器checl類似的問題:

  • 下persitence.xml META-INF
  • DAO注入到EJB
  • 提供商persistence.xml中提到
  • 我不使用彈簧

您有任何問題嗎?

<?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

+0

你有多個項目嗎?或者一個EJB項目? – Hirak

+0

關於第一個問題:它是一個單一的項目/第二個問題我不明白:我在我的應用程序中使用EJB,如果它是什麼意思 –

+0

因此,您有一個包含EJB和hibernate和persistence.xml的項目嗎?你可以發佈你的項目結構嗎?另一個問題是,你是否也確認JNDI「jdbc/HospitalityDataSource」在服務器中是否可用? – Hirak

回答

1

如果我沒有錯,我想的persistence.xml必須在SRC /主/資源/ ME TA-INF/persistence.xml

+0

你是對的,但現在它拋出java.lang.ClassNotFoundException:org.hibernate.jpa.HibernatePersistenceProvider。我剛剛在資源中移動了xml \ META-INF –

+0

您的pom.xml中是否有hibernate-entitymanager依賴項? –

+0

是的,我有: org.hibernate作爲 的Hibernate的EntityManager 4.3.5.Final

相關問題