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. A 【NOIP2012 day2】疫情控制

    时间限制 : 20000 MS   空间限制 : 128000 KB 评测说明 : 2s,128m 问题描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是 ...

  2. 关于C#三层架构增删改查中的“登录”问题

    先来一个界面: DAO中的方法: 实现代码如下: 这里需要特别注意的是一个“安全性”的考虑: 当登入成功时,把登入时输入的用户名赋值到Session,然后在后面的页面进行判断--此时Session保留 ...

  3. 安装elasticsearch-head(源码安装方式)

    gitHub 地址 https://github.com/mobz/elasticsearch-head 克隆到本地 进行npm 安装运行 git clone git://github.com/mob ...

  4. binlog的作用及与redo log的区别

    区别 二进制日志(bin log)会记录所有与MySQL数据库有关的日志记录,包括InnoDB.MyISAM.Heap等其他存储引擎的日志.而InnoDB存储引擎的重做日志只记录有关该存储引擎本身的事 ...

  5. python--匿名函数、文件操作

    一.匿名函数 语法: sum = lambda arg1, arg2: arg1 + arg2 #调用sum函数 print "Value of total : ", sum( 1 ...

  6. 【Web】阿里icon图标webpack插件(webpack-qc-iconfont-plugin)详解

    webpack-qc-iconfont-plugin webpack-qc-iconfont-plugin是一个webpack插件,可以轻松地帮你将阿里icon的图标项目下载至本地 开发初衷 之前已经 ...

  7. 中阶 d05 tomcat 安装 eclipse上配置tomcat

    eclipse使用参考 https://www.bilibili.com/video/av49438855/?p=24 1. 直接解压 ,然后找到bin/startup.bat 2. 可以安装 启动之 ...

  8. python调用js

    安装 pip install PyExecJS 方法 eval() 输入参数:source(JS语句).cwd(路径) 返回值:result(语句执行结果) compile() 输入参数:source ...

  9. Java基础】并发 - 多线程

    Java基础]并发 - 多线程 分类: Java2014-05-03 23:56 275人阅读 评论(0) 收藏 举报 Java   目录(?)[+]   介绍 Java多线程 多线程任务执行 大多数 ...

  10. 2020年iOS进阶面试题总结(一)

    准备找工作的你,可以看看,复习复习!! 1.说一下OC的反射机制 在动态运行下我们可以构建任何一个类,然后我们通过这个类知道这个类的所有的属性和方法,并且如果我们创建一个对象,我们也可以通过对象找到这 ...