【问题标题】:Connecting PostgreSQL 9.2.1 with Hibernate将 PostgreSQL 9.2.1 与 Hibernate 连接起来
【发布时间】:2013-05-10 10:30:07
【问题描述】:

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

我在展示如何将 PostgreSQL 与 Hibernate 连接起来的完整教程上还不够。

这里有什么帮助吗?

【问题讨论】:

    标签: java hibernate postgresql postgresql-9.2


    【解决方案1】:

    这是 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>
    

    【讨论】:

    • 这很好,但是我应该把这个文件放在哪里呢?在 WEB-INF 中?
    • @HrishikeshChoudhari 它应该在你的构建路径中的某个地方。我认为 WEB-INF 会很好。我对 web 项目不太了解,但我觉得 WEB-INF 在构建中路径。
    • org.hibernate.dialect.PostgreSQLDialect 已弃用。你应该改用 org.hibernate.dialect.PostgreSQL82Dialect
    • 对于 9.2 及更高版本,您应该改用org.hibernate.dialect.PostgreSQL92Dialect
    • 这仍然有效吗?
    【解决方案2】:

    这是连接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>
    

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • edit您的帖子并将实际代码/XML 显示为文本而不是屏幕截图。其他人无法从您的图像中复制和粘贴。 See here 了解详情。谢谢。
        猜你喜欢
        • 2012-03-10
        • 1970-01-01
        • 2014-12-05
        • 2020-04-02
        • 2012-10-19
        • 1970-01-01
        • 2019-10-17
        • 1970-01-01
        • 2021-12-07
        相关资源
        最近更新 更多