【问题标题】:Not being able to create entity manager factory using jpa in hibernate无法在休眠中使用 jpa 创建实体管理器工厂
【发布时间】: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


【解决方案1】:

确保 persistence.xml 位于类路径的 META-INF 目录中。

【讨论】:

  • 嗯,我要做的第一件事就是将 REST 控制器与您的权限解耦,以尽可能减少可能会妨碍的变量(这是更好的设计、更模块化、更易于测试等......)无论如何,多次创建 EntityManager 并没有多大意义(尤其是不是在每个客户端请求上)。
  • 这只是测试代码.......我不打算每次客户请求时都创建它......
  • 可能缺少提供者? org.hibernate.jpa.PersistenceProvider 是否存在于类路径中?
  • Shaprio........我是春天的新手......你的意思是它应该在我的休眠实体管理器 jar 内的 maven 依赖项中吗??
  • 如果你想使用 Hibernate 作为你的 JPA 提供者你必须在你的 pom.xml 中有需要的 jars。此外,如果您正在集成 Spring 和 JPA,我认为您不需要自己创建 EntityManager,而是使用 @PersistenceContext 并将 Spring 注入到您的类中。有很多这样做的指南,看看这个:objectdb.com/tutorial/jpa/eclipse/spring
【解决方案2】:

您似乎没有在持久性 xml 中添加类名。

在你的持久化 xml 中添加这个

<class>yourpackage.Project</class>

并在您的实体管理器 bean 中添加属性

<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />

【讨论】:

  • 添加第一行后显示 Class "package.projectname" cannot be resolved
  • 确保你提供了整个包名.. com.drc.model.Project
  • 类应该添加到里面
  • 不工作仍然得到同样的错误........类“”无法解决
  • 很抱歉给您带来麻烦,但我需要将以下行添加到 persistence.xml org.hibernate.ejb.HibernatePersistence 并且它起作用了......无论如何,谢谢帮助尼基尔
猜你喜欢
  • 1970-01-01
  • 2020-01-16
  • 2016-03-17
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 2013-09-23
  • 2014-05-20
  • 2016-07-04
相关资源
最近更新 更多