简体   繁体   English

异常 javax.persistence.PersistenceException 问题

[英]exception javax.persistence.PersistenceException problem

(Excuse me for my english I'm a little frenchie). (对不起我的英语我有点法国人)。 I'm new in Java ee and wanted to put a JPA ORM.我是 Java ee 的新手,想放一个 JPA ORM。 I tried and thought I was doing fine exept when I launched it on my Glassfish server.当我在 Glassfish 服务器上启动它时,我尝试并认为我做得很好。 I had: javax.persistence.PersistenceException: No Persistence provider for EntityManager named test Exception.我有:javax.persistence.PersistenceException:EntityManager 没有持久性提供程序,名为 test Exception。 I know that's it's a quite common error but I tried so many things.我知道这是一个很常见的错误,但我尝试了很多东西。 I don't know where the problem is... Here's some of my code: Article bean:我不知道问题出在哪里......这是我的一些代码:文章bean:

package beans;

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

@Entity
@Table(name = "T_Articles")
public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int idArticle;

    private String description;

    private String brand;

    @Column(name = "UnitaryPrice")
    private double price;

    public Article() {
        this("unknown", "unknown", 0);
    }

    public Article(String description, String brand, double price) {
        this.setDescription(description);
        this.setBrand(brand);
        this.setPrice(price);
    }

    public int getIdArticle() {
        return idArticle;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Article [idArticle=" + idArticle + ", description=" + description + ", brand=" + brand + ", price="
                + price + "]";
    }

}

My persistence.xml:我的坚持。xml:

<?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="test">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>beans.Article</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
        
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/bddtest"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

        </properties>
    </persistence-unit>

</persistence>

My servlet using it:我的 servlet 使用它:

package servlets;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Article;

/**
 * Servlet implementation class aaa
 */
@WebServlet("/aaa")
public class aaa extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public aaa() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
        EntityManager em = emf.createEntityManager();
        Article c;
        c = new Article();

        em.getTransaction().begin();
        em.persist(c);
        em.getTransaction().commit();

        em.close();
        emf.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

My file hierarchy: My file hierarchy我的文件层次结构:我的文件层次结构

I don't know how to fix it can u help me?我不知道如何解决它你能帮我吗? :( :(

Thanks, A Java EE newbie谢谢, Java EE 新手

I think your persistence provider in persistence.xml is wrong.我认为您在 persistence.xml 中的持久性提供程序是错误的。 Try this:尝试这个:

<persistence-unit name="customersFxPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.persistence.PersistenceException:EntityManager 没有持久性提供程序 - javax.persistence.PersistenceException: No Persistence provider for EntityManager javax.persistence.PersistenceException:事务未能刷新 - javax.persistence.PersistenceException: Transaction failed to flush 通过使用createEntityManagerFactory获得javax.persistence.PersistenceException - javax.persistence.PersistenceException by using createEntityManagerFactory Hibernate-JPA。 javax.persistence.PersistenceException - Hibernate-JPA. javax.persistence.PersistenceException javax.persistence.PersistenceException-无法构建EntityManagerFactory - javax.persistence.PersistenceException - Unable to build EntityManagerFactory 线程“ main”中的异常javax.persistence.PersistenceException:没有名为EntityTest的EntityManager的持久性提供程序 - Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named databaseTest 线程“ main”中的异常javax.persistence.PersistenceException:名为lanceurApplication的EntityManager的持久性提供程序没有 - Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named lanceurApplication 线程“主”中的异常javax.persistence.PersistenceException:名为jpa-test的EntityManager的持久性提供程序没有 - Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named jpa-test org.hibernate.exception.SQLGrammarException: 无法准备语句; 嵌套异常是 javax.persistence.PersistenceException - org.hibernate.exception.SQLGrammarException: could not prepare statement; nested exception is javax.persistence.PersistenceException 线程“main”中的异常 javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: 无法执行语句 - Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM