Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用EJB3.0和Hibernate持久性提供程序的应用程序会出现未知服务异常_Java_Hibernate_Jpa_Ejb 3.0_Jpa 2.0 - Fatal编程技术网

Java 使用EJB3.0和Hibernate持久性提供程序的应用程序会出现未知服务异常

Java 使用EJB3.0和Hibernate持久性提供程序的应用程序会出现未知服务异常,java,hibernate,jpa,ejb-3.0,jpa-2.0,Java,Hibernate,Jpa,Ejb 3.0,Jpa 2.0,我正在尝试使用JSF和EJB3.0创建一个web应用程序 我使用普通JSF、Glassfish服务器和Hibernate作为持久性提供者。我的数据库是ApacheDerby 下面是我的无状态会话bean,如下所示: @Stateless @TransactionManagement(TransactionManagementType.CONTAINER) public class StudentServiceBean implements StudentService{

我正在尝试使用JSF和EJB3.0创建一个web应用程序

我使用普通JSF、Glassfish服务器和Hibernate作为持久性提供者。我的数据库是ApacheDerby

下面是我的无状态会话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;
    }
@无状态
@TransactionManagement(TransactionManagementType.CONTAINER)
公共类StudentServiceBean实现StudentService{
@PersistenceContext(unitName=“forpractice”)
私人实体管理者实体GR;
@资源
非公开会话上下文会话上下文;
@凌驾
公共列表fetchStudentListOrderByStudentId(布尔标志){
List studentList=null;
TypedQuery studentQuery=null;
List studentVOList=null;
String queryDisc=“按s.studentId desc从学生实体的顺序中选择s”;
String query=“按s.studentId从学生实体的顺序中选择s”;
试一试{
如果(!标志){
studentQuery=entityMgr.createQuery(查询,StudentEntity.class);
}否则{
studentQuery=entityMgr.createQuery(queryDesc,StudentEntity.class);
}           
studentList=studentQuery.getResultList();
studentVOList=newarraylist();
(学生实体:学生名单){
添加(新的StudentVO(String.valueOf(studentE.getStudentId()),studentE.getStudentName(),studentE.getContactNumber());
}
}捕获(例外e){
System.out.println(“fetchStudentListOrderByStudentId方法中的“+this.getClass().getName()+”中的异常”+e);
}
返回学生志愿者;
}
这是我的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>

org.hibernate.ejb.HibernatePersistence
jdbc/app
com.entity.StudentEntity
在加载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,但您提到的类afaik仅对JPA 1.0和Hibernate 3.x有效。请尝试从配置中删除以下行:

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

为了以防万一,我在Jetty上尝试启动Hibernate会话时遇到以下错误:

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

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


一旦我修复了spring上下文,“请求的未知服务”修复了它本身。因此,如果您看到此错误,在进行过多调查之前,有必要检查早期错误。

您是否链接到Hibernate 4.x库?是的,类路径中有r个Hibernate 4 jar,除了一个,我已经从类路径中删除了jboss事务jar,因为我正在使用Glassfish,非常感谢您的帮助。还有一个事情,你读了Hibernate 4的全部文档了吗?我的意思是,你怎么知道可能的属性值,比如哪些属性应该包含,哪些不应该包含?很高兴它成功了。我在一个项目中使用了Hibernate 4,所以我对它非常熟悉。它似乎在获得工作时遇到了一些问题正在初始化EntityManagerFactory实例。例如,我刚刚调试了一个案例,在该案例中,多个JUnit方法调用的命令行程序关闭了EMF(即EntityManagerFactory.close()),对它的第二次调用因此消息而崩溃,因为EMF已关闭。