Reputation: 79
I want to add database and persist data using JPA/hibernate. I've persistence.xml in right place but I'm getting "no persistence provider for entitymanager" error. Database is in mysql. Any suggestion is much appreciated. Here is my code:
Folder structure:
servlet:
@WebServlet("/Reservations")
public class Reservations extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
if( request != null) {
Enumeration<String> names = request.getParameterNames();
while(names.hasMoreElements()) {
String name = names.nextElement();
System.out.println(name + " : " + request.getParameter(name));
}
String pickUpDate = request.getParameter("pickUpDate");
Integer totalPass = Integer.parseInt(request.getParameter("totalPass"));
ReservationEntity reservation = new ReservationEntity(pickUpDate, totalPass);
ReservationSevice reservationSevice = new ReservationSevice();
reservationSevice.createReservation(reservation);
}else{
System.out.println("#############Request is null#########");
}
}
catch (Exception e){
e.printStackTrace();
System.out.println("Exception occured");
}
}
}
Entity class:
@Entity
@Table(name = "reservations_db")
public class ReservationEntity {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name = "pickUpDate")
private String pickUpDate;
@Column(name = "totalPass")
private Integer totalPass;
public ReservationEntity()
{
}
public ReservationEntity(String pickUpDate, Integer totalPass)
{
this.setPickUpDate(pickUpDate);
this.setTotalPass(totalPass);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPickUpDate() {
return pickUpDate;
}
public void setPickUpDate(String pickUpDate) {
this.pickUpDate = pickUpDate;
}
public Integer getTotalPass() {
return totalPass;
}
public void setTotalPass(Integer totalPass) {
this.totalPass = totalPass;
}
}
Service class for JPA persistence:
public class ReservationSevice
{
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("ReservationsPU");
private static EntityManager em = emf.createEntityManager();
public void createReservation (ReservationEntity reservation)
{
em.getTransaction().begin();
em.persist(reservation);
em.getTransaction().commit();
}
}
persistence.xml:
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ReservationsPU">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/world" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
error:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named ReservationsPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.welcomelimo.service.ReservationSevice.<init>(ReservationSevice.java:11)
at com.welcomelimo.controller.Reservations.doPost(Reservations.java:56)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Exception occured
Update
I'm adding the lib folder in order to show the jar files I've added for the project, since I'm not using maven.
Update 2
I ended up using maven and it's working now. For pom.xml, see below @Surace's answer.
Upvotes: 2
Views: 11235
Reputation: 1188
Change your persistence.xml to
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ReservationsPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/world" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="use_sql_comments" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
Add following dependencies in your pom.xml if you are using Maven
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
</dependencies>
Upvotes: 4
Reputation: 16184
I think the matter is with eclipse. Eclipse may only allow running JPA if it is explicitly set as a JPA project.
You can convert your project into a JPA project, by opening the context menu in the explorer.
Upvotes: 0