无法让 spring boot 自动创建数据库架构 [英] Unable to get spring boot to automatically create database schema

查看:26
本文介绍了无法让 spring boot 自动创建数据库架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动它时,我无法让 spring boot 自动加载我的数据库架构.

I'm unable to get spring boot to automatically load my database schema when I start it up.

这是我的 application.properties:

Here is my 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:

Here is my 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;
    }
}

知道我做错了什么吗?

推荐答案

可能的原因有以下几种:

There are several possible causes:

  1. 您的实体类与您在其中使用 @EnableAutoConfiguration 进行类相同或相对的子包中. 如果不是,则您的 Spring 应用程序看不到它们,因此会不要在 db 中创建任何东西

  1. Your entity classes are in the same or in a sub-package relative one where you have you class with @EnableAutoConfiguration. If not then your spring app does not see them and hence will not create anything in db

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

Check your config, it seems that you are using some hibernate specific options, try to replace them with:

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=

**注意驱动类的手动加载是不必要的,因为它是自动注册的,所以不要自己费心

**note that the manual loading of the driver class is unnecessary because it's automatically registered, so don't bother yourself with it

  1. 您的 application.properties 必须在 src/main/resources 文件夹中.
  1. Your application.properties must be in src/main/resources folder.

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

If you did not specify dialect correctly it might try to default to bundled together with boot in-memory database and (as it was with me) I could see that it tries to connect to local HSQL (see console output) instance and fail at updating the schema.

这篇关于无法让 spring boot 自动创建数据库架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆