【问题标题】:Wicket+Spring+JPA+Hibernate: No Persistence Unit FoundWicket+Spring+JPA+Hibernate:找不到持久性单元
【发布时间】:2011-04-21 20:58:07
【问题描述】:

我正在使用 Wicket+Spring+JPA+Hibernate 开发一个 Web 应用程序。这是我使用此设置的第一个项目,我想我可能犯了一些错误。我收到以下错误:找不到名为“ApplicationEntityManager”的持久性单元。

我的 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="ApplicationEntityManager" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
 </persistence-unit>
</persistence>

我的 applicationContext.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"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- Enable @Transactional support -->
    <tx:annotation-driven/>

    <!-- Enable @AspectJ support -->
    <aop:aspectj-autoproxy/>

    <!-- Scans for @Repository, @Service and @Component -->
    <context:component-scan base-package="org.appfuse"/>
    <context:annotation-config />


 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="maxActive" value="30"/>
    <property name="maxIdle" value="10"/>
    <property name="maxWait" value="1000"/>
    <property name="defaultAutoCommit" value="true"/>
</bean>    


 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="ApplicationEntityManager" />
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="databasePlatform" value="${jpa.databasePlatform}" />
    <property name="showSql" value="${jpa.showSql}" />
    <property name="generateDdl" value="${jpa.generateDdl}" />
   </bean>
  </property>
 </bean>


 <bean id="transactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
  <property name="dataSource" ref="dataSource" />
 </bean>

 <aop:config>
        <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* *..service.*Manager.*(..))"/>
    </aop:config>

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>     
</beans>

我的 web.xml:

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

    <display-name>AppFuse Light</display-name>

    <!-- [INSERT FRAGMENT HERE] -->
    <!-- Define the basename for a resource bundle for I18N -->
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>messages</param-value>
    </context-param>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter>
        <filter-name>messageFilter</filter-name>
        <filter-class>org.appfuse.web.MessageFilter</filter-class>
    </filter>

   <filter>
   <filter-name>open.entitymanager.in.view</filter-name>
   <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
 </filter>

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
    <filter>
        <filter-name>wicket</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>org.appfuse.web.Application</param-value>
        </init-param>
        <init-param>
            <param-name>configuration</param-name>
            <param-value>development</param-value>
        </init-param>
    </filter>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>messageFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>  
 <filter-name>open.entitymanager.in.view</filter-name>  
 <url-pattern>/*</url-pattern>  
 </filter-mapping> 

    <filter-mapping>
        <filter-name>wicket</filter-name>
        <url-pattern>/app/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>

    <error-page>
        <error-code>500</error-code>
        <location>/error.jsp</location>
    </error-page>

</web-app>

你能看出任何明显的错误吗?这是标准配置吗?谢谢!

【问题讨论】:

  • 似乎无法真正发现任何问题。您是否可以将项目以 zip 文件的形式发布到某个地方?
  • 错误的完整堆栈跟踪也会有所帮助。

标签: java hibernate spring jpa wicket


【解决方案1】:

确保persistence.xml 位于正确的位置。在你的构建路径中它应该是/META-INF/persistence.xml,所以在编译后的war文件中它应该以/WEB-INF/classes/META-INF/persistence.xml结尾。

【讨论】:

  • 我已经检查过了,它就在那里。
【解决方案2】:

您是否检查过您的 persistence.xml 是否在 META-INF 目录中,即 persistence.xml 应始终位于 {persistence-unit-root}/META-INF/ 目录中??

【讨论】:

    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    相关资源
    最近更新 更多