Я пытаюсь подключить мой сервер 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;
}
и интерфейс репо
@Repository(value = "productrepo")
public interface ProductCategoryRepository extends
JpaRepository<ProductCategory, Long> {
void deleteByCategoryId(long categoryId);
}
my DBConfig
import 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;