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. [leetcode] 树(Ⅱ)

    All questions are simple level. Construct String from Binary Tree Question[606]:You need to construc ...

  2. MFC之使用blat发送邮件

    blat的下载地址:http://www.blat.net 我用它进行了smtp服务的邮件发送.这里我使用的qq邮箱,qq邮箱使用的密码是授权码,可以再qq邮箱设置里面开启smtp服务.下载下来是文件 ...

  3. Spring 中使用 WebSocket 笔记

    编写 WebSocket 消息处理类,比较简单的方式就是直接继承AbstractWebSocketHandler,并覆写其中的处理方法,下面为一个简单的 demo public class WebSo ...

  4. 痞子衡嵌入式:走进二维码(QR Code)的世界(1)- 引言

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是走进二维码(QR Code)的世界专题的引言. 如今二维码可以说是深入走进大家的生活了,推送名片.扫码支付都离不开它,大家几乎每天都会和 ...

  5. ubuntu上安装lamp环境命令清单

    #install configuration manager sudo apt-get install tasksel #install basic lamp stack sudo tasksel i ...

  6. 微信小程序(黑马优购)完成!!{10天}

    微信小程序(黑马优购)完成!!{10天} 项目技术点: wxml, json, wxss, js, mina框架 附上视频链接添加链接描述

  7. mongodb 指令

    db.xxx.stats() 查看表的大小 db.xxx.remove({'endtime':{'$lte':ISODate('2018-10-01')}}) 删除小于等于固定时间的数据. db.us ...

  8. 2017蓝桥杯购物单(C++B组)

    原题: 标题: 购物单 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞.这不,XX大促销又来了!老板夫人开出了长长的购物单,都是 ...

  9. "二号标题"组件:<h2> —— 快应用组件库H-UI

     <import name="h2" src="../Common/ui/h-ui/text/c_h2"></import> < ...

  10. 使用SVG内置API计算图形或点经过transform之后的新坐标

    一个应用场景是,点击一条路径,显示该路径的控制点.因为有transform变形( 平移.缩放.倾斜.旋转等变换),所以获取变形后的新坐标需要计算. 纯数学的方法,就是用2D变换矩阵的一些公式去运算,过 ...