2016-02-09 3 views
1

Я пытаюсь настроить сохранение с помощью JPA и Hibernate для Java-проекта. Я использую Oracle 11g express для моей базы данных. Я занимаюсь этим уже несколько часов, но независимо от того, что я делаю, я всегда получаю это исключение, когда пытаюсь создать EntityManagerFactory: я нашел немало похожих вопросов относительно этого исключения, но никаких решений, которые я могу добраться до работы. Что я здесь делаю неправильно?Исключение из потока "main" javax.persistence.PersistenceException: Нет Удерживающий механизм для EntityManager с именем lanceurApplication

Подробно:

Ошибка я получаю это:

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

Является ли ваш persistence.xml в папке META-INF? –

+0

Да, это в папке META-INF –

+0

проблема не в папке META-INF –

ответ

0

1-удалить все ваши библиотеки баночка

2-добавить их снова Свойства -> Java Build Path -> Добавить файлы JAR

3-Project -> Clean

4-модификация с spring3

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> 
+0

Почему у вас есть 2 объявленных диалекта? – aelkz

Смежные вопросы