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

 更新时间:2020年03月02日 09:29:09   作者:flydean  
这篇文章主要介绍了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”)

到此这篇关于Spring Boot JPA中使用@Entity和@Table的实现的文章就介绍到这了,更多相关Spring Boot JPA使用@Entity和@Table内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Netty中的心跳检测机制详解

    Netty中的心跳检测机制详解

    这篇文章主要介绍了Netty中的心跳检测机制详解,Netty 是 基于 TCP 协议开发的,在四层协议 TCP 协议的实现中也提供了 keepalive 报文用来探测对端是否可用,TCP 层将在定时时间到后发送相应的 KeepAlive 探针以确定连接可用性,需要的朋友可以参考下
    2023-12-12
  • Java拷贝文件夹和删除文件夹代码实例

    Java拷贝文件夹和删除文件夹代码实例

    这篇文章主要介绍了Java拷贝文件夹和删除文件夹代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • springboot实现用户名查找用户功能

    springboot实现用户名查找用户功能

    本文主要介绍了springboot实现用户名查找用户功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • Redis缓存实例分步详解

    Redis缓存实例分步详解

    实际开发中缓存处理是必须的,不可能我们每次客户端去请求一次服务器,服务器每次都要去数据库中进行查找,为什么要使用缓存?说到底是为了提高系统的运行速度
    2023-04-04
  • SpringBoot自定义starter启动器的实现思路

    SpringBoot自定义starter启动器的实现思路

    这篇文章主要介绍了SpringBoot如何自定义starter启动器,通过starter的自定义过程,能够加深大家对SpringBoot自动配置原理的理解,需要的朋友可以参考下
    2022-10-10
  • 使用@Autowired注解有错误提示的解决

    使用@Autowired注解有错误提示的解决

    这篇文章主要介绍了使用@Autowired注解有错误提示的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 深入理解Java抽象类

    深入理解Java抽象类

    这篇文章主要介绍了Java抽象类的相关资料,帮助大家更好的理解和学习Java,感兴趣的朋友可以了解下
    2020-08-08
  • Spring Boot 控制层之参数传递方法详解

    Spring Boot 控制层之参数传递方法详解

    这篇文章主要介绍了Spring Boot 控制层之参数传递方法详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • SpringBoot @RequestParam、@PathVaribale、@RequestBody实战案例

    SpringBoot @RequestParam、@PathVaribale、@RequestBody实战案例

    这篇文章主要介绍了SpringBoot @RequestParam、@PathVaribale、@RequestBody实战案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • IDEA实现JDBC的操作步骤

    IDEA实现JDBC的操作步骤

    JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,本文给大家介绍IDEA实现JDBC的操作步骤,感兴趣的朋友一起看看吧
    2022-01-01

最新评论