Jpa java.lang.ClassNotFoundException:无法加载请求的类:org.postgresql.Driver
我的问题是,当我在Bean中创建DAO实例时Jpa java.lang.ClassNotFoundException:无法加载请求的类:org.postgresql.Driver,jpa,driver,classnotfoundexception,Jpa,Driver,Classnotfoundexception,我的问题是,当我在Bean中创建DAO实例时 public class ClientDAO { EntityManager entity=JPAutil.getEntityManegerFactory().createEntityManager(); public void create(Client client){ entity.getTransaction().begin(); entity.persist(client);
public class ClientDAO {
EntityManager entity=JPAutil.getEntityManegerFactory().createEntityManager();
public void create(Client client){
entity.getTransaction().begin();
entity.persist(client);
entity.getTransaction().commit();
JPAutil.shotDown();
}
public void update(Client client){
entity.getTransaction().begin();
entity.merge(client);
entity.getTransaction().commit();
JPAutil.shotDown();
}
public void find(int id){
entity.getTransaction().begin();
entity.find(Client.class,id);
}
public List<Client> findall(){
List<Client> listaC= new ArrayList<Client>();
Query q=entity.createQuery("SELECT c FROM Client c");
listaC = q.getResultList();
return listaC;
}
public Client findbyId(int idc){
Client C= new Client();
Query q=entity.createQuery("SELECT c FROM Client c WHERE id="+idc);
C = (Client) q.getSingleResult();
return C;
}
}
谢谢你抽出时间
xml我已经使测试和本地JPA工作正常。问题是当我尝试在服务器中使用它时。在这个Pom中,有一个驱动程序von Postgres和连接器Hibernate
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>pruebe</artifactId>
<version>1.0-SNAPSHOT</version>
<!---Servlet-->
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.10.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>```
4.0.0
groupId
普鲁比
1.0-快照
爪哇
JavaEEAPI
8
假如
org.hibernate
休眠实体管理器
5.4.10.最终版本
postgresql
postgresql
9.1-901.jdbc4
编译
org.apache.maven.plugins
maven编译器插件
3.5.1
1.8
1.8
```
您需要将JDBC驱动程序下载到项目中的数据库中,您可以从这里下载
您的gradle或pom文件是否具有postresql依赖关系?添加配置文件DataSource大家好谢谢您的回答我的maven中具有依赖关系。我将添加一张关于此的照片=Di在POM依赖项中具有驱动程序。这就是问题所在,问题是当我在Bean中创建DAO实例时。谢谢你的回答=D
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>pruebe</artifactId>
<version>1.0-SNAPSHOT</version>
<!---Servlet-->
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.10.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>```