【问题标题】:Spring ignores Entity annotationSpring忽略实体注释
【发布时间】:2021-06-30 08:34:07
【问题描述】:

当我尝试使用 Spring Boot 任何 mysql 创建表时遇到问题,似乎 Spring boot 没有创建表,尝试了各种解决方案,包括:

我使用的是 Windows 10,并且在 MySql Workbench 中为当前用户授予所有权限。即使尝试默认: 根 根 它不起作用

@EntityScan(basePackage={""})

这是我的课:

@Entity
public class Student {

@Id
private long id;
private String firstName;
private String lastName;
private String email;
private int age;

public Student(){
    System.out.println("student executed");
}

public Student(long id, String firstName, String lastName, String email, int age) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.age = age;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}



@Override
public String toString() {
    return "Student{" +
            "id=" + id +
            ", firstName='" + firstName + '\'' +
            ", lastName='" + lastName + '\'' +
            ", email='" + email + '\'' +
            ", age=" + age +
            '}';
}

}

和application.properties:

   server.port=8080
spring.datasource.username=newuser
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/spring


spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql=true

启动应用程序标准Spring启动方式。

主类:

@EntityScan("com.example.demo.models")
@SpringBootApplication
public class DemoApplication {



    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }
}

enter image description here 感谢您的任何提示。

【问题讨论】:

  • 您的应用程序的结构是什么?我的意思是包
  • DemoApplication(包含 main 方法)在:com.example.demo 和 Student(@Entity model) 在 com.example.demo.models。
  • 试试@EntityScan("com.example.demo.models")
  • 已经用不同的注释尝试了多次,但不起作用
  • 请编辑您的问题并粘贴具有@EntityScan 的类

标签: java spring spring-boot jpa


【解决方案1】:

我从 Spring Initializr 创建了一个新项目,并使用 Java 8 而不是 Java 11,它似乎有效,我确实更新了我拥有的任何 maven 依赖项并重新检查了最新版本。 我有java version "1.8.0_261" 安装了,谢谢大家的努力!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2012-05-19
    • 2013-11-05
    • 2014-10-06
    • 2018-02-23
    • 1970-01-01
    • 2016-05-22
    相关资源
    最近更新 更多