mysql表名大写,造成jpa Table doesn't exist问题

本文介绍了一个关于Spring JPA应用中遇到的表未找到的问题及其解决方法。问题表现为Hibernate无法找到数据库中的表,原因是表名大小写不匹配。通过调整Spring JPA配置中的物理命名策略解决了该问题。

报错信息

org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet


Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table XXX doesn't exist

报错原因调查

mysql里实际表名都是大写

尝试在entity上标记表名大写,实际测试无效,仍然找不到表

@Entity
@Data
@Table(name = "T_BASE_ORDER_PRINT_CLIENT")
public class MerchantPrinter {

    @Id
    @Column(name = "print_client_id")
    private String id;

    @Column(name = "merchant_id")
    private String merchantId;

    private String imei;

}

最终的解决方案

在spring jpa配置中增加physical-strategy配置解决问题

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://XXX:3306/dbname
    username: root
    password: *******
    schema:
  jpa:
    hibernate:
      ddl-auto: none
      naming:
        physical-strategy:  org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    show-sql: true

physical naming strategy :used to convert a “logical name” (either implicit or explicit) name of a table or column into a physical name (e.g. following corporate naming guidelines)
physical naming strategy:物理命名策略,用于转换“逻辑名称”(隐式或显式)的表或列成一个物理名称

这个错误提示 `Table 'mysql.book' doesn't exist` 说明你的程序试图访问数据库中的 `book`,但该并不存在。这个错误通常出现在使用 Java + Spring Boot + JPA 构建的 Web 应用中,尤其是在连接到 MySQL 数据库时。 --- ### ✅ 原因分析 1. **实体类与数据库不匹配**:Spring Boot 默认将实体类(如 `Book`)转为小写作为(如 `book`),如果你数据库中没有为 `book` 的,就会报错。 2. **数据库未正确初始化**:你可能没有创建相应的数据库和结构。 3. **配置文件指向了错误的数据库**:比如配置的是 `mysql` 数据库而不是你自己创建的图书馆系统数据库(如 `library_system`)。 --- ### ✅ 解决方案 #### 1. 确保 `application.properties` 中配置的数据库是正确的 ```properties spring.datasource.url=jdbc:mysql://localhost:3306/library_system?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=your_password spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect ``` > 注意:确保数据库是 `library_system` 而不是 `mysql`。 --- #### 2. 使用 `@Table(name = "books")` 明确指定 因为你在 SQL 中创建的是 `books`,而默认 JPA 会去找 `book` 。所以需要在实体类上加注解: ```java import javax.persistence.*; @Entity @Table(name = "books") // 明确指定对应数据库 public class Book { @Id private String isbn; private String title; private String author; private String publisher; private String description; // Getters and Setters } ``` --- #### 3. 如果你想让 JPA 自动帮你建,可以设置: ```properties spring.jpa.hibernate.ddl-auto=create-or-restore ``` 这将在应用启动时自动根据实体类生成结构。 --- #### 4. 检查数据库是否存在 你可以手动执行以下 SQL 创建数据库和: ```sql CREATE DATABASE IF NOT EXISTS library_system; USE library_system; -- 创建书籍 CREATE TABLE books ( isbn VARCHAR(13) PRIMARY KEY, title VARCHAR(255), author VARCHAR(255), publisher VARCHAR(255), description TEXT ); ``` --- ### ✅ 示例完整实体类 + Repository ```java @Entity @Table(name = "books") public class Book { @Id private String isbn; private String title; private String author; private String publisher; private String description; // getters and setters } ``` ```java public interface BookRepository extends JpaRepository<Book, String> { List<Book> findByAuthor(String author); List<Book> findByTitleContaining(String title); } ``` --- ### ✅ 测试是否成功 你可以添加一个简单的测试接口来验证数据库是否连接成功: ```java @RestController @RequestMapping("/test") public class TestController { @Autowired private BookRepository bookRepository; @GetMapping public List<Book> getAllBooks() { return bookRepository.findAll(); } } ``` 如果返回空列说明连接成功;否则继续检查数据库连接信息或是否存在。 --- ###
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值