【问题标题】:Why do I get an unexpected EntityManagerFactory when using JPA within WebLogic?为什么在 WebLogic 中使用 JPA 时会出现意外的 EntityManagerFactory?
【发布时间】:2012-10-18 00:08:11
【问题描述】:

我正在尝试创建一个 Spring MVC Web 应用程序(Spring Framework 3.0.5)。我正在使用 IntelliJ IDEA 11.1.3 在 WebLogic Server (10.3.4) 上部署我的应用程序。我的一个网页尝试使用 JPA 将一些数据存储在数据库中。我的 persistence.xml 指定:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
    <persistence-unit name="LeaveSchedulerJPA" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.engilitycorp.leavetracker.jpa.UserRole</class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xe "/>
        <property name="javax.persistence.jdbc.user" value="leavescheduler"/>
        <property name="javax.persistence.jdbc.password" value="xxx"/>
        <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
    </properties>
    </persistence-unit>
</persistence>

但是,当我查看调试器时,我的 EntityManagerFactory 显示为 org.apache.openjpa.persistence.EntityManagerFactoryImpl,当我调用 createEntityManager 时,我得到一个 org.apache.openjpa.persistenceArgumentException,指出“必须指定 JDBC 驱动程序或数据源类名在 ConnectionDriverName 属性中”。

在我的新手看来,persistence.xml 可能没有得到处理。我试过把它放在(project)/src/main/resources/META-INF(project)/src/main/resources/META-INF/spring 中,同样不幸的结果。

我不致力于使用 Hibernate 持久性;但是,我确实想使用实现 JPA 2 的东西,而且我很难配置我的环境。例如,我几乎不知道 openjpa 是如何参与我的应用程序的。我想它可能是某些东西的默认 JPA 提供程序(WebLogic?,IntelliJ IDEA?)。任何帮助/建议将不胜感激。

我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

根上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>

servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.engilitycorp.leavetracker" />

</beans:beans>

【问题讨论】:

  • 你能发布你的配置文件吗?
  • 我刚做了。你还有其他想看的吗?

标签: spring spring-mvc intellij-idea jpa-2.0 weblogic-10.x


【解决方案1】:

WebLogic 10.3.4 与 Java EE 5 兼容,并附带 JPA 1.0 实现:OpenJPA 和 TopLink。

根据 WebLogic 文档 (http://docs.oracle.com/cd/E17904_01/web.1111/e13720/using_toplink.htm#CIHDJHHI),它也可以与 JPA 2.0 一起使用,但只能在应用补丁后使用。只需按照说明进行即可(打补丁似乎很简单,但我没有测试过)。

也许您也可以使用自己的 JPA 2.0 提供程序而无需打补丁,正如 user1654209 在第一个答案中所写的那样。但是 WebLogic 提供的 JPA 1.0 类可能会造成阻碍,因为它们是由更高级别的类加载器加载的,并且优先于打包在 WAR 文件中的类。为防止此类行为,您有两种选择:

  • 使用包含以下行的META-INF/weblogic-application.xml 文件将应用程序的WAR 打包到EAR 存档中(您还必须包含标准META-INF/application.xml 文件):

    <?xml version="1.0" encoding="UTF-8"?>
    <weblogic-application xmlns="http://xmlns.oracle.com/weblogic/weblogic-application/1.0/weblogic-application.xsd">
      <prefer-application-packages>
        <package-name>javax.persistence.*</package-name>
      </prefer-application-packages>
    </weblogic-application>
    
  • WEB-INF/weblogic.xml 文件添加到您的 WAR 存档中,其中包含以下几行:

    <?xml version='1.0' encoding='UTF-8'?>
    <weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
    
      <container-descriptor>
        <prefer-web-inf-classes>true</prefer-web-inf-classes>
      </container-descriptor>
    </weblogic-web-app>
    

【讨论】:

    【解决方案2】:

    您需要在您的 context.xml 中配置一个 entityManagerFactory bean。下面是一个使用 eclipselink 作为 JPA 提供者的例子

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            id="dataSource">
            <property name="driverClassName" value="${database.driverClassName}" />
            <property name="url" value="${database.url}" />
            <property name="username" value="${database.username}" />
            <property name="password" value="${database.password}" />
        </bean>
    
        <bean
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
            id="entityManagerFactory">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean id="jpaAdapter"
                    class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                </bean>
            </property>
        </bean>
    

    【讨论】:

      【解决方案3】:

      仅设置提供程序是不够的。您需要设置数据库连接数据,如 jdbc url、用户名和密码。你设置了吗?如前所述,您还需要设置 jdbc 类名,并且 jdbc 驱动程序需要在您的类路径中。

      如果你在容器上运行它并且想在 weblogic 中配置数据源,你可以从你的 hibernate cfg 中引用一个 jndi 数据源,但是你需要进入 weblogic 控制台并创建一个数据源和一个连接那里的游泳池。

      查看 hibernate 文档,了解如何在 persistence.xml 中设置 JNDI 名称

      【讨论】:

      • 我添加了更完整的配置文件描述。现在可以看到,URL等设置好了,当我从IntelliJ IDEA测试连接时,连接正常,可以查看数据库。
      猜你喜欢
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 2010-10-19
      相关资源
      最近更新 更多