我试图运行我的第一个Hibernate&JBOSS AS-7在JBOSS开发工作室IDE中。因此,我从一个简单的示例开始,一周后,我可以说我对以下错误一无所知(我使用PostgreSQL作为数据库)
堆栈跟踪是:
12:18:14,080 WARN [org.hibernate.engine.jdbc.internal.JdbcServicesImpl] (http--0.0.0.0-8080-1) HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:postgresql://localhost:5432/postgres
12:18:14,081 INFO [org.hibernate.dialect.Dialect] (http--0.0.0.0-8080-1) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
12:18:14,081 INFO [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (http--0.0.0.0-8080-1) HHH000422: Disabling contextual LOB creation as connection was null
12:18:14,082 INFO [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (http--0.0.0.0-8080-1) HHH000399: Using default transaction strategy (direct JDBC transactions)
12:18:14,083 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (http--0.0.0.0-8080-1) HHH000397: Using ASTQueryTranslatorFactory
12:18:14,088 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] (http--0.0.0.0-8080-1) HHH000227: Running hbm2ddl schema export
12:18:14,088 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (http--0.0.0.0-8080-1) HHH000231: Schema export unsuccessful: java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgres
=====================================================================================
我的Hibernate.cfg.xml是:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">amir</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.listeners.envers.autoRegister">false</property>
</session-factory>
我的persistence.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<persistence 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"
version="2.0">
<persistence-unit name="Hiber_test" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/Hiber_testDatasource</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="amir"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/Hiber_testEntityManagerFactory"/>
</properties>
</persistence-unit>
</persistence>
=======================================================================================我的Java程序是:
try {
Class.forName("org.postgresql.Driver");
//on classpath
} catch(ClassNotFoundException e) {
// not on classpath
System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK" + e);
}
Configuration cfg = new Configuration()
.addResource("Event.hbm.xml").configure();
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession();
======================================================================================
我非常感谢任何可以帮助我解决这个问题的人。
解决问题。
我所做的如下:
1-因为我是在使用JDK 1.7实现应用程序的,因此我必须从此链接中使用合适的PostgreSQL驱动程序:http://jdbc.postgresql.org/download.html-1100不是JDBC4 PostgreSQL驱动程序版本9.3-1100)
2-史蒂芬姆指向我指向正确的方向,以手动配置JBOSS AS-7中的JDBC驱动程序,但不必在此处使用Module.xml-> $ JBOSS_HOME/MODULES/org/org/postgresql/main/main而应必须执行以下操作:
3-将JDBC SQL JAR添加到:〜 jboss7 模块 org hibernate main
4-并在此处修改模块,然后将<resource-root>
更改为 <resource-root path="JDBC41 Postgresql Driver Version 9.3-1100"/>
5-不要忘记复制JDBC41 PostgreSQL驱动程序版本9.3-1100.jar。
就是这样。:)
您不应编辑来自JBOSS的模块。由于您使用JPA和permistence.xml,因此您不需要Hibernate.cfg.xml。由于您使用dataSource(很好),因此您在standalone.xml中配置连接。您不需要这些属性。xml。
您需要在standalone.xml中配置数据源:
<subsystem xmlns="urn:jboss:domain:datasources:1.1">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jndi-name="java:/Hiber_testDatasource" pool-name="bookingDatasource" enabled="true">
<connection-url>jdbc:postgresql://localhost:5432/postgres</connection-url>
<driver>postgresql</driver>
<security>
<user-name>postgres</user-name>
<password>amir</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="postgresql" module="org.postgresql.jdbc">
<xa-datasource-class>org.postgresql.Driver</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
然后,您在$ jboss_home/org/postgresql/jdbc/main中创建一个新的模块,使用模块:
<module xmlns="urn:jboss:module:1.1" name="org.postgresql.jdbc">
<resources>
<resource-root path="postgresql-9.1-901.jdbc4.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
而不是创建模块,您也可以部署驱动程序,但我通常使用模块。