Java JPA - 没有名为EntityManager的持久性提供程序

时间:2014-10-12 20:57:44

标签: java mysql maven jpa eclipselink

在Eclipse Link中使用Java JPA我试图将Blog应用程序的简单Post模型保存到MySQL数据库中,但是我收到错误No Persistence provider for EntityManager named

这是我正在使用的代码

例外文字

  

javax.persistence.PersistenceException:没有持久性提供程序   EntityManager名为blog     javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)     javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)     com.uk.jacob.blog.controllers.PostController。(PostController.java:9)     com.uk.jacob.blog.controllers.HomeController.doGet(HomeController.java:51)     javax.servlet.http.HttpServlet.service(HttpServlet.java:620)     javax.servlet.http.HttpServlet.service(HttpServlet.java:727)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

应用结构

app structure

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.uk.jacob</groupId>
  <artifactId>blog</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Jacob Clark Blog</name>
  <description>A blog for jacob.uk.com</description>

  <dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <version>2.6.0-M3</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.33</version>
    </dependency>
  </dependencies>
</project>

的persistence.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="blog" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>uk.com.jacob.blog.models.Post</class>
       <properties>
           <property name="javax.persistence.jdbc.url"
                   value="jdbc:mysql://localhost:3306/jpadb"/>
           <property name="javax.persistence.jdbc.user" value="root"/>
          <property name="javax.persistence.jdbc.password" value="root"/>
          <property name="javax.persistence.jdbc.driver"
                   value="com.mysql.jdbc.Driver"/>
           <property name="eclipselink.logging.level" value="FINE"/>
          <property name="eclipselink.ddl-generation" 
               value="create-tables"/>
       </properties>
   </persistence-unit>
</persistence>

发布Bean

package com.uk.jacob.blog.models;

import java.util.Date;
import javax.persistence.*;

@Entity
public class Post {
    @Id
    private int     _id;
    private String  _title;
    private String  _author;
    private String  _body;
    private Date    _published;

    public int getId(){
        return _id;
    }

    public void setId(int _id){
        this._id = _id;
    }

    public String getTitle() {
        return _title;
    }

    public void setTitle(String _title) {
        this._title = _title;
    }

    public String getAuthor() {
        return _author;
    }

    public void setAuthor(String _author) {
        this._author = _author;
    }

    public String getBody() {
        return _body;
    }

    public void setBody(String _body) {
        this._body = _body;
    }

    public Date getPublished() {
        return _published;
    }

    public void setPublished(Date _published) {
        this._published = _published;
    }

}

后置控制器

package com.uk.jacob.blog.controllers;

import javax.persistence.*;

import com.uk.jacob.blog.models.Post;

public class PostController {

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("blog");
    private EntityManager em = emf.createEntityManager();
    private EntityTransaction tx = em.getTransaction();

    public Post createPost(Post post){
        tx.begin();
        em.persist(post);
        tx.commit();
        return post;
    }

    public Post getPost(int id){
        return em.find(Post.class, id);
    }

    public void removePost(Post post){
        em.remove(em.merge(post));
    }

    public void updatePost(Post post){
        tx.begin();
            Post postToBeUpdated = em.merge(post);
        tx.commit();
    }

}

1 个答案:

答案 0 :(得分:0)

这是因为你的pom.xml中没有eclipselink依赖。也 你不需要

<dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>javax.persistence</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa</artifactId> <version>2.6.0-M3</version> </dependency>

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.4.2</version>
</dependency>

就足够了(版本可能会改变)