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

时间:2012-10-18 00:08:11

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

我正在尝试创建一个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属性“。

中指定DataSource类名

我的新手眼睛似乎可能没有处理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>

根context.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>

3 个答案:

答案 0 :(得分:2)

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一起使用,但仅在应用补丁后使用。只需按照说明进行操作(修补程序似乎非常简单,但我没有对其进行测试)。

正如user1654209在第一个回答中写道的那样,你可以使用自己的JPA 2.0提供程序而无需修补。但是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>
    

答案 1 :(得分:1)

您需要在context.xml中配置entityManagerFactory bean。 Heres是使用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>

答案 2 :(得分:0)

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

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

查看关于如何在persistence.xml中设置JNDI名称的hibernate文档