线程" main"中的例外情况javax.persistence.PersistenceException:没有名为TEST

时间:2016-12-22 13:59:50

标签: entitymanager hibernate-entitymanager

我在JPA的一个项目上工作,我将它连接到一个MySQL数据库,其中包含已经使用GlassFish服务器填充的表格,我创建了一个测试类来测试显示一个表的列,但是我还有即使我已经配置了persistence.xml文件

,也会出现持久性提供程序问题

实体:

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;
}
}

主要:

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>

我尝试将Main和Entity放在同一个包中,但我仍然遇到同样的问题。

结果:

PersistenceException

1 个答案:

答案 0 :(得分:0)

这是异常代码:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named testJPA
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at org.test.Test.main(Test.java:24)