为什么在 WebLogic 中使用 JPA 时会出现意外的 EntityManagerFactory?



我正在尝试创建一个Spring MVC Web应用程序(Spring Framework 3.0.5)。 我正在使用 IntelliJ IDEA 11.1.3 在 WebLogic Server (10.3.4) 上部署我的应用程序。 我的一个网页尝试使用 JPA 将一些数据存储在数据库中。 我的坚持.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,指出"必须在 ConnectionDriverName 属性中指定 JDBC 驱动程序或数据源类名"。

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

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

我的网站.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>

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 文件中打包的类。要防止此类行为,您有两种选择:

  • 将应用程序的 WAR 打包在 EAR 存档中,其中包含META-INF/weblogic-application.xml包含以下行的文件(还必须包含标准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>
    

您需要在上下文中配置实体管理器工厂 bean.xml。这是一个使用 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>

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

如果您在容器上运行它并希望在 weblogic 中配置数据源,则只需从休眠 cfg 引用 jndi 数据源,但您需要在 weblogic 控制台中输入并在那里创建数据源和连接池。

查看休眠文档,了解如何在持久性中设置 JNDI 名称.xml

最新更新