Spring ORM - 持久化 Hibernate



persistence.xml 定義了與 Hibernate 關聯的各種方面,如下所示。

  • 永續性單元 - 包含所有詳細資訊的永續性單元。它用於在 Spring 上下文中獲取引用。

  • 提供程式 - org.hibernate.jpa.HibernatePersistenceProvider 類將用作提供程式。

  • 資料庫 URL 和憑據 - 在屬性部分,我們傳遞與資料庫相關的值。

  • show_sql - 顯示生成的 SQL 查詢

  • hbm2ddl - 允許 Hibernate 建立表。

在 src -> main > resources > META-INF 資料夾中建立 persistence.xml。

persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
   http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">

   <persistence-unit name="Hibernate_JPA">
   <description> Spring Hibernate JPA Configuration Example</description>
   <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
   <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://:3306/tutorialspoint?useSSL=false" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="root@123" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.hbm2ddl.auto" value="create" />
   </properties>
   </persistence-unit>
</persistence>
廣告
© . All rights reserved.