正在使用Spring中的Hibernate动态模型获取EntityManagerFactory:无法解析命名映射文件



在使用Hibernate时,我正在尝试获取JPA EntityManagerFactory。此外,我正在使用动态模型,正如这里所描述的

https://docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch04.html#persistent-类动态模型

所以我没有PersistenceUnit、Persistence.xml、orm.xml等。我甚至不知道/不控制我的类是在哪个包中创建的…

SessionFactory的工作就像一个魅力,在WEB-INF目录中只有hibernate映射文件。但是另一个lib需要EntityManagerFactory,所以我尝试创建它而不是SessionFactory。

我不断地得到以下错误,我不明白为什么,请帮助!谷歌向我展示了很多信息,包括工厂构建器和相关来源,这些来源表明classloader是用来获取这个资源的,还有一个javadoc解释说META-INF位置应该可以工作,但它不。。。

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to resolve named mapping-file [com/ttt/app/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.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336)
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)
... 55 more

pom:

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate-version}</version>
    </dependency>
    <!--<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate-version}</version>
    </dependency>-->

hbm:

<class entity-name="codeTranslation">
    <composite-id name="codeTranslationId">
        <key-property name="codeNm" type="string" length="255"/>
        <key-property name="codeCd" type="string" length="32"/>
        <key-property name="langCd" type="string" length="32"/>
    </composite-id>
    <property name="description" type="string" length="256"/>
    <property name="longDescription" type="string" length="256"/>
</class>
<class entity-name="Entity">
    <comment>
        Individual or corporate person, or contract - identifiable entity of work
    </comment>
    <id name="id" type="string" length="32">
        <generator class="uuid"/>
    </id>       
    <discriminator column="entityType" type="string" length="1"/>
... etc etc

spring应用程序上下文:

<context:property-placeholder local-override="true" ignore-resource-not-found="true"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource" >
    <property name="driverClass" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost/hib5" />
    <property name="username" value="postgres" />
    <property name="password" value="******" />
    <!--<property name="defaultAutoCommit" value="false" />-->
</bean>      
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--<property name="packagesToScan" value="have nothing to put here" />-->
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <!-- these are guessing variants. tried formatting that as <list> as well -->
    <property name="mappingResources" value="com/ttt/app/hibernate.hbm.xml"/>
    <property name="mappingResources" value="/WEB-INF/hibernate.hbm.xml"/>
    <property name="mappingResources" value="/META-INF/hibernate.hbm.xml"/>
    <property name="mappingResources" value="WEB-INF/hibernate.hbm.xml"/>
    <property name="mappingResources" value="META-INF/hibernate.hbm.xml"/>
    <property name="mappingResources" value="classpath:WEB-INF/hibernate.hbm.xml"/>
    <property name="mappingResources" value="classpath:META-INF/hibernate.hbm.xml"/>
    <property name="mappingResources" value="hibernate.hbm.xml"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop> 
        </props>
    </property>
</bean>
<!-- old, worked that way -->
<!--<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingLocations">
        <list>
            <value>/WEB-INF/hibernate.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop> 
        </props>           
    </property>
</bean>-->
<bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <!--<property name="sessionFactory" ref="sessionFactory"/>-->
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

hbm文件位置(无处不在):

srcmainjavacomtttapp
srcmainwebappMETA-INF
srcmainwebappWEB-INF

不幸的是,Netbeans没有将"src\main\java\com\ttt\app\"复制到发行版中,但无论如何,我想将文件保存在更接近根目录的位置,WEB-INF将是理想的,因为它与SessionFactory 一起工作

在Netbeans中,我不得不将"hibernate.hbm.xml"文件放在"src\main\resources"(项目视图中的"Other Sources"节点下)中。然后它就可以使用了

<property name="mappingResources" value="hibernate.hbm.xml"/>

相关内容

  • 没有找到相关文章

最新更新