Spring Boot JPA中使用@Entity和@Table的实现

作者:flydean 时间:2023-11-22 11:49:58 

本文中我们会讲解如何在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”)

来源:https://segmentfault.com/a/1190000021886398

标签:Spring,Boot,JPA,@Entity,@Table
0
投稿

猜你喜欢

  • 用Java实现简单ATM机功能

    2023-05-10 13:36:03
  • Android RadarView雷达图(蜘蛛网图)的实现代码

    2022-12-09 21:45:19
  • java实现拼图游戏

    2022-09-21 11:14:19
  • 详解Java Streams 中的异常处理

    2021-09-03 11:26:11
  • java代码规范review异常事故记录

    2023-08-03 07:23:24
  • java ThreadPoolExecutor线程池拒绝策略避坑

    2021-09-05 08:39:52
  • Java动态数组Arraylist存放自定义数据类型方式

    2023-07-25 10:41:30
  • C# 用什么方法将BitConverter.ToString产生字符串再转换回去

    2021-10-07 17:49:40
  • Java中实现线程间通信的实例教程

    2021-10-12 17:33:30
  • C#制作简单的多人在线即时交流聊天室

    2023-02-23 12:12:23
  • Java实现二叉搜索树的插入、删除功能

    2023-07-15 20:54:53
  • 深入分析JAVA流程控制语句

    2023-11-20 10:48:32
  • Android开发ImageView图片无法显示解决过程

    2023-06-07 21:34:39
  • java实现在性能测试中进行业务验证实例

    2022-10-15 09:50:11
  • C#中WebClient实现文件下载

    2022-10-11 18:04:57
  • Android电量优化提高手机续航

    2022-06-14 11:39:40
  • MyBatis自定义类型转换器实现加解密

    2023-09-10 20:18:00
  • Java下载远程服务器文件到本地(基于http协议和ssh2协议)

    2022-08-29 12:23:18
  • iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

    2023-07-08 16:52:22
  • Spring Boot分离配置文件的多种方式总结

    2021-08-31 14:43:37
  • asp之家 软件编程 m.aspxhome.com