0

I have tried to run my first Hibernate & Jboss AS-7 in jboss development studio IDE. so I started with a simple example and after a week struggling with it, now I can say I know nothing about following error(I am using postgresql as database)

stack trace is:

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

============================================================================

my hibernate.cfg.xml is:

<?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>

my persistence.xml is:

<?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>

============================================================================ my java program is:

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();

=============================================================================

I really appreciate any person who can help me solve this issue.``

StanislavL
  • 56,971
  • 9
  • 68
  • 98
user3239802
  • 11
  • 1
  • 3
  • by the way, I am using Hibernate 4.3.1 and postgresql 9.3 – user3239802 Jan 27 '14 at 09:23
  • Check whether the postgres jar with driver in your classpath – StanislavL Jan 27 '14 at 09:24
  • add postgres jar then you can get it – Phoenix Bird Jan 27 '14 at 09:30
  • yes, I can confirm that postgresql-9.3-1100.jdbc4.jar is in my classpath by checking through this path in jboss development studio: Sysmtem tab>preferences>server>runtime environment>default classpath – user3239802 Jan 27 '14 at 09:33
  • See [this post][1] that explains how to create the JBoss module with the driver. [1]: http://stackoverflow.com/questions/12403428/how-to-connect-jboss-as-7-1-1-with-postgresql – StephaneM Jan 27 '14 at 09:48
  • @StephaneM could you please explain step 6 and how to do it? – user3239802 Jan 27 '14 at 10:20
  • I tried to create datasource by issuing this command: data-source add --name "Hiber_test-ds" --connection-url "jdbc:postgresql://localhost:5432/postgres" --jndi-name "Hiber_testDatasource" --driver-name "postgresql-9.3-1100.jdbc4.jar" and result is duplicate datasource – user3239802 Jan 27 '14 at 11:12

2 Answers2

1

problem solved.

what I did is as follow:

1- because i was implementing my application using jdk 1.7 so i have to use suitable postgresql driver for that from this link: http://jdbc.postgresql.org/download.html (it should be JDBC41 Postgresql Driver Version 9.3-1100 not JDBC4 Postgresql Driver Version 9.3-1100)

2- StephaneM pointed me to the right direction to manually configure the jdbc driver in jboss AS-7 but not have to make module.xml in here --> $JBOSS_HOME/modules/org/postgresql/main and instead should have to do the following:

3- Add the jdbc sql jar into: ~\jboss7\modules\org\hibernate\main

4- and modify the module.xml there and change the <resource-root> to be <resource-root path="JDBC41 Postgresql Driver Version 9.3-1100"/>

5- do not forget to copy the JDBC41 Postgresql Driver Version 9.3-1100.jar there.

thats it. :)

user3239802
  • 11
  • 1
  • 3
0

You should not edit modules that come from JBoss. Since you use JPA and a persistence.xml, you don't need the hibernate.cfg.xml. Since you use a datasource (which is good), you configure the connection.url, connection.username and connection.password in standalone.xml. You don't need these properties in persistence.xml.

You need to configure the datasource in 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>

Then you create a new module in $JBOSS_HOME/modules/org/postgresql/jdbc/main with modules.xml:

<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>

Instead of creating the module you can also deploy the driver, but I usually use a module.

Erhard Siegl
  • 557
  • 2
  • 8