JPA and web app

865 views Asked by At

I am building a simple webapp to practice java web development. I am using Netbeans and maven and I tried to add JPA support manually adding the following file which is the standard configuration file in JPA.

src/main/resources/META-INF/persistence.xml

Also I have a simple Entity class :

package com.pet.shop.repository;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Animal implements Serializable{
    @Id
    private int id;
}

Then I tried to call my entitymanager like this :

EntityManagerFactory emf = Persistence.createEntityManagerFactory("PetShopJPA");

However I get the following error :

enter image description here

My pom.xm file has the following dependencies:

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.17</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

My web.xml just has a servlet configuration :

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>IndexServlet</servlet-name>
        <servlet-class>com.pet.shop.servlets.IndexServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>IndexServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

My tomcat /lib has :

enter image description here

My persistence.xml is :

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
  <persistence-unit name="PetShopJPA" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.pet.shop.repository.Animal</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/petshop"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.password" value="m$1234"/>
    </properties>
  </persistence-unit>
</persistence>

Please any help.

2

There are 2 answers

0
afym On

I decided used eclipse IDE and I solved the problem. Netbeans is not working in my case. Also I decided worked with EclipseLink 2.x.

2
Alisson Vieira On

You need to configure your persistence provider in your persistence.xml.

<persistence>
    <persistence-unit name="PetShopJPA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>...</jta-data-source>
        <...>...</...>
        ...
        ...
    </persistence-unit>
</persistence>

What is your persistence.xml configuration?

@Edit

Depending on your provider of choice, you'll need to add the maven dependencies for the provider.

Example for Hibernate Provider:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.3.2.GA</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>