将 PostgreSQL 9.2.1 与 Hibernate 连接起来

     2023-02-26     187

关键词:

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

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

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

这里有什么帮助吗?

【问题讨论】:

【参考方案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 了解详情。谢谢。

为啥 PostgreSQL 9.2.1 可以存储大于 2GB 的大对象?

】为啥PostgreSQL9.2.1可以存储大于2GB的大对象?【英文标题】:HowcomePostgreSQL9.2.1canstoreLargeObjectbiggerthan2GB?为什么PostgreSQL9.2.1可以存储大于2GB的大对象?【发布时间】:2013-06-1108:27:25【问题描述】:我对PostgreSQL和数据库完全陌生,... 查看详情

Postgresql 9.2.1 在独立后端模式下完全真空后无法初始化

】Postgresql9.2.1在独立后端模式下完全真空后无法初始化【英文标题】:Postgresql9.2.1failedtoinitializeafterfullvacuuminstandalonebackendmode【发布时间】:2017-10-2316:07:01【问题描述】:在postgresql9.2.1版本中,数据库不接受任何命令以避免wraparo... 查看详情

linux怎么安装postgresql-9.2.1-1-linux

...yum具体怎么操作,但我想应该与apt的功能差不多。首先,postgresql是很有名的数据库,你用不着指定具体的版本。你可以直接用yum去查询源或者库里是否有这个软件(具体操作请参考yum的文档,我这边是apt-cachesearchpostgresql)当然... 查看详情

greenplum实时数据仓库实践——greenplum监控与运维(代码片段)

目录9.1权限与角色管理9.1.1Greenplum中的角色与权限9.1.2管理角色及其成员9.1.3管理对象权限9.1.4口令加密9.2数据导入导出9.2.1file协议及其外部表9.2.2gpfdist协议及其外部表9.2.3基于Web的外部表9.2.4外部表错误处理9.2.5使用gpload导入数据9... 查看详情

PostgreSQL 中 JSON 数据类型的大小限制

】PostgreSQL中JSON数据类型的大小限制【英文标题】:SizelimitofJSONdatatypeinPostgreSQL【发布时间】:2012-09-2802:37:44【问题描述】:有人知道PostgreSQL9.2中JSON数据类型的大小有什么限制吗?【问题讨论】:【参考方案1】:查看PostgreSQL9.2.... 查看详情

将 Magento 1.8 升级到 1.9.2.1

】将Magento1.8升级到1.9.2.1【英文标题】:UpgradingMagento1.8To1.9.2.1【发布时间】:2020-05-0209:18:03【问题描述】:有机会使用Magento2进行新构建,应用程序的架构发生了显着变化,对我来说,这无疑增加了启动和运行Magento项目的难度,... 查看详情

无法将 postgresql 与 django 连接

】无法将postgresql与django连接【英文标题】:Notabletoconnectpostgresqlwithdjango【发布时间】:2015-02-2818:54:49【问题描述】:我在postgresql中创建了一个名为testdb1的数据库,我正在尝试将它与我的django应用程序连接起来。但是当我运行pyt... 查看详情

将 DialogflowChatbot 与 PostgreSQL 数据库连接

】将DialogflowChatbot与PostgreSQL数据库连接【英文标题】:ConnectDialogflowChatbotwithPostgreSQLdatabase【发布时间】:2019-05-0807:42:59【问题描述】:我想将Dialogflow中的自定义聊天机器人与PostgreSQL数据库连接起来。场景是用户将他/她的查询... 查看详情

将 Django 和 Postgresql 与 Docker 连接起来

】将Django和Postgresql与Docker连接起来【英文标题】:LinkingDjangoandPostgresqlwithDocker【发布时间】:2014-12-0508:46:59【问题描述】:我有两个Docker容器。第一个是Postgresql容器,我使用以下命令运行它。sudodockerrun-v/home/mpmsp/project/ezdict/post... 查看详情

如何将postgresql与jmeter连接起来

】如何将postgresql与jmeter连接起来【英文标题】:Howtoconnectpostgresxlwithjmeter【发布时间】:2019-04-1912:49:25【问题描述】:我正在尝试将我的postgresXL数据库与jmeter连接,我在哪里可以找到postgresXljdbc驱动程序以及如何将其连接到jmeter... 查看详情

将 postgresql 与 sqlalchemy 连接起来

】将postgresql与sqlalchemy连接起来【英文标题】:Connectingpostgresqlwithsqlalchemy【发布时间】:2012-03-1008:41:40【问题描述】:我知道这可能是一个非常简单的问题,但我不知道解决方案。当我尝试连接到postgresql时,这里发生了什么?... 查看详情

无法将 postgreSQL 与 psycopg2 连接

】无法将postgreSQL与psycopg2连接【英文标题】:Can\'tconnectthepostgreSQLwithpsycopg2【发布时间】:2011-07-2621:35:01【问题描述】:第一次找不到一些技术问题的答案这是我的问题:>>conn=psycopg2.connect(database="mydb",user="postgres",password="123"... 查看详情

将远程 Postgresql 与 AWS for Rails 应用程序一起使用

】将远程Postgresql与AWSforRails应用程序一起使用【英文标题】:UsingaremotePostgresqlwithAWSforRailsapp【发布时间】:2016-05-0106:49:27【问题描述】:我在另一台服务器上使用远程PostgreSQL,并希望将Rails应用程序部署到AWS。我希望AWS与那个... 查看详情

Docker/Hibernate/PostgreSQL - 将运行 Hibernate/SpringBoot 应用程序的容器与 postgreSQL 容器不工作连接

】Docker/Hibernate/PostgreSQL-将运行Hibernate/SpringBoot应用程序的容器与postgreSQL容器不工作连接【英文标题】:Docker/Hibernate/PostgreSQL-ConnectingcontainerrunningHibernate/SpringBootappwithapostgreSQLcontainernotworking【发布时间】:2018-08-2707:32:56 查看详情

如何将 postgresql 与 javascript 一起使用? [关闭]

】如何将postgresql与javascript一起使用?[关闭]【英文标题】:howusepostgresqlwithjavascript?[closed]【发布时间】:2016-05-2715:56:54【问题描述】:我是JavaScript新手,我想创建一个在数据库中存储数据的页面,所以请帮助我并建议我阅读有... 查看详情

将 symfony2 应用程序与 mysql 和 postgresql 一起使用

】将symfony2应用程序与mysql和postgresql一起使用【英文标题】:Usesymfony2applicationwithmysql&postgresql【发布时间】:2015-12-2011:42:10【问题描述】:我用symfony2框架开发了一个应用程序。该应用程序需要在不同的服务器上运行,一台使... 查看详情

是否可以将 Debezium 与不在 docker 中的 Postgresql 连接起来?

】是否可以将Debezium与不在docker中的Postgresql连接起来?【英文标题】:IsitpossibletoconnectDebeziumwithPostgresqlwhichisnotindocker?【发布时间】:2020-10-2213:53:29【问题描述】:目前我在我的WSL2环境中使用PostgreSQL12,我希望用debezium和kafka实... 查看详情

将 Spring Data 与 PostgreSQL JsonB 类型属性一起使用

】将SpringData与PostgreSQLJsonB类型属性一起使用【英文标题】:UsingSpringDatawithPostgreSQLJsonBtypeproperties【发布时间】:2021-07-2721:27:08【问题描述】:我很难在SpringData中使用带有JsonB数据类型的PostgreSQL。我有下表:createtablemytable(...MYDATA... 查看详情