將 Spanner 與 Hibernate ORM (PostgreSQL 方言) 整合

Hibernate 是 Java 程式設計語言的物件關聯對應工具。 這個架構可將物件導向網域模型對應至關聯式資料庫。

您可以使用開放原始碼的 PostgreSQL JDBC 驅動程式,將 PostgreSQL 方言資料庫與 Hibernate 整合。PostgreSQL 方言資料庫支援 Hibernate ORM 6.3

設定 PGAdapter

請確認 PGAdapter 與使用 Hibernate 的應用程式是否在同一部電腦上執行。

詳情請參閱「啟動 PGAdapter」。

使用 PostgreSQL 設定 Hibernate

在專案中,為 Hibernate ORM 核心和 PostgreSQL JDBC 驅動程式新增 Apache Maven 依附元件。

<!-- Hibernate core dependency -->
<dependency>
  <groupId>org.hibernate.orm</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>6.3.1.Final</version>
</dependency>

<!-- Postgresql JDBC driver dependency -->
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.7.1</version>
</dependency>

設定 Hibernate 屬性

設定 hibernate.properties,使用 PostgreSQL 方言和 PostgreSQL JDBC 驅動程式。

hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class=org.postgresql.Driver

hibernate.connection.url=jdbc:postgresql://localhost:5432/test-database
hibernate.connection.username=pratick

hibernate.connection.pool_size=5

hibernate.show_sql=true
hibernate.format_sql=true

# hibernate.hbm2ddl.auto validate
hibernate.hbm2ddl.auto=update

使用 Hibernate

如要進一步瞭解整合 Hibernate 與 PostgreSQL 方言資料庫的功能和建議,請參閱 GitHub 上的參考說明文件

後續步驟