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

时间:2016-10-16 00:56:37

标签: java jpa entity

我的错误文字:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named jpa-test

我已经从这里尝试了关于这个问题的所有想法但是没有用。我仍然得到同样的错误。 我不知道该怎么做..请不要给我链接这样的问题。我真的尝试了所有的想法,但仍然没有工作 的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="jpa-test" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.licenta.ascourses.model.Utilizator</class>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
      <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:test.db" />
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
      <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>
    </persistence-unit>
</persistence>

实体:

   package com.licenta.ascourses.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Utilizator implements Serializable {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int id_utilizator;
    private String username;
    private String parola;
    private String email;
    private String nume;
    private String prenume;
    private int cod;
    private int status;

    public Utilizator(String nume, String prenume){
        this.nume=nume;
        this.prenume=prenume;

    }



    public Utilizator(String username, String parola, String email, String nume,
            String prenume, int cod, int status) {


        this.username = username;
        this.parola = parola;
        this.email = email;
        this.nume = nume;
        this.prenume = prenume;
        this.cod = cod;
        this.status = status;
    }

    public Utilizator(String email, String nume, String prenume, int status) {
        super();
        this.email = email;
        this.nume = nume;
        this.prenume = prenume;
        this.status = status;
    }



    public Utilizator(String string) {
        this.username=string;
    }



    public int getId_utilizator() {
        return id_utilizator;
    }

    public void setId_utilizator(int id_utilizator) {
        this.id_utilizator = id_utilizator;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getParola() {
        return parola;
    }

    public void setParola(String parola) {
        this.parola = parola;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNume() {
        return nume;
    }

    public void setNume(String nume) {
        this.nume = nume;
    }

    public String getPrenume() {
        return prenume;
    }

    public void setPrenume(String prenume) {
        this.prenume = prenume;
    }

    public int getCod() {
        return cod;
    }

    public void setCod(int cod) {
        this.cod = cod;
    }

    public int isStatus() {
        return status;
    }

    public void setStatus(int status) {

        this.status = (status==1)?1:0;


    }

    public int getStatus() {


        return (this.status==1)?1:0;

    }

}

我的测试班:

package com.licenta.ascourses.DbConnection;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.licenta.ascourses.model.Utilizator;

public class JPATest {

    public static void performJPA() {

        // Creating objects representing some products
        Utilizator user1=new Utilizator("andrei", "parola", "jean@jean", "jean", "popescu", 123, 1);
        Utilizator user2=new Utilizator("miu", "parola2", "ana@ana", "ana", "cartianu", 234, 1);

        // Connecting to the database through EntityManagerFactory
        // connection details loaded from persistence.xml
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa-test");

        EntityManager em = emf.createEntityManager();

        // Creating a new transaction
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        // Persisting the product entity objects
        em.persist(user1);
        em.persist(user2);

        tx.commit();

        // Querying the contents of the database using JPQL query
        Query q = em.createQuery("SELECT * FROM Utilizatori p");

        @SuppressWarnings("unchecked")
        List<Utilizator> results = q.getResultList();

        System.out.println("List of products\n----------------");

        for (Utilizator user : results) {

            System.out.println(user.getNume() + " (id=" + user.getId_utilizator() + ")");
        }

        // Closing connection
        em.close();

        emf.close();
    }




}

1 个答案:

答案 0 :(得分:0)

您的类路径中需要JPA提供程序。示例提供商:Hibernate, Toplink, Apache OpenJPA, EclipseLink.

要了解Java的类路径,请参阅:Java Tutorial on Classpath