[英]Hibernate-JPA. javax.persistence.PersistenceException
I learning JPA. 我在学习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. 我认为我应该创建persistence.xml。 How to generate it?
如何产生呢? What dependencies I should to add?
我应该添加哪些依赖项?
You are missing the persistenceUnitName
as clearely reported in the error log. 您缺少错误日志中明确报告的
persistenceUnitName
。 So it should be declared as a property inside your entityMangerFactory
bean declaration as below: 因此,应将其声明为您的
entityMangerFactory
bean声明内的属性,如下所示:
<property name="persistenceUnitName" value="yourpersistenceunit" />
Then you should add the persistence.xml file under classpath:META-INF/ with below content: 然后,您应该将persistence.xml文件添加到classpath:META-INF /下,其内容如下:
<?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. BR。
The problem is solved! 问题已经解决了! :) The problem was in pom.xml.
:)问题出在pom.xml中。 PersistenceUnit is not required.
不需要PersistenceUnit。 Enough following configuration entityManagerFactory:
以下足够的配置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 ######## -->
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.