I am trying to get JPA EntityManagerFactory while working with Hibernate. Additionally, I am using Dynamic Models as they're described here
So I don't have Persistence Unit, persistence.xml, orm.xml etc. I don't even know / don't control which package my classes are created in...
SessionFactory worked like a charm having just hibernate mapping file in WEB-INF directory. But another lib needs EntityManagerFactory, so I am trying to create it instead of SessionFactory.
I am constantly get the following error, and I don't understand why, please help! Google showed me lot's of info including the factory builder and related sources which show that classloader is used to get this resource, and a javadoc explaining that META-INF location should work, but it doesn't...
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 file locations (it's everywhere):
src\main\java\com\ttt\app\
src\main\webapp\META-INF\
src\main\webapp\WEB-INF\
Unfortunately Netbeans doesn't copy "src\main\java\com\ttt\app\" into the distribution, but anyway I'd like to keep the file somewhere closer to root, WEB-INF would be ideal, as it worked with SessionFactory
In Netbeans, I had to put the "hibernate.hbm.xml" file in "src\main\resources" (under "Other Sources" node in project view). Then it became available with
<property name="mappingResources" value="hibernate.hbm.xml"/>