"不存在异常如何解决-6ren"> "不存在异常如何解决-我正在尝试将 MySql 服务器连接到我的 springboot 项目。我有一个名为product_cateogories 的表,我正在尝试将其与实体productCategory 连接,如下所示: -6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:32:59 27 4
gpt4 key购买 nike

我正在尝试将 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com