I work on a project in JPA, I connected it with a MySQL database that contains tables already filled using the GlassFish server, I created a test class to test to display a column of a table With a SELECT query but I still have a persistence provider problem even though I have configured my persistence.xml file
Entity :
package org.planetjpa;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Entity
@Table(name="films", schema = "database")
public class Films implements Serializable {
@Id@Column(name="id")
private int id_film;
@Column(name="titre")
private String nom_film;
@Column(name="realisateur")
private String realisateur;
@Column(name="date")
private Date date_film;
@Column(name="cadre")
private String cadre;
@Column(name="lieu")
private String lieu_film;
@Column(name="arrondissement")
private int arrondissement;
@Column(name="adresse")
private String adresse;
@Column(name="cinema")
@SequenceGenerator(name = "id_Sequence_cinema", sequenceName = "ID_SEQ_CINEMA")
private String cinema;
@Column(name="prix")
private double prix;
@Column(name="nb_personne")
private int nb_places;
private static final long serialVersionUID = 1L;
public Films() {
super();
}
public int getId_film() {
return id_film;
}
public void setId_film(int id_film) {
this.id_film = id_film;
}
public String getNom_film() {
return nom_film;
}
public void setNom_film(String nom_film) {
this.nom_film = nom_film;
}
public String getRealisateur() {
return realisateur;
}
public void setRealisateur(String realisateur) {
this.realisateur = realisateur;
}
public Date getDate_film() {
return date_film;
}
public void setDate_film(Date date_film) {
this.date_film = date_film;
}
public String getCadre() {
return cadre;
}
public void setCadre(String cadre) {
this.cadre = cadre;
}
public String getLieu_film() {
return lieu_film;
}
public void setLieu_film(String lieu_film) {
this.lieu_film = lieu_film;
}
public int getArrondissement() {
return arrondissement;
}
public void setArrondissement(int arrondissement) {
this.arrondissement = arrondissement;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public String getCinema() {
return cinema;
}
public void setCinema(String cinema) {
this.cinema = cinema;
}
public double getPrix() {
return prix;
}
public void setPrix(double prix) {
this.prix = prix;
}
public int getNb_places() {
return nb_places;
}
public void setNb_places(int nb_places) {
this.nb_places = nb_places;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Main :
package org.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.*;
import org.planetjpa.Films;
import java.util.List;
public class Test {
public static void main(String[] args) {
Films book = new Films();
// Obtains an entity manager and a transaction
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TEST");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
// Persists the book to the database
tx.begin();
em.persist(book);
tx.commit();
TypedQuery<Films> tq =em.createQuery("Select b from Films b",Films.class);
List<Films> lb = tq.getResultList();
for (Films b:lb) System.out.println("######### " + b.getCinema());
//Closes the entity manager and the factory
//em.close();
emf.close();
}
}
persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<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="TEST" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.planetjpa.Films</class>
<class>org.planetjpa.Lieux</class>
<class>org.planetjpa.Scences</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/database/projetjsf"/>
<property name="javax.persistence.jdbc.username" value="root"/>
<property name="javax.persistence.jdbc.password" value="amine1993"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
I tried to put the Main and the Entity in the same package but i still have the same problem.
Result :
here is the exception code :