2014-03-03 69 views
0

我在學習JPA。試圖建立該項目,但趕上錯誤:Hibernate-JPA。 javax.persistence.PersistenceException

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 

誰有這方面的經驗,請幫助我。 我的背景:

<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> 

我認爲我應該創建的persistence.xml。如何生成它?我應該添加哪些依賴關係?

回答

0

問題解決了! :)問題出現在pom.xml中。 PersistenceUnit不是必需的。夠了以下配置的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> 

末依賴性,需要:

<!-- 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 ######## --> 
0

您錯過了錯誤日誌中清晰報告的persistenceUnitName。 所以應該聲明爲entityMangerFactory bean聲明如下里面的屬性:下面的內容META-INF/:

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

那麼你應該添加的persistence.xml文件下類路徑

<?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> 

您應該注意,此聲明可能需要一些調整以適應您的確切配置。
BR。

相關問題