随笔 - 54  文章 - 0  评论 - 2  阅读 - 65713

Caused by: java.sql.SQLSyntaxErrorException: Table 'sell.hibernate_sequence' doesn't exist

数据表:

create table 'product_category'(
    'category_id' int not null auto_increment,
    'category_name' varchar(64) not null comment '类目名字',
    ......  

domain:    

复制代码
@Entity
public class ProductCategory {
   @Id
   @GeneratedValue
   private Integer categoryId;
   private String categoryName;
   private Integer categoryType;
    setter... 
    getter...
复制代码

Repository:

public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {

}

测试代码:

@Test
    public void saveTest(){
        ProductCategory productCategory = new ProductCategory();
        productCategory.setCategoryName("测试b");
        productCategory.setCategoryType(3);
        repository.save(productCategory);
    }

报错:Caused by: java.sql.SQLSyntaxErrorException: Table 'sell.hibernate_sequence' doesn't exist

解决:在domain实体类指明主键生成策略,保持数据库一致

@Entity
public class ProductCategory {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer categoryId;
   private String categoryName;
   private Integer categoryType;

JPA四种生成策略了解下。

疑点:原文使用@GeneratedValue注解,即@GeneratedValue(strategy = GenerationType.AUTO),不是应该根据数据库支持的主键生成策略自动选择合适的吗,数据库中使用了auto_increment,为什么这边没有支持而报错?

 

posted on   kongieg  阅读(3239)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示