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相关推荐
- Spring Boot JPA中关联表的使用
文章目录 添加依赖 构建Entity 构建Repository 构建初始数据 测试 Spring Boot JPA中关联表的使用 本文中,我们会将会通过一个Book和Category的关联关系,来讲解 ...
- Spring Boot JPA 中transaction的使用
文章目录 @Transactional的实现 @Transactional的使用 Transaction的传播级别 REQUIRED SUPPORTS MANDATORY NEVER NOT_SUPP ...
- Spring Boot JPA中java 8 的应用
文章目录 Optional Stream API CompletableFuture Spring Boot JPA中java 8 的应用 上篇文章中我们讲到了如何在Spring Boot中使用JPA ...
- Spring Boot JPA的查询语句
文章目录 准备工作 Containing, Contains, IsContaining 和 Like StartsWith EndsWith 大小写不敏感 Not @Query Spring Boo ...
- 在Spring Boot测试中使用Testcontainer进行数据库集成测试
在此博客文章中,我想演示如何在Spring Boot测试中集成Testcontainer以便与数据库一起运行集成测试. 我没有使用Testcontainers的Spring Boot模块. 如何与他们 ...
- 芋道 Spring Boot JPA 入门(一)之快速入门
点击上方"芋道源码",选择"设为星标" 做积极的人,而不是积极废人! 源码精品专栏 原创 | Java 2019 超神之路,很肝~ 中文详细注释的开源项目 RP ...
- Spring Boot JPA实体类idea自动生成 其一-https://www.jianshu.com/p/44bb7e25f5c7
Spring Boot JPA实体类idea自动生成 其一 marioplus12 2018.09.17 19:29* 字数 138 阅读 762评论 0喜欢 2 view -> Tool Wi ...
- Spring Boot JPA 2.7.2
icon: edit date: 2022-01-02 category: CategoryA tag: tag A tag B star: true Spring Boot JPA 2.7.2 项目 ...
- Spring Boot Jpa 配置多个数据源,并读取其中一个表的具体数据
总体简介: Spring Boot Jpa配置多个数据源(此次两个mysql数据库),访问其中一个库 alime_counsel_assign_log下的assign_data_backflow表,实 ...
最新文章
- Docker 公司是如何做社区的?
- 编程字典keras.layers API方法
- 基本系统部署完成!北斗三号闪耀中国智慧
- hive 调优(一)coding调优
- SE11 仅以有限的方式支持生成的 DDL SQL 视图
- 袁亚湘:愿为数学做更多
- k8---proxy
- 深度学习《CNN架构续篇 - 梯度下降的优化》
- WebForms VS. MVC(翻译)
- C#序列化与反序列化方式简单总结
- IDEA将maven项目复制成一个新的框架/项目
- FastReport浏览器直接打印无须预览(2023终版)
- 计算机上的策略禁止用户安装,Win7系统如何使用组策略禁止安装软件?
- Android Volley 之自定义Request
- 谨以此文,献给我的大学四年—双非本科到双一流985的成长之路
- 分享几款UI设计师快速提升工作效率的辅助设计软件
- Visual Studio 2019背景美化(背景透明化+自定义背景图片)
- ABP理论学习之本地化
- NFS服务器搭建指南
- Allegro 异形焊盘的制作方法
热门文章
- 机器学习-分类之多层感知机原理及实战
- python爬虫-利用代理ip访问网页(urllib)
- QT乱码总结6.编码测试和总结一
- 逆向工程核心原理学习笔记(十):IA-32寄存器基本讲解
- 使用CArchive类进行序列化
- 边缘计算、区块链、5G,哪个能走的更远
- 高级数据结构与算法 | 回溯算法(Back Tracking Method)
- C++ 泛型编程(二):非类型模板参数,模板特化,模板的分离编译
- 使用drawio进行画图真的很方便(WEB版/Chrome APP版/桌面版)
- Mark一下 | 当当优惠码,实付满150减30 | + 荐书