Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect hibernate with PostgreSQL in eclipse?

I try to connect hibernate with PostgreSQL, but I don't know why it is not working. I have searched many posts about the setting, but they are not working for me. Thank you very much!

Below is the structure of my files:

enter image description here

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.PostgreSQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      org.postgresql.Driver
   </property>
   <property name="hibernate.connection.url">
      jdbc:postgresql://localhost:5432/hibernatedb
   </property>
   <property name="hibernate.connection.username">
      eric
   </property>
   <property name="hibernate.connection.password">
      eric123
   </property>

   <mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>
</hibernate-mapping>

Employee.java

package hibernate.application;

    public class Employee {
       private int id;
       private String firstName; 
       private String lastName;   
       private int salary;  

       public Employee() {}
       public Employee(String fname, String lname, int salary) {
          this.firstName = fname;
          this.lastName = lname;
          this.salary = salary;
       }
       public int getId() {
          return id;
       }
       public void setId( int id ) {
          this.id = id;
       }
       public String getFirstName() {
          return firstName;
       }
       public void setFirstName( String first_name ) {
          this.firstName = first_name;
       }
       public String getLastName() {
          return lastName;
       }
       public void setLastName( String last_name ) {
          this.lastName = last_name;
       }
       public int getSalary() {
          return salary;
       }
       public void setSalary( int salary ) {
          this.salary = salary;
       }
    }

ManageEmployee.java

public class ManageEmployee {
   private static SessionFactory factory;
   private static ServiceRegistry serviceRegistry;

   public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
        factory = configuration.buildSessionFactory(serviceRegistry);   

        ManageEmployee ME = new ManageEmployee();
        Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);
        ...
   }   
}

Error Messages

2013/9/26 上午 10:40:56 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
2013/9/26 上午 10:40:56 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
2013/9/26 上午 10:40:56 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2013/9/26 上午 10:40:56 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2013/9/26 上午 10:40:56 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2013/9/26 上午 10:40:56 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2013/9/26 上午 10:40:56 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
2013/9/26 上午 10:40:56 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2013/9/26 上午 10:40:56 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Exception in thread "main" org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver org.postgresql.Driver could not be loaded
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:111)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)
    at hibernate.application.ManageEmployee.main(ManageEmployee.java:18)
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.postgresql.Driver]
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:149)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver
    at java.lang.Class.forName(Class.java:172)
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:146)
    ... 12 more
like image 627
LoveTW Avatar asked Sep 26 '13 02:09

LoveTW


1 Answers

I finally find the way to solve it!!!

I have to import JDBC file of PostgreSQL (postgresql-9.2-1003.jdbc4.jar). This file can let eclipse know how to connect with PostGreSQL.

like image 160
LoveTW Avatar answered Oct 11 '22 15:10

LoveTW