线程“main”中的异常javax.persistence.PersistenceException:没有名为lanceurApplication的EntityManager的持久性提供程序

时间:2016-02-09 14:29:17

标签: java hibernate jpa oracle11g spring-3

我正在尝试使用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 个答案:

答案 0 :(得分:0)

1 - 删除所有库jar

2再添加它们属性 - &gt; Java构建路径 - &gt;添加JAR

3-Project - &gt;清洁

使用spring3进行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;
    }

}

application-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>