我正在尝试使用Hibernate时获取JPA EntityManagerFactory。另外,我正在使用动态模型,因为它们在这里被描述
所以我没有Persistence Unit,persistence.xml,orm.xml等。我甚至不知道/不控制我的类在哪个包中创建...
SessionFactory就像一个在WEB-INF目录中只有hibernate映射文件的魅力。但另一个lib需要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
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 app context:
<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是理想的,因为它适用于SessionFactory
答案 0 :(得分:0)
在Netbeans中,我不得不放置&#34; hibernate.hbm.xml&#34;文件在&#34; src \ main \ resources&#34; (在&#34;其他来源&#34;项目视图中的节点)。
随后可用<property name="mappingResources" value="hibernate.hbm.xml"/>