今晚我发疯了一段时间,试图用 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,应在此行将其注入通用存储库:
@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>