日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot JPA中使用@Entity和@Table

發布時間:2024/2/28 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot JPA中使用@Entity和@Table 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 默認實現
    • 使用@Table自定義表格名字
    • 在JPQL Queries中重寫表格名字

Spring Boot JPA中使用@Entity和@Table

本文中我們會講解如何在Spring Boot JPA中實現class和數據表格的映射。

默認實現

Spring Boot JPA底層是用Hibernate實現的,默認情況下,數據庫表格的名字是相應的class名字的首字母大寫。命名的定義是通過接口ImplicitNamingStrategy來定義的:

/*** Determine the implicit name of an entity's primary table.** @param source The source information** @return The implicit table name.*/public Identifier determinePrimaryTableName(ImplicitEntityNameSource source);

我們看下它的實現ImplicitNamingStrategyJpaCompliantImpl:

@Overridepublic Identifier determinePrimaryTableName(ImplicitEntityNameSource source) {if ( source == null ) {// should never happen, but to be defensive...throw new HibernateException( "Entity naming information was not provided." );}String tableName = transformEntityName( source.getEntityNaming() );if ( tableName == null ) {// todo : add info to error message - but how to know what to write since we failed to interpret the naming sourcethrow new HibernateException( "Could not determine primary table name for entity" );}return toIdentifier( tableName, source.getBuildingContext() );}

如果我們需要修改系統的默認實現,則可以實現接口PhysicalNamingStrategy:

public interface PhysicalNamingStrategy {public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment);public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment);public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment);public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment);public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment); }

使用@Table自定義表格名字

我們可以在@Entity中使用@Table來自定義映射的表格名字:

@Entity @Table(name = "ARTICLES") public class Article {// ... }

當然,我們可以將整個名字寫在靜態變量中:

@Entity @Table(name = Article.TABLE_NAME) public class Article {public static final String TABLE_NAME= "ARTICLES";// ... }

在JPQL Queries中重寫表格名字

通常我們在@Query中使用JPQL時可以這樣用:

@Query(“select * from Article”)

其中Article默認是Entity類的Class名稱,我們也可以這樣來修改它:

@Entity(name = "MyArticle")

這時候我們可以這樣定義JPQL:

@Query(“select * from MyArticle”)

更多精彩內容且看:

  • 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
  • Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
  • Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
  • java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程

更多教程請參考 flydean的博客

總結

以上是生活随笔為你收集整理的Spring Boot JPA中使用@Entity和@Table的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。