【发布时间】:2014-03-19 14:27:00
【问题描述】:
我正在使用 jpa 、 hibernate 和 eclipse 来构建一个 spring mvc 应用程序。以下是我的控制器:
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>
我做错了什么??
更新好现在尝试了几个小时后我得到以下错误可以请有人指出问题是什么吗?我的 hql 错了吗??
【问题讨论】:
-
伙计们,我的 hql 查询是错误的......这就是为什么我得到语法异常错误............搞清楚了
标签: hibernate spring-mvc jpa