在尝试通过WebSphere中的Hibernate访问H2嵌入式内存数据库时创建EntityManagerFactory时出错

时间:2014-09-02 20:46:45

标签: hibernate jpa java-ee-6 h2 websphere-8

当尝试在WebSphere中使用H2嵌入式内存数据库时,我收到以下错误:

EJBException: Injection failure; nested exception is: 
java.lang.IllegalStateException:
EntityManagerFactory has not been created for PU : 
PuId=javaee6-0_0_1-SNAPSHOT_war#javaee6-0.0.1-SNAPSHOT.war#javaee6

javaee6是我的应用程序的名称,我使用Maven构建到javaee6-0.0.1-SNAPSHOT.war

我在WebSphere上部署H2的尝试是将h2-1.3.173.jar复制到WebSphere\AppServer8\lib并重新启动服务器。

我的WebSphere版本是8.0.0.5。

的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="javaee6" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>com.learning.business.car.Car</class>

        <properties>
            <property name="connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true" />

        </properties>
    </persistence-unit>
</persistence>

管理bean:

package com.learning.app.car;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import com.learning.business.car.CarRepo;

@ManagedBean
public class CarBean {

    @EJB
    public CarRepo carRepo;

    public Integer getCarsCount() {
        return carRepo.getAll().size();
    }
}

EJB模块:

package com.learning.business.car;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

@Stateless
public class CarRepo {

    @PersistenceContext
    protected EntityManager em;

    public List<Car> getAll() {

        TypedQuery<Car> query = em.createQuery("SELECT c FROM Car c", Car.class);
        return query.getResultList();
    }
}

实体:

package com.learning.business.car;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Car {

    @Id
    @GeneratedValue
    private long id;

    public long getId() {
        return id;
    }
}

cars.xhtml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html">
<head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
    <body>
        <h:outputText value="I have #{carBean.carsCount}" />
    </body>
</html>

访问页面时显示完整的堆栈跟踪错误:

javax.faces.FacesException: javax.ejb.EJBException: Injection failure; nested exception is: java.lang.IllegalStateException: EntityManagerFactory has not been created for PU : PuId=javaee6-0_0_1-SNAPSHOT_war#javaee6-0.0.1-SNAPSHOT.war#javaee6
    at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.wrap(ExceptionHandlerImpl.java:241)
    at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:156)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:258)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1224)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1032)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3751)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:962)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1690)
Caused by: javax.ejb.EJBException: Injection failure; nested exception is: java.lang.IllegalStateException: EntityManagerFactory has not been created for PU : PuId=javaee6-0_0_1-SNAPSHOT_war#javaee6-0.0.1-SNAPSHOT.war#javaee6
Caused by: java.lang.IllegalStateException: EntityManagerFactory has not been created for PU : PuId=javaee6-0_0_1-SNAPSHOT_war#javaee6-0.0.1-SNAPSHOT.war#javaee6
    at com.ibm.ws.jpa.management.JPAPUnitInfo.getEntityManagerFactory(JPAPUnitInfo.java:1425)
    at com.ibm.ws.jpa.management.JPAPUnitInfo.getEntityManagerPool(JPAPUnitInfo.java:1652)
    at com.ibm.ws.jpa.management.JPATxEntityManager.<init>(JPATxEntityManager.java:161)
    at com.ibm.ws.jpa.management.AbstractJPAComponent.getEntityManager(AbstractJPAComponent.java:509)
    at com.ibm.ws.jpa.management.JPAComponentImpl.getEntityManager(JPAComponentImpl.java:128)
    at com.ibm.ws.util.JPAJndiLookupObjectFactory.getObjectInstance(JPAJndiLookupObjectFactory.java:176)
    at com.ibm.wsspi.injectionengine.InjectionBinding.getInjectionObject(InjectionBinding.java:1038)
    at com.ibm.wsspi.injectionengine.InjectionBinding.getInjectableObject(InjectionBinding.java:1003)
    at com.ibm.wsspi.injectionengine.InjectionTarget.inject(InjectionTarget.java:198)
    at com.ibm.ws.injectionengine.AbstractInjectionEngine.inject(AbstractInjectionEngine.java:924)
    at com.ibm.ejs.container.StatelessBeanO.initialize(StatelessBeanO.java:300)
    at com.ibm.ejs.container.BeanOFactory.create(BeanOFactory.java:147)
    at com.ibm.ejs.container.EJSHome.createBeanO(EJSHome.java:1240)
    at com.ibm.ejs.container.EJSHome.createBeanO(EJSHome.java:1358)
    at com.ibm.ejs.container.activator.UncachedActivationStrategy.atActivate(UncachedActivationStrategy.java:88)
    at com.ibm.ejs.container.activator.Activator.preInvokeActivateBean(Activator.java:614)
    at com.ibm.ejs.container.EJSContainer.preInvokeActivate(EJSContainer.java:4110)
    at com.ibm.ejs.container.EJSContainer.EjbPreInvoke(EJSContainer.java:3476)
    at com.learning.business.car.EJSLocalNSLCarRepo_a1ab5865.getAll(EJSLocalNSLCarRepo_a1ab5865.java)
    at com.learning.business.car.CarBean.getCarsCount(CarBean.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:91)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:55)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:173)
    at org.apache.el.parser.AstDeferredExpression.getValue(AstDeferredExpression.java:44)
    at org.apache.el.parser.AstCompositeExpression.getValue(AstCompositeExpression.java:50)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:283)
    at org.apache.myfaces.view.facelets.el.TagValueExpression.getValue(TagValueExpression.java:85)
    at javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:243)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:71)
    at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getValue(RendererUtils.java:343)
    at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getStringValue(RendererUtils.java:295)
    at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.renderOutput(HtmlTextRendererBase.java:92)
    at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.encodeEnd(HtmlTextRendererBase.java:79)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:535)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:626)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:622)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1320)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:263)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1224)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1032)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3751)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:962)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1690)

1 个答案:

答案 0 :(得分:2)

WebSphere包含OpenJPA实现并默认使用它。如果要使用第三方,请确保在应用程序或共享库中正确配置它。检查这些链接如何配置第三方JPA和Hibernate:

并且避免将jar文件放到WebSphere\AppServer8\lib。如果您正在使用您的应用程序打包JPA提供程序,请尝试将h2-1.3.173.jar放在那里(WEB-INF / lib)。或者创建JDBC Provider和DataSource,并在persistence.xml中使用Datasource。

如果您想使用默认提供商:

  • 删除<provider>元素以使用默认值,并从应用程序中删除Hibernate JPA jar:
  

<provider>org.hibernate.ejb.HibernatePersistence</provider>

  • 将hibernate相关属性替换为默认JPA
    要么使用DataSource:
  

<jta-data-source>jdbc/DataSourceJNDI</jta-data-source>

或使用连接属性:

<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>

有关如何配置默认提供程序的详细信息,请参阅此页面:Associating persistence providers and data sources