将PostgreSQL 9.2.1与Hibernate连接起来

Hri*_*ari 74 java postgresql hibernate postgresql-9.2

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

我正在简要介绍如何将PostgreSQL与Hibernate连接的完整教程.

这里有什么帮助?

San*_*age 134

这是posgresql的hibernate.cfg.xml,它将帮助你完成posgresql的基本hibernate配置.

<!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>
Run Code Online (Sandbox Code Playgroud)

  • org.hibernate.dialect.PostgreSQLDialect已弃用.你应该使用org.hibernate.dialect.PostgreSQL82Dialect代替 (11认同)
  • 对于9.2及更高版本,您应该使用`org.hibernate.dialect.PostgreSQL92Dialect`代替. (6认同)
  • @HrishikeshChoudhari它应该在你的构建路径中的某个地方。我认为WEB-INF会很好。我对Web项目没有太多了解,但我觉得WEB-INF在构建路径中。 (2认同)

小智 6

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


小智 6

这是用于连接 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>
Run Code Online (Sandbox Code Playgroud)

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