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的更多相关文章

  1. spring boot JPA中实体类常用注解

    spring boot jpa中的注解很多,参数也比较多.没必要全部记住,但是经常查看官方文档也比较麻烦,记录一下一些常用的注解.通过一些具体的例子来帮助记忆. @Entity @Table(name ...

  2. Spring Boot JPA中关联表的使用

    文章目录 添加依赖 构建Entity 构建Repository 构建初始数据 测试 Spring Boot JPA中关联表的使用 本文中,我们会将会通过一个Book和Category的关联关系,来讲解 ...

  3. Spring Boot JPA 中transaction的使用

    文章目录 @Transactional的实现 @Transactional的使用 Transaction的传播级别 REQUIRED SUPPORTS MANDATORY NEVER NOT_SUPP ...

  4. Spring Boot JPA中java 8 的应用

    文章目录 Optional Stream API CompletableFuture Spring Boot JPA中java 8 的应用 上篇文章中我们讲到了如何在Spring Boot中使用JPA ...

  5. Spring Boot JPA的查询语句

    文章目录 准备工作 Containing, Contains, IsContaining 和 Like StartsWith EndsWith 大小写不敏感 Not @Query Spring Boo ...

  6. spring boot jpa 使用update 报错解决办法

    在spring boot jpa 中自定义sql,执行update操作报错解决办法: 在@Query(...)上添加 @Modifying@Transactional注解

  7. Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例

    Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...

  8. Spring Boot JPA 连接数据库

    本文将介绍怎样在Spring Boot project中加入JPA作为持久化方式. 改动 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 改 ...

  9. Spring boot Jpa添加对象字段使用数据库默认值

    Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...

随机推荐

  1. 8.MSFvenom

    Meterpreter 01 Meterpreter API调用 Meterpreter提供了多种APl调用,在编写自己的脚本时可以使用这些API来提供额外功能或定制功能. 关于ruby的更多信息,请 ...

  2. Springboot系列(四)web静态资源配置详解

    Springboot系列(四)web静态资源配置 往期精彩 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 SpringBoot系列(三)配 ...

  3. .NET Core项目部署到Linux(Centos7)(九)防火墙配置,允许外网或局域网访问.NET Core站点

    目录 1.前言 2.环境和软件的准备 3.创建.NET Core API项目 4.VMware Workstation虚拟机及Centos 7安装 5.Centos 7安装.NET Core环境 6. ...

  4. 【Linux】Apache服务配置

    一. URL 统一资源定位符 http://www.sina.com.cn:80/admin/index.html 二. 环境安装 LAMP 源码包编译安装 版本可以自定义 生产环境 安全 稳定 开发 ...

  5. 【Linux】Linux(一)Linux常用命令

    一 命令行提示符 1.[root@localhost ~]# 当前登录用户@主机名:当前所在目录$ # 超级用户 $  普通用户 当前所在目录:~ 用户家目录 管理员 /root 普通用户 /home ...

  6. 深入了解CI/CD:工具、方法、环境、基础架构的全面指南

    本文来自Rancher Labs 持续集成和持续交付(CI/CD)是DevOps背后的助推力之一.如果你的企业正在考虑使用DevOps,那么CI/CD绝对是需要考虑的其中一部分.但是CI/CD到底意味 ...

  7. [转] [知乎] 浅谈Roguelike

    浅谈Roguelike 从柏林诠释说起 在2008年召开的国际Roguelike开发会议上,众多的Roguelike开发者与爱好者共同制定了<柏林诠释>,规定了Roguelike游戏需要具 ...

  8. OSI 七层模型以及TCP/IP模型

    OSI 七层模型 定义 OSI(Open System Interconnection)即开放式系统互联通信参考模型.该模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系,一 ...

  9. Persona & User Scenario

    Persona: Tom:男,21岁,大学生,周末经常和同学们一起出去吃饭.唱歌.打球.郊游,期间会时不时拍一些照片以作纪念,长期积累的照片数量较多且内容繁杂,很少对照片进行整理: Alisa:女,2 ...

  10. Python套接字之UDP

    目录 基于UDP的socket 发送消息 接收消息 基于UDP的socket 面向无连接的不可靠数据传输,可以没有服务器端,只不过没有服务器端,发送的数据会被直接丢弃,并不能到达服务器端 发送消息 在 ...