Question

I learning JPA. Trying to build the project, but catch the error:

Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/HomeProject-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory

Who have experience in this, please help my. My context:

<context:property-placeholder location="file:${hp.ini}"/>
<!-- Включаем опцию использования конфигурационных аннотаций (@Annotation-based configuration)-->
<context:annotation-config/>
<tx:annotation-driven/>

<jpa:repositories base-package="com.kuzmenko.homeproject.repo"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClass}"
      p:url="${jdbc.url}"
      p:username="${jdbc.user}"
      p:password="${jdbc.password}"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.kuzmenko.homeproject.domain</value>
        </list>
    </property>
</bean>

I think that I should to create persistence.xml. How to generate it? What dependencies I should to add?

Was it helpful?

Solution 2

The problem is solved! :) The problem was in pom.xml. PersistenceUnit is not required. Enough following configuration entityManagerFactory:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.kuzmenko.homeproject.domain</value>
        </list>
    </property>
</bean>

End dependencies, that need:

<!-- Spring dependencies for Hibernate -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.4.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- ######### End ######## -->
    <!-- Hibernate dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.5.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.5.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <!-- ######### End ######## -->

OTHER TIPS

You are missing the persistenceUnitName as clearely reported in the error log. So it should be declared as a property inside your entityMangerFactory bean declaration as below:

<property name="persistenceUnitName" value="yourpersistenceunit" />

Then you should add the persistence.xml file under classpath:META-INF/ with below content:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

 <persistence-unit name="yourpersistenceunit" transaction-type="RESOURCE_LOCAL">
   <class>yourpackage.yourclass</class>
   ...
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <properties>
     <property name="hibernate.dialect" value=${dialect.for.your.rdbms} />
   </properties>
  </persistence-unit>
</persistence>

You should note that this declaration may need some adjustements to fit in with your exact configuration.
BR.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top