【问题标题】:im creating project using spring boot but i got error and not resolved yet我正在使用 spring boot 创建项目,但出现错误但尚未解决
【发布时间】:2023-01-24 05:47:12
【问题描述】:

while running application got this error I am using MySQL 8 version and also here get dialect version 8 but its not working

我想连接数据库并存储我已经通过的值 包com.practice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import com.practice.dao.UserRepository;
import com.practice.entities.User;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
    ApplicationContext context =     SpringApplication.run(DemoApplication.class, args);
    
    UserRepository userRepository =context.getBean(UserRepository.class);
    
    User user = new User();
    user.setName("shrikant suryawanshi");
    user.setCity("Pune");
    user.setStatus("active");
    
    User user1 = userR
Repository.save(user);
    System.out.println(user1);`
    }
}

错误:

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Entity 'com.practice.entities.User' has no identifier (every '@Entity' class must declare or inherit at least one '@Id' or '@EmbeddedId' property)

【问题讨论】:

  • 添加代码和错误消息作为文本而不是图像。
  • 您是否阅读了错误,因为它告诉您到底出了什么问题! very '@Entity' class must declare or inherit at least one '@Id' or '@EmbeddedId' property 看起来很清楚。

标签: spring spring-boot spring-data-jpa


【解决方案1】:

每个实体(您数据库中的表)都必须使用 @Entity 注释声明,并且每个实体都必须具有使用 @Id 注释 https://www.baeldung.com/jpa-entities 注释的唯一标识符。如果修复实体类后错误仍然存​​在,请将它们添加到您的问题中

【讨论】:

    【解决方案2】:

    我得到了同样的错误,结果证明我对@Id 的导入不正确。正确的导入是

    import jakarta.persistence.Id

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 2021-05-08
      • 2020-06-23
      • 2012-09-05
      • 2016-10-03
      • 2021-09-21
      • 1970-01-01
      • 2021-07-13
      • 2021-01-09
      相关资源
      最近更新 更多