SpringBootJPA中使用@Entity和@Table的實(shí)現(xiàn)

本文中我們會(huì)講解如何在Spring Boot JPA中實(shí)現(xiàn)class和數(shù)據(jù)表格的映射。

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站建設(shè)、甘南網(wǎng)絡(luò)推廣、小程序制作、甘南網(wǎng)絡(luò)營(yíng)銷、甘南企業(yè)策劃、甘南品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供甘南建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn

默認(rèn)實(shí)現(xiàn)

Spring Boot JPA底層是用Hibernate實(shí)現(xiàn)的,默認(rèn)情況下,數(shù)據(jù)庫(kù)表格的名字是相應(yīng)的class名字的首字母大寫。命名的定義是通過接口ImplicitNamingStrategy來(lái)定義的:

  /**
   * 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);

我們看下它的實(shí)現(xiàn)ImplicitNamingStrategyJpaCompliantImpl:

  @Override
  public 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 source
      throw new HibernateException( "Could not determine primary table name for entity" );
    }

    return toIdentifier( tableName, source.getBuildingContext() );
  }

如果我們需要修改系統(tǒng)的默認(rèn)實(shí)現(xiàn),則可以實(shí)現(xiàn)接口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來(lái)自定義映射的表格名字:

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

當(dāng)然,我們可以將整個(gè)名字寫在靜態(tài)變量中:

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

在JPQL Queries中重寫表格名字

通常我們?cè)贎Query中使用JPQL時(shí)可以這樣用:

@Query(“select * from Article”)

其中Article默認(rèn)是Entity類的Class名稱,我們也可以這樣來(lái)修改它:

@Entity(name = "MyArticle")

這時(shí)候我們可以這樣定義JPQL:

@Query(“select * from MyArticle”)

到此這篇關(guān)于Spring Boot JPA中使用@Entity和@Table的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring Boot JPA使用@Entity和@Table內(nèi)容請(qǐng)搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!

網(wǎng)站標(biāo)題:SpringBootJPA中使用@Entity和@Table的實(shí)現(xiàn)
文章轉(zhuǎn)載:http://muchs.cn/article14/gdecde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、App設(shè)計(jì)、服務(wù)器托管做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化