Entity 已经加了@Id 注解后仍然出现 No identifier specified for entity
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.domain.SystemConfig
2019-03-01 13:15:30.976 INFO 1796 — [ main] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService ‘taskScheduler’
2019-03-01 13:15:30.999 INFO 1796 — [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed
2019-03-01 13:15:31.004 INFO 1796 — [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
查看网上的资料,应该是报错的实体类ccom.domain.SystemConfig中没有添加加主键的注解@Id,这个是必须的。但是我的实体类中明明已经添加了@Id,为什么还会报这个错误呢?
后来检查了很久,发现是我import的包出现了错误,正确的应该是import javax.persistence.Id; 而我却导入了import org.springframework.data.annotation.Id;这样虽然@Id 在IDE语法检查时不会报错,但并不是我们本来想要的那个功能,因此运行时会报上面但错误。