持久实体 'Product' 应该有主键

Persistent entity 'Product' should have primary key

将鼠标悬停在 class 产品上,您会看到“持久实体 'Product' 应该有主键”。通过快速搜索,我没有找到任何相关内容。

package com.example.demo.entity;

import lombok.Data;

import javax.persistence.Entity;

import javax.persistence.Table;


@Table(name="product")
@Data
@Entity
public class Product {
}

这意味着您的实体 class 应该定义一个 ID 字段,jpa 会根据该字段来决定它需要生成哪种类型的主键。

从 javax.persistence 包中定义实体 class 中的字段,如下所示。

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;