我有 Spring MVC 项目。我正在使用 HSQL 数据库和 Hibernate。我在与 Spring MVC(默认为 JPA)和 Hibernate 建立连接时卡住了。我persistance.xml
在 Spring MVC 目录中创建了文件:src/main/resources/META-INF
. 我也尝试将它放在 Spring MVC 目录中:Deployed Resources/webapp/WEB-INF/classes/META-INF
. 我不知道哪一个是正确的。我仍然有同样的问题。
这是我的persistance.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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" version="2.0">
<persistence-unit name="entityManager">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>package.pl.models.Users</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:/home/user/hsqldb/lib/my.spring.mvc/mybase" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
这是我的 Spring MVC 控制器方法体之一:
UsersHome usersHome = new UsersHome();
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("entityManager");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
usersHome.setEntityManager(entityManager);
Users user = usersHome.findByUsername(login);
当我EntityManagerFactory
第一次使用时,我在线路上收到错误 - 指示我的持久性单元的名称。
我的例外是:
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is javax.persistence.PersistenceException: No Persistence
provider for EntityManager named entityManager
根本原因是:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named entityManager
我在EntityManagerFactory
. 大概我在我的Dispatcher Servlet Context
文件中犯了错误。根据 Hibernate 等,我有此代码:
<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource">
<beans:ref bean="dataSource" />
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
</beans:props>
</beans:property>
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>package.pl.models.Users</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<!-- Hibernate session factory end -->
<!-- <beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<beans:property name="persistenceUnitName" value="entityManager" />
</beans:bean> -->
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<!-- <beans:bean id="usersDao" class="hutter.pl.dao.UsersHome">
<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean> -->
我已经评论了bean id="entityManagerFactory"
and bean id="usersDao"
。有必要吗?但是我无法使用 uncommented 启动我的 Web 应用程序bean id="entityManagerFactory"
。我收到此异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'entityManagerFactory' defined in ServletContext resource
[/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method failed;
nested exception is javax.persistence.PersistenceException: No Persistence
provider for EntityManager named entityManager