【问题标题】:An application using EJB 3.0 and Hibernate persistence provider gives an UnknownServiceException使用 EJB 3.0 和 Hibernate 持久性提供程序的应用程序给出了 UnknownServiceException
【发布时间】:2012-03-26 20:36:27
【问题描述】:

我正在尝试使用 JSF 和 EJB 3.0 创建一个 Web 应用程序。

我使用普通的 JSF、Glassfish 服务器、Hibernate 作为我的持久性提供程序。我的数据库是 apache derby。

这里我的无状态会话 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]

你能告诉我我错过了什么,或者我哪里出错了吗?

谢谢。

【问题讨论】:

  • 您是否链接到 Hibernate 4.x 库?
  • 是的,在类路径中有 r hibernate 4 个 jar,除了一个,我已经从类路径中删除了 jboss-transaction jar,因为我正在使用 Glassfish,

标签: java hibernate jpa ejb-3.0 jpa-2.0


【解决方案1】:

您表示您使用的是 Hibernate 4.x,但您提到的 afaik 类仅对 JPA 1.0 和 Hibernate 3.x 有效。尝试从您的配置中删除以下行:

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

【讨论】:

  • 非常感谢它成功了。还有一件事,你读过 Hibernate 4 的全部文档吗?我的意思是你怎么知道可能的属性值,比如包含哪些属性,哪些不应该包含?
  • 很高兴它成功了。我在我现在正在做的一个项目中使用 Hibernate 4,所以我对它非常熟悉。
【解决方案2】:

以防万一,我在尝试启动 Hibernate 会话时在 Jetty 上收到此错误:

请求未知服务 [org.hibernate.service.jdbc.connections.spi.ConnectionProvider]

根本原因是先前的异常(日志中早几秒)导致 spring 上下文 (WebAppContext) 无法初始化。

一旦我修复了 spring 上下文,“请求的未知服务”就会自行修复。因此,如果您看到此错误,则值得在进行过多调查之前检查早期的错误。

【讨论】:

  • 似乎它发生在有关获取工作 EntityManagerFactory 实例的几个问题上。例如,我刚刚调试了一个 EMF 被多个 JUnit 方法调用的命令行程序关闭(即 EntityManagerFactory.close())的情况,对它的第二次调用因此消息而崩溃,导致 EMF已关闭。
猜你喜欢
  • 2012-07-09
  • 1970-01-01
  • 2016-11-04
  • 2012-06-21
  • 1970-01-01
  • 2017-01-17
  • 2014-12-15
  • 1970-01-01
  • 2010-10-10
相关资源
最近更新 更多