Java 连接 PostgreSQL 9.2.1 和 Hibernate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16572039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:30:11  来源:igfitidea点击:

Connecting PostgreSQL 9.2.1 with Hibernate

javahibernatepostgresqlpostgresql-9.2

提问by Hrishikesh Choudhari

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

我有一个空白的 Spring MVC 项目,我已经使用 Maven 安装了 Hibernate 和 PostgreSQL 驱动程序。

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

我缺少展示如何将 PostgreSQL 与 Hibernate 连接的完整教程。

Any help here?

这里有什么帮助吗?

采纳答案by Sanjaya Liyanage

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

这是 posgresql 的 hibernate.cfg.xml,它将帮助您进行 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>

回答by derp

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

如果项目是maven放置的src/main/resources,在打包阶段会复制进去../WEB-INF/classes/hibernate.cfg.xml

回答by waseem khan

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

这是用于连接 postgresql 9.5 的 hibernate.cfg.xml 文件,这有助于您进行基本配置。

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

确保文件位置应在src/main/resources/hibernate.cfg.xml 下

回答by venu mannepalli

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.

是的,通过使用带有休眠配置文件的 spring-boot,我们可以将数据持久化到数据库中。在 src/main/resources 文件夹中保持休眠 .cfg.xml 以读取与数据库相关的配置。

enter image description here

在此处输入图片说明