-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Help with Hello World Hibernate
PostPosted: Wed Sep 30, 2009 9:43 pm 
Newbie

Joined: Wed Sep 30, 2009 9:33 pm
Posts: 4
Hello All

I downloaded and installed Hibernate.

I created the hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "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.url">jdbc:postgresql://localhost:5432/gunnar</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">password</property>
        <!-- <property name="hibernate.default_schema">public</property> -->
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
         <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <!--  <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -->

           
        <!-- Drop and re-create the database schema on startup -->
        <!--<property name="hbm2ddl.auto">update</property> -->

        <mapping resource="hello/Teste.hbm.xml"/>

       
    </session-factory>
</hibernate-configuration>


The model Teste class:
Code:
package hello;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class Teste {
    private int id;

    public int getId() {
   return id;
    }

    public void setId(int id) {
   this.id = id;
    }

}


the hbm file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hello">
  <class name="Teste" table="teste">
     <id name="id" column="id">
       <generator class="native"/>           
    </id>     
  </class>
</hibernate-mapping>


The Util class:
Code:
package hello;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateSAUtil {
   private static final SessionFactory sessionFactory;

   static {
      try {

         sessionFactory = new Configuration().configure().buildSessionFactory();
      } catch (Throwable ex) {
         // Log exception!
         throw new ExceptionInInitializerError(ex);
      }
   }

   public static Session getSession() throws HibernateException {
      return sessionFactory.openSession();
   }
}



and the main class:
Code:
package hello;

import org.hibernate.Session;

public class Main {
   public static void main(String[] args) {
      HibernateSAUtil util = new HibernateSAUtil();
      Session session = util.getSession();
      
      
   }
}



When I run the code from the Main class I get the following exception:
Code:
Exception in thread "main" java.lang.ExceptionInInitializerError
   at hello.HibernateSAUtil.<clinit>(HibernateSAUtil.java:18)
   at hello.Main.main(Main.java:7)
Caused by: java.lang.AbstractMethodError: org.postgresql.jdbc2.Jdbc2DatabaseMetaData.supportsGetGeneratedKeys()Z
   at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:123)
   at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)
   at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)
   at hello.HibernateSAUtil.<clinit>(HibernateSAUtil.java:15)
   ... 1 more


I can connect to the database using jdbc without problems.
I really have no idea what I am doing wrong here....any help is welcome.

best regards


Last edited by gk-bgh on Fri Oct 02, 2009 2:39 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Help with Hello World Hibernate
PostPosted: Thu Oct 01, 2009 12:48 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 6:39 am
Posts: 44
Location: Mumbai, India
hibernate.cfg.xml in your classpath?

_________________
Thx,
Murugesan.
Web: http://www.murugesanpitchandi.com


Top
 Profile  
 
 Post subject: Re: Help with Hello World Hibernate
PostPosted: Thu Oct 01, 2009 1:48 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Obviously it is. Otherwise there wouldn't be "postgresql" in the stacktrace...


Top
 Profile  
 
 Post subject: Re: Help with Hello World Hibernate
PostPosted: Thu Oct 01, 2009 2:14 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Code:
Caused by: java.lang.AbstractMethodError: org.postgresql.jdbc2.Jdbc2DatabaseMetaData.supportsGetGeneratedKeys()Z


This looks like a JDBC2 driver. You need a JDBC3 driver.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Re: Help with Hello World Hibernate
PostPosted: Thu Oct 01, 2009 8:12 am 
Newbie

Joined: Wed Sep 30, 2009 9:33 pm
Posts: 4
Thanks for your replies!

Christian, I´m going to change the driver and I will post the results later.

Cheers


Top
 Profile  
 
 Post subject: Re: Help with Hello World Hibernate
PostPosted: Thu Oct 01, 2009 10:12 pm 
Newbie

Joined: Wed Sep 30, 2009 9:33 pm
Posts: 4
Hi all,

as Christian said the problem was the driver. I changed the database to Mysql with no changes whatsoever to the code and my main class ran fine after that, even with annotations.

Thanks all for your replies.
Cheers


Top
 Profile  
 
 Post subject: Re: Help with Hello World Hibernate
PostPosted: Fri Oct 02, 2009 1:29 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Ooouch,
you can download the correct driver here http://jdbc.postgresql.org/download.html.


Top
 Profile  
 
 Post subject: Re: Help with Hello World Hibernate
PostPosted: Fri Oct 02, 2009 1:07 pm 
Newbie

Joined: Wed Sep 30, 2009 9:33 pm
Posts: 4
I tried that before changing the DB. The problem was that my JRE has a postgres driver inside it (the one with problem),
so I couldn´t change which driver the project used even if I changed the driver load order.

Cheers


Top
 Profile  
 
 Post subject: Re: Help with Hello World Hibernate
PostPosted: Mon Oct 05, 2009 8:18 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Why does your JRE contain a database driver?!?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.