Spring集成JPA报错:AnnotationException: No Identifier specified for entity

SpringBoot项目集成JPA,运行起来时报No Identifier specified for entity错误,具体如下:

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.example.entity.User
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]

错误原因是没有给id添加注释@Id。如图:

其中@Id,包的全路径是import javax.persistence.Id;

版权声明:著作权归作者所有。

相关推荐

Spring Boot集成JSP

Spring Boot是不推荐使用JSP作为模板文件。如果Spring Boot使用JSP是有一些限制,其中只能打包成可执行的war包或者可在外部容器(如tomcat)部署的war包。项目目录结构在普通的spring boot项目基础上,在src/main下新增目录webapp/WEB-INF/jsp用来存放jsp文件,静态文件存放在/src/resources/static。结构如图:

Spring Cloud Stream集成Kafka

这里演示使用Spring Boot ,Spring Cloud集成Kafka来实现一个简单的实时流系统。添加依赖可以在https://start.spring.io创建一个基于spring boot的maven项目。需要添加的主要依赖:spring-cloud-stream以及spring-cloud-starter-stream-kafka,如下:<

Spring Boot集成RabbitMQ发送接收JSON

在Spring Boot 集成RabbitMQ一文中介绍了如何集成RabbitMQ。默认情况下发送的消息是转换为字节码,这里介绍一下如何发送JSON数据。ObjectMapper最简单发送JSON数据的方式是把对象使用ObjectMapper等JSON工具类把对象转换为JSON格式,然后发送。如下:@Autowired private ObjectMapper&nbs

Spring Boot 集成RabbitMQ

此处假设已经安装好了RabbitMQ,主要讲述使用Spring Boot如何集成RabbitMQ。添加依赖在Maven的pom.xml添加rabbitmq的starter依赖,内容如下:<?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo

Spring Boot集成kafka

Spring Boot对kafka提供了自动配置(auto configuration)。使用用Spring Boot只需要做很少的配置即可集成对kafka的访问。pom配置1、继承spring-boot-starter-parent<parent>   <groupId>org.springframework.boot&l

Spring Boot:日志集成

Java日志框架Java有好几个日志框架,我们有时在选择Java日志框架时会有点迷惑。下面几个是常用的日志框架Common Logging:Apache最早提供的日志的门面接口。避免和具体的日志方案直接耦合。SLF4J(Simple Logging Facade for JAVA):和Common Logging一样,是一个门面框架,是对