无法在hibernate中使用jpa创建实体管理器工厂
我正在使用jpa、hibernate和eclipse构建一个SpringMVC应用程序。以下是我的控制器:无法在hibernate中使用jpa创建实体管理器工厂,hibernate,spring-mvc,jpa,Hibernate,Spring Mvc,Jpa,我正在使用jpa、hibernate和eclipse构建一个SpringMVC应用程序。以下是我的控制器: package com.something.controller; import java.util.ArrayList; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persisten
package com.something.controller;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.drc.model.Project;
import com.drc.service.ProjectService;
@RestController("projectController")
@RequestMapping("/project")
public class ProjectController {
@Autowired
private ProjectService projectService;
@RequestMapping(value = "/{projectName}", method = RequestMethod.GET)
public String createProject (@PathVariable String projectName)
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
EntityManager em = emf.createEntityManager();
TypedQuery<Project> query= em.createQuery("from project where name=:projectName",Project.class);
List<Project> results= query.getResultList();
if(results.size()>0)
{
return "there are entries";
}
else
{
return "there are no entries";
}
}
这是我的jpaContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.drc" />
<jpa:repositories base-package="com.drc.repository"/>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"></entry>
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.format_sql" value="true" />
</map>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/apecadprojects?autoReconnect=true" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
</beans>
Whenever i browse to the page say localhost:8080/projectname/project/myname i get the following error
正如您所见,它不允许我创建EntityManagerFactory。为什么
下面是我的persistence.xml
<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="punit">
<provider>org.hibernate.jpa.PersistenceProvider</provider>
</persistence-unit>
</persistence>
我做错了什么
更新Ok now在尝试了几个小时后现在我得到了以下错误,请有人指出问题出在哪里??我的hql错了吗
确保persistence.xml位于类路径的META-INF目录中。看起来您没有在持久性xml中添加类名 将其添加到持久性xml中
<class>yourpackage.Project</class>
在您的Entity manager bean中添加proeprity
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
首先,我要做的是将REST控制器与您的存在性解耦,以尽可能减少可能会妨碍它更好的设计、更模块化、更易于测试等的变量。。不管怎么说,多次创建EntityManager没有多大意义,尤其不是在每次客户端请求时。这只是测试代码……。我不打算每次客户端请求时都创建它……。可能缺少提供程序?类路径中是否存在org.hibernate.jpa.PersistenceProvider?Shaprio…….我在春季是个新手…你的意思是它应该在hibernate实体管理器jar中的maven Dependencies中吗?如果你想使用hibernate作为你的jpa提供程序,你必须在pom.xml中有所需的jar。此外,如果您正在集成Spring和JPA,我不认为您需要自己创建EntityManager,而是使用@PersistenceContext并让Spring将其注入到您的类中。有很多这样做的指南,请查看这一条:添加第一行后,它会说Class package.projectname无法解析请确保提供了整个包的名称。。com.drc.model.project应该在里面添加这个类nope不起作用,仍然得到相同的错误…….这个类无法解决,很抱歉出现了问题,但是我需要在persistence.xml org.hibernate.ejb.HibernatePersistence中添加以下行,它起作用了。。。。。。无论如何,感谢你们的帮助,我的hql查询是错误的……这就是为什么我得到语法异常错误的原因……。解决了它