Hibernate的-JPA。 javax.persistence.PersistenceException

时间:2014-03-03 15:28:10

标签: java hibernate jpa

我学习JPA。尝试构建项目,但抓住错误:

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。如何生成它?我应该添加哪些依赖项?

2 个答案:

答案 0 :(得分:0)

您错过了错误日志中明确报告的persistenceUnitName。 因此,它应该在entityMangerFactory bean声明中声明为属性,如下所示:

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

然后你应该在 classpath:META-INF / 下添加 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。

答案 1 :(得分: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 ######## -->