gpt4 book ai didi

java - 线程 "main"javax.persistence.PersistenceException : No Persistence provider for EntityManager named lanceurApplication 中出现异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:08 25 4
gpt4 key购买 nike

我正在尝试使用 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)

持久性.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();
}

}

地址.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-删除所有库jar

2-再次添加它们属性 -> Java 构建路径 -> 添加 JAR

3-项目 -> 清理

4-用spring3修改

地址.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;
}

}

应用程序上下文.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>

持久性.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>

关于java - 线程 "main"javax.persistence.PersistenceException : No Persistence provider for EntityManager named lanceurApplication 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com