无法使用Spring启动来自动创建数据库模式 [英] Unable to get spring boot to automatically create database schema

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

问题描述



这是我的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 =创建
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);




$ b $ p
$ b

以下是一个示例实体:

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

private Long _id;

private String _name;

私人列表<问题> _questions;

/ **
* @return调查的ID。
* /
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name =survey_id,unique = true,nullable = false)
public Long getId(){
return _id;
}

/ **
* @返回调查名称。
* /
@Column(name =name)
public String getName(){
return _name;
}


/ **
* @返回调查问题列表。
* /
@OneToMany(mappedBy =survey)
@OrderBy(id)
public List< Question> getQuestions(){
return _questions;
}

/ **
* @param id要设置的id。
* /
public void setId(Long id){
_id = id;
}

/ **
* @param命名问题的名称。
* /
public void setName(final String name){
_name = name;
}

/ **
* @param问题问题列表。
* /
public void setQuestions(List< question> question){
_questions = questions;


$ / code $ / pre

任何想法我做错了吗?

解决方案

有几种可能的原因:


  1. 你的实体类与你使用 @EnableAutoConfiguration类进行分类的相同或相同的子包中。如果没有,那么你的spring应用程序不会看到它们,因此不会在db中创建任何东西
  2. 检查你的配置,似乎你正在使用一些特定于hibernate的选项,试着用它们替换它们:

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


  3. 您的 application.properties 必须位于 src / main / resources 内存数据库和(和我一样)我可以看到它试图连接到本地 HSQL (请参阅控制台输出)实例,并且无法更新模式。


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

    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
    

    Here is my Application.java:

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

    Here is a sample entity:

    @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;
        }
    }
    

    Any ideas what I'm doing wrong?

    解决方案

    There are several possible causes:

    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
    2. 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.jdbc.Driver
      spring.datasource.url=jdbc:mysql://localhost:3306/test
      spring.datasource.username=test
      spring.datasource.password=
      

    3. Your application.properties must be in src/main/resources folder.

    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启动来自动创建数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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