我正在尝试在使用 Hibernate 时获取 JPA EntityManagerFactory。此外,我正在使用动态模型,因为它们在此处描述
所以我没有 Persistence Unit、persistence.xml、orm.xml 等。我什至不知道/不控制我的类是在哪个包中创建的......
SessionFactory 就像在 WEB-INF 目录中具有休眠映射文件一样的魅力。但是另一个库需要 EntityManagerFactory,所以我试图创建它而不是 SessionFactory。
我经常收到以下错误,我不明白为什么,请帮助!谷歌向我展示了很多信息,包括工厂构建器和相关资源,这些信息表明类加载器用于获取此资源,以及解释 META-INF 位置应该工作的 javadoc,但它没有......
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
起居室:
<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
春季应用程序上下文:
<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 文件位置(无处不在):
src\main\java\com\ttt\app\
src\main\webapp\META-INF\
src\main\webapp\WEB-INF\
不幸的是,Netbeans 没有将“src\main\java\com\ttt\app\”复制到发行版中,但无论如何我想将文件保存在更接近根目录的地方,WEB-INF 是理想的,因为它与会话工厂