SpringBoot 遇到 No identifier specified for entity
No identifier specified for entity 从字面上不难看出来是没有设置主键
因为没有为标注为@Entity的实体类注明主键
import lombok.Data; import org.springframework.data.annotation.Id; import javax.persistence.*; @Data @Table(name = "user") @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Integer age; }
但实际上bean用@Id和@GenerateValue标注在ID上,仔细观察发现是导错包:import org.springframework.data.annotation.Id;这是Spring提供的;而应该导入javax.persistence.Id,很细微的差别,可能不注意看就错了而且很难去排查问题
区别在于:org.springframework.data.annotation.Id是spring用来支持MongoDB等非关系型数据库的持久化(spring-data-mongodb等);javax.persistence.Id适用于关系型的数据