Spring Boot JPA中使用@Entity和@Table

简介: 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”)
相关文章
|
7月前
|
Java API 数据库
JPA简介:Spring Boot环境下的实践指南
上述内容仅是JPA在Spring Boot环境下使用的冰山一角,实际的实践中你会发现更深更广的应用。总而言之,只要掌握了JPA的规则,你就可以借助Spring Boot无比丰富的功能,娴熟地驾驶这台高性能的跑车,在属于你的程序世界里驰骋。
282 15
|
12月前
|
XML Java 应用服务中间件
Spring Boot 两种部署到服务器的方式
本文介绍了Spring Boot项目的两种部署方式:jar包和war包。Jar包方式使用内置Tomcat,只需配置JDK 1.8及以上环境,通过`nohup java -jar`命令后台运行,并开放服务器端口即可访问。War包则需将项目打包后放入外部Tomcat的webapps目录,修改启动类继承`SpringBootServletInitializer`并调整pom.xml中的打包类型为war,最后启动Tomcat访问应用。两者各有优劣,jar包更简单便捷,而war包适合传统部署场景。需要注意的是,war包部署时,内置Tomcat的端口配置不会生效。
2842 17
Spring Boot 两种部署到服务器的方式
|
9月前
|
SQL Java 编译器
深入理解 Spring Data JPA 的导入与使用:以 UserRepository为例
本文深入解析了 Spring Data JPA 中 `UserRepository` 的导入与使用。通过示例代码,详细说明了为何需要导入 `User` 实体类、`JpaRepository` 接口及 `@Repository` 注解。这些导入语句分别用于定义操作实体、提供数据库交互方法和标识数据访问组件。文章还探讨了未导入时的编译问题,并展示了实际应用场景,如用户保存、查询与删除操作。合理使用导入语句,可让代码更简洁高效,充分发挥 Spring Data JPA 的优势。
554 0
|
10月前
|
Java 数据库 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
418 0
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
620 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
存储 运维 安全
Spring运维之boot项目多环境(yaml 多文件 proerties)及分组管理与开发控制
通过以上措施,可以保证Spring Boot项目的配置管理在专业水准上,并且易于维护和管理,符合搜索引擎收录标准。
847 2
|
SQL Java 关系型数据库
Springboot引入jpa来管理数据库
Springboot引入jpa来管理数据库
295 0
Springboot引入jpa来管理数据库
|
缓存 NoSQL Java
Springboot自定义注解+aop实现redis自动清除缓存功能
通过上述步骤,我们不仅实现了一个高度灵活的缓存管理机制,还保证了代码的整洁与可维护性。自定义注解与AOP的结合,让缓存清除逻辑与业务逻辑分离,便于未来的扩展和修改。这种设计模式非常适合需要频繁更新缓存的应用场景,大大提高了开发效率和系统的响应速度。
570 2
|
Java 数据库连接 API
【Java笔记+踩坑】Spring Data JPA
从常用注解、实体类和各层编写方法入手,详细介绍JPA框架在增删改查等方面的基本用法,以及填充用户名日期、分页查询等高级用法。
【Java笔记+踩坑】Spring Data JPA
|
SQL Java 数据库连接
springBoot+Jpa(hibernate)数据库基本操作
springBoot+Jpa(hibernate)数据库基本操作
375 0