Spring Boot JPA中使用@Entity和@Table
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:
@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”)
更多教程请参考 flydean的博客
Spring Boot JPA中使用@Entity和@Table的更多相关文章
- spring boot JPA中实体类常用注解
spring boot jpa中的注解很多,参数也比较多.没必要全部记住,但是经常查看官方文档也比较麻烦,记录一下一些常用的注解.通过一些具体的例子来帮助记忆. @Entity @Table(name ...
- 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 jpa 使用update 报错解决办法
在spring boot jpa 中自定义sql,执行update操作报错解决办法: 在@Query(...)上添加 @Modifying@Transactional注解
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...
- Spring Boot JPA 连接数据库
本文将介绍怎样在Spring Boot project中加入JPA作为持久化方式. 改动 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 改 ...
- Spring boot Jpa添加对象字段使用数据库默认值
Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
随机推荐
- Maximum splitting
Maximum splitting You are given several queries. In the i-th query you are given a single positive i ...
- [原创] 关于步科eview人机界面HMI的使用 - HMI做Slave - Modbus RS485通讯
做测试设备,或者自动化设备常常用到HMI 触摸屏 我有个案子用到了 步科的eview 触摸屏 型号 ET070 我的是单片机主板 控制 HMI显示,通讯用485 MODBUS 单片机板充当 主控 , ...
- python的字符串、列表、字典和函数
一.字符串 在python中字符串无需通过像php中的explode或者javascript中的split进行分解即可完成切片,可以直接通过下标获取字符串中的每一个字符,下标从0开始,如果从厚望签署, ...
- docker 私有仓库 删除镜像
1.查找官方删除法 https://github.com/burnettk/delete-docker-registry-image 2.民用删除法 https://segmentfault.com/ ...
- Python 1基础语法三(变量和标识符的区别)
一.字面量: 就是一个一个的值,如1.2.3.‘world’,就是它自己本身表达的字面值.字面意思,在程序中可以直接使用. 二.变量(variable): 可以用来保存字面量,变量本身没有任何意思:如 ...
- matplotlib TransformedBbox 和 LockableBbox
TransformedBbox 和 LockableBbox 都是BboxBase的子类.TransformedBbox支持使用变换来初始化bbox, LockableBbox可实现锁定bbox的边不 ...
- cxx signal信号捕获
kill -9 [pid] 该信号不能被捕获 #include <iostream> #include <csignal> static void vSignalHandler ...
- AJ学IOS(14)UI之UITableView扩充_表格的修改_(增删移动)
AJ分享,必须精品 先看效果图 代码 // // Created by apple on 14-8-19. // Copyright (c) 2014年 itcast. All rights rese ...
- Pormetheus(一)
(1)Prometheus由来普罗米修斯的灵感来自于谷歌的Borgmon.它最初是由马特·t·普劳德(Matt T. Proud)作为一个研究项目开发的,普劳德曾是谷歌(google)的一名雇员.在普 ...
- 代码质量管理 SonarQube 系列之 安装
简介 SonarQube 是一个开源的代码质量管理系统. 功能介绍: 15种语言的静态代码分析 Java.JavaScript.C#.TypeScript.Kotlin.Ruby.Go.Scala.F ...