【发布时间】:2013-03-09 04:23:58
【问题描述】:
今晚我已经发疯了一段时间,试图用 java 中的 Spring MVC 和 Hibernate 运行一个简单的项目。基本上我从一个错误跌跌撞撞到另一个错误,但它们都是由于缺少 jar 文件这一事实,我立即解决了将依赖项添加到 Maven 中的问题。这一直有效,直到出现此错误:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'emf' defined in ServletContext resource
[/WEB-INF/spring/root-context.xml]: Invocation of init method failed;
nested exception is java.lang.IllegalArgumentException: Cannot find class [Hibernate]
现在,奇怪的是找不到该类,并且异常是 Illegal Argument 而不是 ClassNotFound 或类似的东西。
我在 servlet-context 中设置了这个
<context:load-time-weaver/>
根上下文是:
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
</bean>
<bean id="taskRepo" class="com.leandro.dao.GenericRepository">
<constructor-arg>
<value>com.leandro.models.Task</value>
</constructor-arg>
</bean>
显然无法生成 emf bean,应该在这一行将其注入到 Generic Repo 中:
@PersistenceContext
private EntityManager em;
public void setEntityManager(EntityManager em) {
this.em = em;
}
我整晚都在做这个,有人知道我可能会错过什么吗?
更新:根据要求,这是持久性 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="testmvc" transaction-type="RESOURCE_LOCAL">
<provider>Hibernate</provider>
<class>com.leandro.models.Task</class>
<properties>
<property name="hibernate.connection.username" value="***"/>
<property name="hibernate.connection.password" value="***"/>
<property name="hibernate.connection.url" value="jdbc:sqlserver://localhost\SQLEXPRESS;databaseName=CoveyTMM"/>
<property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost\SQLEXPRESS;databaseName=CoveyTMM"/>
<property name="javax.persistence.jdbc.user" value="***"/>
<property name="javax.persistence.jdbc.password" value="***"/>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
</properties>
</persistence-unit>
</persistence>
【问题讨论】:
-
你的类路径中有没有休眠
-
也可以发布persistence.xml吗?我认为这个例外与此有关。
-
是的,我在 Maven 依赖项中的类路径中有 Hibernate。我首先手动添加了一些hibernate jars,然后将hibernate-entitymanager 添加到pom.xml 中,并且跨越了所有依赖项。 @sgp15 我已将持久性 xml 添加到问题中。
标签: java spring hibernate entitymanager