我试过运行我的第一个Hibernate& Jboss开发工作室IDE中的Jboss AS-7。所以我从一个简单的例子开始,经过一周的努力,现在我可以说我对跟踪错误一无所知(我使用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();
=============================================== ==============================
我非常感谢任何能帮我解决这个问题的人.`
答案 0 :(得分:0)
问题解决了。
我所做的如下:
1-因为我正在使用jdk 1.7实现我的应用程序所以我必须使用合适的postgresql驱动程序从这个链接:http://jdbc.postgresql.org/download.html(它应该是JDBC41 Postgresql驱动程序版本9.3-1100而不是JDBC4 Postgresql驱动程序版本9.3 -1100)
2- StephaneM向我指出了在jboss AS-7中手动配置jdbc驱动程序的正确方向,但不必在此处生成module.xml - &gt; $ JBOSS_HOME /模块/组织/ PostgreSQL的/主 而应该做以下事情:
3-将jdbc sql jar添加到: 〜\ jboss7 \模块\有机\冬眠\主
4-并修改那里的module.xml并将<resource-root>
更改为<resource-root path="JDBC41 Postgresql Driver Version 9.3-1100"/>
5-不要忘记复制JDBC41 Postgresql驱动程序版本9.3-1100.jar。
就是这样。 :)
答案 1 :(得分:0)
您不应该编辑来自JBoss的模块。由于您使用JPA和persistence.xml,因此不需要hibernate.cfg.xml。由于您使用数据源(这是好的),因此您可以在standalone.xml中配置connection.url,connection.username和connection.password。您在persistence.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>
然后使用modules.xml在$ JBOSS_HOME / modules / 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>
您可以部署驱动程序,而不是创建模块,但我通常使用模块。