无法让spring boot自动创建数据库模式

新手上路,请多包涵

当我启动它时,我无法让 spring boot 自动加载我的数据库模式。

这是我的application.properties:

 spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=test
spring.datasource.password=
spring.datasource.driverClassName = com.mysql.jdbc.Driver

spring.jpa.database = MYSQL

spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto = create
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy

这是我的Application.java:

 @EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(final String[] args){
        SpringApplication.run(Application.class, args);
    }
}

这是一个示例实体:

 @Entity
@Table(name = "survey")
public class Survey implements Serializable {

    private Long _id;

    private String _name;

    private List<Question> _questions;

    /**
     * @return survey's id.
     */
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "survey_id", unique = true, nullable = false)
    public Long getId() {
        return _id;
    }

    /**
     * @return the survey name.
     */
    @Column(name = "name")
    public String getName() {
        return _name;
    }

    /**
     * @return a list of survey questions.
     */
    @OneToMany(mappedBy = "survey")
    @OrderBy("id")
    public List<Question> getQuestions() {
        return _questions;
    }

    /**
     * @param id the id to set to.
     */
    public void setId(Long id) {
        _id = id;
    }

    /**
     * @param name the name for the question.
     */
    public void setName(final String name) {
        _name = name;
    }

    /**
     * @param questions list of questions to set.
     */
    public void setQuestions(List<Question> questions) {
        _questions = questions;
    }
}

任何想法我做错了什么?

原文由 napentathol 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 546
2 个回答

有几个可能的原因:

  1. 您的实体类与您的实体类相同或在子包中,您使用 @EnableAutoConfiguration. 如果没有,那么您的 spring 应用程序看不到它们,因此不会在 db 中创建任何东西

  2. 检查您的配置,您似乎正在使用一些特定于休眠的选项,请尝试将它们替换为:

    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
   spring.jpa.hibernate.ddl-auto=update
   spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
   spring.datasource.url=jdbc:mysql://localhost:3306/test
   spring.datasource.username=test
   spring.datasource.password=

**请注意,驱动程序类的手动加载是不必要的,因为它是自动注册的,所以不要为它烦恼

  1. 您的 application.properties 必须在 src/main/resources 文件夹中。

如果您没有正确指定方言,它可能会尝试默认与启动内存数据库捆绑在一起,并且(就像我一样)我可以看到它尝试连接到本地 HSQL (请参阅控制台输出) 实例并在更新架构时失败。

原文由 borys86 发布,翻译遵循 CC BY-SA 4.0 许可协议

您是否尝试使用以下方式运行它:

 spring.jpa.generate-ddl=true

接着

spring.jpa.hibernate.ddl-auto = create

默认情况下,DDL 执行(或验证)被推迟到 ApplicationContext 启动后。还有一个 spring.jpa.generate-ddl 标志,但如果 Hibernate autoconfig 处于活动状态则不会使用它,因为 ddl-auto 设置更细粒度。

查看 spring-boot-features

原文由 liorsolomon 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题