簡體   English   中英

線程“ main”中的異常javax.persistence.PersistenceException:名為lanceurApplication的EntityManager的持久性提供程序沒有

[英]Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named lanceurApplication

我正在嘗試使用JPA和Hibernate為Java項目設置持久性。 我將Oracle 11g express用於我的數據庫。 我已經花了幾個小時了,但是無論我做什么,當我嘗試創建EntityManagerFactory時總是會遇到此異常:我已經找到了很多與此異常有關的類似問題,但是沒有能夠解決的解決方案上班 我在這里做錯了什么?

詳細:

我得到的錯誤是這樣的:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named lanceurApplication
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at ap.s.tn.test.Main.main(Main.java:15)

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="lanceurApplication">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>ap.s.tn.beans.Adresse</class>
    <properties>
    <property name="hibernate.hbm2ddl.auto" value="create"/>
    <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
    <property name="hibernate.connection.username" value="appACTEL"/>
    <property name="hibernate.connection.password" value="appACTEL"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
    </properties>
    </persistence-unit>
</persistence>

Main.java

    package ap.s.tn.test;

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

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("lanceurApplication");
        EntityManager entityManager = emf.createEntityManager();
    }

}

Adresse.java

    package ap.s.tn.beans;

import java.io.Serializable;
import java.lang.String;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: Adresse
 *
 */
@Entity

public class Adresse implements Serializable {


    private int id_adresse;
    private String rue;
    private String ville;
    private String pays;
    private static final long serialVersionUID = 1L;

    public Adresse() {
        super();
    }   
    @Id  
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId_adresse() {
        return this.id_adresse;
    }

    public void setId_adresse(int id_adresse) {
        this.id_adresse = id_adresse;
    }   
    public String getRue() {
        return this.rue;
    }

    public void setRue(String rue) {
        this.rue = rue;
    }   
    public String getVille() {
        return this.ville;
    }

    public void setVille(String ville) {
        this.ville = ville;
    }   
    public String getPays() {
        return this.pays;
    }

    public void setPays(String pays) {
        this.pays = pays;
    }

}

1-刪除所有的圖書館罐

2再次添加它們屬性-> Java構建路徑->添加JAR

3個項目->清潔

用spring4修改4

Adresse.java:

package com.springJPA.domain;

import java.io.Serializable;
import java.lang.String;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: Adresse
 *
 */
@Entity

public class Adresse implements Serializable {


    private int id_adresse;
    private String rue;
    private String ville;
    private String pays;
    private static final long serialVersionUID = 1L;

    public Adresse() {
        super();
    }   
    @Id  
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence_adresse")
    @SequenceGenerator(name = "id_Sequence_adresse", sequenceName = "ID_SEQ_ADRESSE")
    public int getId_adresse() {
        return this.id_adresse;
    }

    public void setId_adresse(int id_adresse) {
        this.id_adresse = id_adresse;
    }   
    public String getRue() {
        return this.rue;
    }

    public void setRue(String rue) {
        this.rue = rue;
    }   
    public String getVille() {
        return this.ville;
    }

    public void setVille(String ville) {
        this.ville = ville;
    }   
    public String getPays() {
        return this.pays;
    }

    public void setPays(String pays) {
        this.pays = pays;
    }

}

Main.java:

   package com.springJPA.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springJPA.domain.Adresse;
import com.springJPA.domain.Utilisateur;
import com.springJPA.service.AdresseService;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Utilisateur utilisateur = new Utilisateur();

        //--------
        Adresse adresse = new Adresse();
        adresse.setRue("kantawi");
        adresse.setVille("Sousse");
        adresse.setPays("Tunisie");
        //--------

        ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
        AdresseService adrService = (AdresseService) context.getBean("adrService");
        adrService.ajoutAdresse(adresse);
    }

}

MyEntityManagerFactory.java:

package com.springJPA.util;

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

import org.springframework.stereotype.Component;

@Component("myEMF")
public class MyEntityManagerFactory {
    private EntityManager entityManager;
    private String unitName = "SpringJPA";

    public EntityManager getEntityManager() {
        if(entityManager == null){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName);
        entityManager = emf.createEntityManager();
        }
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public String getUnitName() {
        return unitName;
    }

    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

}

TransactionAspect.java:

package com.springJPA.util;


public class TransactionAspect {


    private MyEntityManagerFactory entityManagerFactory;

    public void begin(){
        entityManagerFactory.getEntityManager().getTransaction().begin();
    }

    public void commit(){
        entityManagerFactory.getEntityManager().getTransaction().commit();
    }

    public MyEntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }
    public void setEntityManagerFactory(MyEntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

}

應用程序的context.xml:

<?xml version="1.0" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.springJPA.service"/>
<context:component-scan base-package="com.springJPA.util"/>
<bean id="tr" class="com.springJPA.util.TransactionAspect">
    <property name="entityManagerFactory" ref="myEMF"/>
</bean>

<aop:config>
    <aop:pointcut expression="execution(* com.springJPA.service.AdresseService.*(..))" id="adrPC"/>
    <aop:pointcut expression="execution(* com.springJPA.service.UtilisateurService.*(..))" id="utlPC"/>
    <aop:aspect ref="tr">
     <aop:before pointcut-ref="adrPC" method="begin"></aop:before>
     <aop:after pointcut-ref="adrPC" method="commit"></aop:after>

     <aop:before pointcut-ref="utlPC" method="begin"></aop:before>
     <aop:after pointcut-ref="utlPC" method="commit"></aop:after>       
    </aop:aspect>
 </aop:config>

</beans>

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="SpringJPA" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.springJPA.domain.Adresse</class>
    <class>com.springJPA.domain.Utilisateur</class>
    <properties>
    <property name="hibernate.archive.autodetection" value="class" />
    <property name="hibernate.dialect" value="com.mysema.query.jpa.support.ExtendedOracleDialect" />
    <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
    <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
    <property name="hibernate.connection.username" value="appACTEL" />
    <property name="hibernate.connection.password" value="appACTEL" />
    <property name="hibernate.flushMode" value="FLUSH_AUTO" />
    <property name="hibernate.hbm2ddl.auto" value="create" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
    </properties>
    </persistence-unit>
</persistence>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM