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做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
随机推荐
- 阿里 IOS 面试官教你在面试中脱颖而出
前言: 知己知彼.百战不殆,面试也是如此. 只有充分了解面试官的思路,才能更好地在面试中充分展现自己. 今天,阿里高级技术专家将分享自己作为面试官的心得与体会.如果你是面试者,可以借此为镜,对照发现自 ...
- 剖析手写Vue,你也可以手写一个MVVM框架
剖析手写Vue,你也可以手写一个MVVM框架# 邮箱:563995050@qq.com github: https://github.com/xiaoqiuxiong 作者:肖秋雄(eddy) 温馨提 ...
- HDU6024:Building Shops(简单DP)
Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- C++编译/编辑器对OIer的必要功能
(没有引战的意思,如果有不同意见可以评论区发言,只是写出我目前的情况) 作为一个C++ OIer肯定是用过Dev的,因为学校推荐啊我也没有办法.都知道Dev又丑又没有代码补全,但是却是最最最适合OIe ...
- shell脚本实现自动压缩一天前的日志文件 ,并传到ftp服务器上
shell脚本实现自动压缩一天前的日志文件 ,并传到ftp服务器上 naonao_127关注2人评论19401人阅读2012-06-08 11:26:16 生产环境下脚本自动备份脚本是 ...
- Centos7.x & RedHat7.x系统忘记 root 密码解决办法
重启系统进入引导页面 先将机器重启 根据提示按下e进入内核编辑页面 找到linux16参数行,并在行尾加上rd.break,之后按下Ctrl+X重启 如上图所示,重启之后将进入救援模式. 这是依次输入 ...
- C语言数据结构无向图
#include<stdio.h>#include<stdlib.h>#define num 8struct nearnode{ int order; nearnode* pn ...
- C语言学生管理系统(C语言课程设计/精简版)
#include<stdio.h>#include<stdlib.h>#include<windows.h>#include<conio.h>typed ...
- 第一天总结(while计数器+成绩大小+获取时间+猜拳大小)
#*_* coding:utf-8 *_*# while 先有一个计数器 input = 0# input = input('输入数字')while input < 5: input= inpu ...
- 一个不错的spring 学习博客
http://www.iteye.com/blogs/subjects/spring-tittle-tattle