springboot 2.2.5 在使用jpa时,出现异常BeanCreationException时的解决办法
实体类为:
//使用JPA注解配置映射关系
@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "user") //指定和哪一个数据表进行映射;如果省略默认表明就是类名小写
public class User {
@Id //这是一个主键
@GeneratedValue(strategy = GenerationType.AUTO) //自增组件
private Integer id;
导入的@Id包为:
import org.springframework.data.annotation.Id;
出现的异常为:
ConfigServletWebServerApplicationContext : 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.atguigu.springboot.entity.User
致错原因:导入@Id包错误,应该导入的是:
import javax.persistence.Id;
完美解决:
2020-03-08 19:57:32.965 INFO 9356 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-08 19:57:33.755 INFO 9356 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-08 19:57:33.758 INFO 9356 --- [ main] c.a.s.SpringBoot06DataJpaApplication : Started SpringBoot06DataJpaApplication in 28.325 seconds (JVM running for 31.204)
可以作为参考。