我正在编写一个 Web 应用程序,并尝试在我的项目中使用 hibernate 连接到 PostgreSQL 8.4.13 数据库。到目前为止我一直不成功,我收到以下错误(java.sql.SQLException:没有为 jdbc:postgresql://localhost:5432/mcappdb 找到合适的驱动程序)——跟踪位于底部。我已经浏览了很多论坛,但没有找到解决我问题的方法:
我已将以下依赖项包含到我的 pom.xml 中,并且构建成功。我正在使用“mvn jboss-as:deploy”部署到 jboss eap 6。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.5.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-702.jdbc4</version>
</dependency>
我已经配置了 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="mcappdb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>uk.co.metasphere.entities.userConfig</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mcappdb"/>
<property name="hibernate.connection.username" value="mcapp"/>
<property name="hibernate.connection.password" value="mc2000"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
</properties>
</persistence-unit>
该文件位于 src/main/resources/META-INF 目录中。
该应用程序部署成功,但在尝试使用休眠连接到数据库时引发错误。我从我的应用程序连接的代码如下:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mcappdb");
EntityManager em1 = emf.createEntityManager();
userConfig uscon = em1.find(userConfig.class, 0);
调用 em1.find() 时会引发错误。
我已经阅读了许多论坛帖子。大多数表明错误是因为驱动程序不在类路径上或定义的 url 错误。驱动程序 jar 文件位于 /target/MCapp-1.0/WEB-INF/lib/postgresql-8.4-702.jdbc4.jar 中,我相信它部署在与所有其他相关 jar 文件相同的位置(正确吗?)。如果我通过基本的 JBDC 连接(而不是休眠)连接,我使用的 url 有效,所以我相信这也可以。
另一个建议是我需要使用以下代码加载驱动程序:
Class.forName("org.postgresql.Driver");
这也无济于事。
由于直接的 JDBC 连接工作正常,Hibernate 似乎是问题所在?谁能告诉我问题可能是什么或如何进一步调查?hibernate 在哪里/如何查找数据库驱动程序?我是否需要将它部署到另一个目录或进行另一个配置步骤以便它可以找到它?
感激地收到任何帮助。
错误跟踪:
15:12:53,932 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/MCapp-1.0].[Mcapp Application]] (http-/0.0.0.0:8080-1) Servlet.service() for servlet Mcapp Application threw exception: java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/mcappdb
at java.sql.DriverManager.getConnection(DriverManager.java:596) [rt.jar:1.7.0_25]
at java.sql.DriverManager.getConnection(DriverManager.java:187) [rt.jar:1.7.0_25]
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:193) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.extractPhysicalConnection(ConnectionProxyHandler.java:82) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at com.sun.proxy.$Proxy17.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:147) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:166) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:145) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1711) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.doQuery(Loader.java:828) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.loadEntity(Loader.java:2033) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3719) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:449) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:418) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:251) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1079) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.SessionImpl.access$2200(SessionImpl.java:172) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2425) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:802) [hibernate-entitymanager-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:776) [hibernate-entitymanager-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at uk.co.metasphere.McappUI.init(McappUI.java:210) [classes:]
at com.vaadin.ui.UI.doInit(UI.java:607) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238) [vaadin-server-7.1.5.jar:7.1.5]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.1.Final-redhat-1.jar:1.0.1.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:489) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:679) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931) [jbossweb-7.0.16.Final-redhat-1.jar:]
at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]