javax.persistence.PersistenceException:[PersistenceUnit:default]无法解析命名映射文件[hibernate.hbm.xml]

时间:2015-05-29 14:18:45

标签: java spring hibernate jpa

我需要在Spring上下文中添加一个hibernate映射资源文件。我不知道怎么能做这些事情?

<context:annotation-config />
    <bean id="myEmf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="ProjectManagerDataSource" />
        <property name="packagesToScan" value="com.ads" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>

            </props>
        </property>
    </bean>
    <bean id="ProjectManagerDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://localhost:3306/project-management" />
        <property name="username" value="usera" />
        <property name="password" value="userausera" />
    </bean>

    <!-- <oxm:jaxb2-marshaller id="jaxbMarshallerBean"> -->
    <!-- <oxm:class-to-be-bound name="com.ads.configs.AppConfig" /> -->
    <!-- </oxm:jaxb2-marshaller> -->


    <jpa:repositories base-package="com.ads.dao"
        entity-manager-factory-ref="myEmf" />
    <tx:annotation-driven />
    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="com.ads.*" />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
    </bean>

我已经使用Session工厂创建了一个解决方案,但由于我使用的是实体管理器工厂,因此我无法使用我的项目。

更新:我的新问题是发生了javax.persistence.PersistenceException  这是我的堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEmf' defined in ServletContext resource [/WEB-INF/rest-servlet.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to resolve named mapping-file [hibernate.hbm.xml]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to resolve named mapping-file [hibernate.hbm.xml]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1225)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1221)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.buildMappingFileDescriptor(EntityManagerFactoryBuilderImpl.java:348)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.buildDeploymentResources(EntityManagerFactoryBuilderImpl.java:264)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:220)
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:51)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:182)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:177)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:67)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:318)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)

3 个答案:

答案 0 :(得分:0)

向你的org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean bean添加以下属性

<property name="mappingResources">
            <value>com/your/package/resources/*.hbm.xml</value>
        </property>

答案 1 :(得分:0)

我刚刚将文件的位置更改为src / main / resources,然后问题就解决了。

答案 2 :(得分:0)

这意味着你的映射文件orm.xml(如果使用JPA或hbm.xml,如果使用hibernate)不在部署档案中,或者它们位于错误的位置或错误地命名为persistence.xml文件或文件名本身。

由于这些可能的原因,您的容器或运行时无法找到在persistence.xml文件中声明的所需文件。

根据部署档案类型,映射文件应位于META-INF或WEB-INF下。

在persistence.xml中为JPA声明这样:

/META-INF/jpa/Entity-orm.xml

将解决可能与目录相关的问题。

假设Entity-orm.xml文件位于/ META-INF / jpa /目录下。