java - spring boot中@Table注解添加表名后出现 "Table <table_name>"不存在异常如何解决

标签 java mysql spring-boot

我正在尝试将 MySql 服务器连接到我的 springboot 项目。我有一个名为product_cateogories 的表,我正在尝试将其与实体productCategory 连接,如下所示:

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity(name = "product_categories")
@Table(name = "product_categories", schema =<schema>)
public class ProductCategory implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
private long categoryId;

@Column(name = "category_name", nullable = false)
private String categoryName;

@Column(name = "category_parent_id")
private long categoryParentId;

@Column(name = "category_level")
private int categoryLevel;
@Column(name = "category_status")
private int categoryStatus;

}

和仓库接口(interface)

@Repository(value = "productrepo")
public interface ProductCategoryRepository extends 
JpaRepository<ProductCategory, Long> {
void deleteByCategoryId(long categoryId);
}

我的数据库配置

导入javax.sql.DataSource;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import 
 org.springframework.boot.context.properties.ConfigurationProperties;  
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import 
 org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import 
  org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories(basePackages = 
"com.b2bdaddy.adminproject.repository")
@EntityScan(basePackages = {"com.b2bdaddy.adminproject.entities"})
@EnableTransactionManagement
public class DBConfig {

@Bean
@ConfigurationProperties(prefix = "spring.dbb2bdaddy")
public DataSource createProductCategoryServiceDataBase() {
    return DataSourceBuilder.create().build();
}

 }

该项目启动正常,没有任何问题,但每当我尝试访问数据时,我都会收到以下错误。

    java.sql.SQLSyntaxErrorException: Table '<schema>.product_category' doesn't exist
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~ 
    [mysql-connector-java-8.0.17.jar:8.0.17]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) 
    ~[mysql-connector-java-8.0.17.jar:8.0.17]
    at

还有这个错误。

   com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException 
(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.17.jar:8.0.17]
  Bad SQL grammar [SELECT product_category.category_id AS category_id, 
  product_category.category_name AS category_name, 
  product_category.category_parent_id AS category_parent_id, 
  product_category.category_level AS category_level, 
  product_category.category_status AS category_status FROM  
  product_category]; 
  nested exception is java.sql.SQLSyntaxErrorException: Table 
  '<schema>.product_category' doesn't exist

即使我添加了@Entity和@Table以及表名,我仍然得到product_category而不是product_categories。

创建表的语句:

 CREATE TABLE `product_categories` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(300) DEFAULT NULL,
`category_parent_id` int(11) DEFAULT NULL,
`category_level` int(11) DEFAULT '1',
`category_status` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE 
 CURRENT_TIMESTAMP,
  PRIMARY KEY (`category_id`),
  KEY `category_Status_fk_idx` (`category_status`),
   CONSTRAINT `category_status_fk` FOREIGN KEY (`category_status`) 
   REFERENCES 
   `status` (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 
   COLLATE=utf8mb4_0900_ai_ci;

最佳答案

您应该检查数据库中列名称的大小写敏感性

关于java - spring boot中@Table注解添加表名后出现 "Table <table_name>"不存在异常如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58541613/

相关文章:

php - 从 SQL 查询中选择要加入的表名

python - 加快 MySQL 更新/插入语句

java - 如何在 Spring Boot 中重写InternalResourceViewResolver?

java - 当前没有 session 绑定(bind)到执行上下文

java - controlsfx 的新版本controlsfx-8.20.7 中缺少 CommandLink

java - 优雅与效率 | java

mysql - 用户注册时自动在数据库中创建表是一种好习惯吗?

java - 在 Tomcat 8 上获取运行本地 Spring Boot 应用程序的 404

java - Spring Boot项目中在多个数据库中执行搜索操作

java - 计算txt文件java文件io中的行数