Connecting PostgreSQL 9.2.1 with Hibernate

Hrishikesh Choudhari picture Hrishikesh Choudhari · May 15, 2013 · Viewed 190k times · Source

I have a blank Spring MVC project, and I've installed Hibernate and the PostgreSQL drivers using Maven.

I'm running short on complete tutorials that show how to connect PostgreSQL with Hibernate.

Any help here?

Answer

Sanjaya Liyanage picture Sanjaya Liyanage · May 15, 2013

This is a hibernate.cfg.xml for posgresql and it will help you with basic hibernate configurations for posgresql.

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "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.username">postgres</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>



        <property name="connection_pool_size">1</property>

        <property name="hbm2ddl.auto">create</property>

        <property name="show_sql">true</property>



       <mapping class="org.javabrains.sanjaya.dto.UserDetails"/>

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