Connecting PostgreSQL 9.2.1 with Hibernate
Asked Answered
C

4

87

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?

Catacaustic answered 15/5, 2013 at 18:5 Comment(0)
B
152

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>
Bootie answered 15/5, 2013 at 18:11 Comment(6)
This is great, but where do I place this file? In WEB-INF?Catacaustic
@HrishikeshChoudhari it should be somewhere in your build path.I think WEB-INF will be fine.I don't have much understanding about web projects,but I feel that WEB-INF is in the build path.Bootie
org.hibernate.dialect.PostgreSQLDialect is deprecated. you should use org.hibernate.dialect.PostgreSQL82Dialect insteadNixon
For 9.2 and later, you should use org.hibernate.dialect.PostgreSQL92Dialect instead.Intort
is this still valid?Baxley
WEB-INF is a web folder and not necessary existing in a project.Citarella
T
8

This is the hibernate.cfg.xml file to connect postgresql 9.5 and this is help to you basic configuration.

 <?xml version='1.0' encoding='utf-8'?>

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration
>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5433/hibernatedb</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">password</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <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.internal.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>
        <mapping class="com.waseem.UserDetails"/>
    </session-factory>
</hibernate-configuration>

Make sure File Location should be under src/main/resources/hibernate.cfg.xml

Tarlatan answered 17/11, 2016 at 3:58 Comment(0)
P
6

If the project is maven placed it in src/main/resources, in the package phase it will copy it in ../WEB-INF/classes/hibernate.cfg.xml

Poach answered 15/5, 2013 at 19:38 Comment(0)
Z
-4

Yes by using spring-boot with hibernate configuration files we can persist the data to the database. keep hibernating .cfg.xml in your src/main/resources folder for reading the configurations related to database.

enter image description here

Zoltai answered 5/3, 2018 at 2:24 Comment(1)
Please edit your post and show the actual code / XML as text instead of screenshots. Others can't copy and paste from your images. See here for details. Thank you.Expression

© 2022 - 2024 — McMap. All rights reserved.