2012-03-09 14 views
2

我想創建使用JSF和EJB 3.0中的Web應用程序。使用EJB 3.0和Hibernate持久性提供應用程序給出了一個UnknownServiceException

我使用普通JSF中,GlassFish服務器,Hibernate作爲持久性我的供應商。我的數據庫是Apache的德比。

這裏我無狀態會話bean如下:

@Stateless 
    @TransactionManagement(TransactionManagementType.CONTAINER) 
    public class StudentServiceBean implements StudentService{ 


    @PersistenceContext(unitName="forPractise") 
    private EntityManager entityMgr; 

    @Resource 
    private SessionContext sessionContext; 

    @Override 
    public List<StudentVO> fetchStudentListOrderByStudentId(boolean flag){ 
     List<StudentEntity> studentList = null; 
     TypedQuery<StudentEntity> studentQuery = null; 
     List<StudentVO> studentVOList = null; 
     String queryDesc = "select s from StudentEntity s order by s.studentId desc"; 
     String query = "select s from StudentEntity s order by s.studentId"; 
     try{ 

      if(!flag){ 
       studentQuery = entityMgr.createQuery(query,StudentEntity.class); 
      }else{ 
       studentQuery = entityMgr.createQuery(queryDesc,StudentEntity.class); 
      }   

      studentList = studentQuery.getResultList(); 
      studentVOList = new ArrayList<StudentVO>(); 
      for(StudentEntity studentE : studentList){    
       studentVOList.add(new StudentVO(String.valueOf(studentE.getStudentId()),studentE.getStudentName(),studentE.getContactNumber())); 
      } 

     }catch(Exception e){ 
      System.out.println(" EXCEPTION IN "+this.getClass().getName()+" in method fetchStudentListOrderByStudentId "+e); 
     } 
     return studentVOList; 
    } 

,這是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
       version="2.0"> 
    <persistence-unit name="forPractise" transaction-type="JTA"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <jta-data-source>jdbc/app</jta-data-source> 
     <class>com.entity.StudentEntity</class> 
     <properties> 
       <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /> 
       <property name="hibernate.show_sql" value="true" /> 
       <property name="hibernate.format_sql" value="true" />    
       <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup" />    
     </properties> 
    </persistence-unit> 
</persistence> 

會發生什麼事是在JSP頁面的加載,StudentList的getter方法是所謂的 - 裏面的getter方法我寫了邏輯,如果studentList爲空,則調用studentService.fetchStudentListOrderByStudentId(true);

但我做到這一點時,我得到一個異常:

例外com.bb.StudentServiceBean的方法 fetchStudentListOrderByStudentId org.hibernate.service.UnknownServiceException:未知服務 要求 [org.hibernate作爲。 service.jdbc.connections.spi.ConnectionProvider]

您能告訴我我錯過了什麼,或者我錯了哪裏?

謝謝。

+0

你鏈接到的休眠4.x的庫? – Perception 2012-03-09 11:52:54

+0

是有[R冬眠4個罐子在classpath中,除一人外,我從classpath中刪除的jboss-交易罐子,因爲我使用的是GlassFish, – 2012-03-09 11:57:26

回答

4

表明您正在使用Hibernate的4.x的,但據我所知,你提到的類是僅適用於JPA 1.0和Hibernate 3.x的嘗試從配置中刪除以下行:

<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup" /> 
+0

非常感謝你,它的工作。 還有一件事,你有沒有讀過Hibernate 4的整個文檔? 我的意思是你怎麼知道甚至可能的財產價值,如哪些財產包括在哪些不應該是? – 2012-03-09 14:34:28

+0

很高興工作。我在我現在正在開發的一個項目中使用Hibernate 4,所以我對它很熟悉。 – Perception 2012-03-09 15:47:07

2

以防萬一,這是任何利益,我在碼頭得到這個錯誤試圖啓動一個Hibernate會話時:

未知服務請求[org.hibernate作爲。 service.jdbc.connections.spi.ConnectionProvider]

根本原因是之前的異常(在日誌前幾秒)導致Spring上下文(WebAppContext)無法初始化。

一旦我固定在Spring上下文,「請求了未知服務」固定本身。所以,如果你看到這個錯誤很值得研究太多前檢查先前的錯誤..

+0

它似乎發生了幾個有關獲取工作的EntityManagerFactory實例的問題。例如,我剛剛通過多個JUnit方法調用的命令行程序調試了EMF關閉的情況(即EntityManagerFactory.close()),第二次調用該消息導致EMF關門了。 – zakmck 2013-09-10 14:40:01

相關問題