Utilising the Java JPA with Eclipse Link I am attempting to persist a simple Post model of a Blog application into a MySQL database however I am recieving the error No Persistence provider for EntityManager named
Here is the code I am utilising
Exception Text
javax.persistence.PersistenceException: No Persistence provider for EntityManager named 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>
Post 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;
}
}
Post Controller
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();
}
}