av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Spring Boot JPA中使用@Entity和@Table的實現

瀏覽:85日期:2023-09-19 10:38:44

本文中我們會講解如何在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:

@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() ); }

如果我們需要修改系統的默認實現,則可以實現接口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”)

到此這篇關于Spring Boot JPA中使用@Entity和@Table的實現的文章就介紹到這了,更多相關Spring Boot JPA使用@Entity和@Table內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 精品国产乱码久久久久久蜜臀网站 | 国产精品一区二区三区四区五区 | 一级片在线视频 | 欧洲av在线 | 激情五月综合 | 精品国产乱码久久久久久蜜臀网站 | 国产伦精品一区二区免费 | 黄色a一级片 | 一级片免费在线观看 | 国产传媒一区二区 | 中文字幕有码在线 | 亚洲黄色大片 | 日韩国产一区 | 国产小视频在线观看 | 成人精品在线观看 | 五月网站| 亚洲福利一区 | 这里只有精品视频在线观看 | 中文在线字幕 | 99re国产精品 | 亚洲免费小视频 | 91黄色大片 | 国产小视频在线观看 | 国产一二三 | 国产人成一区二区三区影院 | 天天综合色 | 丁香六月激情 | 亚洲永久精品视频 | 国产精品aaa | 国产高清免费视频 | 久草资源站| 欧美精品综合 | 成年在线观看 | 国产三区四区 | 日产毛片 | 日韩免费高清 | 三级黄色 | 国产一区二区不卡 | 黄色小视频在线观看免费 | 国产精品自拍第一页 | 国产又色又爽又黄又免费 |