无法获得spring boot以自动创建数据库架构

nap*_*hol 50 java mysql spring hibernate 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
Run Code Online (Sandbox Code Playgroud)

这是我的Application.java:

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

这是一个示例实体:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我有什么想法我做错了吗?

bor*_*s86 58

有几种可能的原因:

  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=
    
    Run Code Online (Sandbox Code Playgroud)
  3. application.properties必须在src/main/resources文件夹中.

如果你没有正确指定方言,它可能会尝试默认与boot in-memory数据库捆绑在一起(和我一样)我可以看到它尝试连接到本地HSQL(请参阅控制台输出)实例并且在更新时失败架构.

  • 将方言改为`org.hibernate.dialect.MySQL5InnoDBDialect`就可以了.谢谢您的帮助! (12认同)
  • 1号对我有用.如果我不想让我的模型与我的主类在同一个项目中,我该怎么办?我在模型包中添加了componentscan,但我没有帮助我. (2认同)
  • @ borys86,所以在您的解决方案中,我们应该改用org.hibernate.dialect.MySQL5InnoDBDialect,因为问题是关于MySQL的! (2认同)
  • #2 对我有用,但是不需要 spring.datasource.driverClassName=com.mysql.jdbc.Driver 并给出警告:正在加载类“com.mysql.jdbc.Driver”。这已被弃用。新的驱动程序类是“com.mysql.cj.jdbc.Driver”。驱动程序通过 SPI 自动注册,通常不需要手动加载驱动程序类。 (2认同)

lio*_*mon 46

你尝试过运行它:

spring.jpa.generate-ddl=true
Run Code Online (Sandbox Code Playgroud)

然后

spring.jpa.hibernate.ddl-auto = create
Run Code Online (Sandbox Code Playgroud)

默认情况下,DDL执行(或验证)将延迟到ApplicationContext启动为止.还有一个spring.jpa.generate-DDL标志,但如果Hibernate的自动配置是活动的,因为DDL-自动设置更精细的不使用它.

看看spring-boot-features


Ar *_*maj 14

@SpringBootApplication
@EnableConfigurationProperties
@EntityScan(basePackages = {"com.project.ppaa.model"})  // scan JPA entities
public class Application {

  private static ConfigurableApplicationContext applicationContext;

  public static void main(String[] args) {
    Application.applicationContext = SpringApplication.run(Application.class, args);
  }
}
Run Code Online (Sandbox Code Playgroud)

它应该自动工作,但如果没有,你可以输入基础包

@EntityScan(basePackages = {"com.project.ppaa.model"})  // scan JPA entities manually
Run Code Online (Sandbox Code Playgroud)


Abd*_*IKH 9

你只需createDatabaseIfNotExist=true像这样添加

spring.datasource.url=jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf-8&amp;amp;autoReconnect=true
Run Code Online (Sandbox Code Playgroud)

到您的 application.properties 文件

  • 这就是我长期以来一直在寻找的。谢谢,它有效 (2认同)

lee*_*der 8

可悲的是,对我来说,上面给出的答案都不起作用,因为我后来发现问题来自我的pom文件。我使用了 spring boot starter 项目,并添加了另一种 spring jpa,但它不起作用。最初我有这个,

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency> 
Run Code Online (Sandbox Code Playgroud)

我用这个替换它:

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency> 
Run Code Online (Sandbox Code Playgroud)

记下spring-boot-starter-data-jpa。希望这可以帮助某人。检查您的 pom 文件并确保您的依赖项匹配。


Jes*_*sus 7

我用这个解决方案解决了我的问题。刚刚在application.properties文件的spring.datasource.url属性上插入了一个新参数createDatabaseIfNotExist=true,如下所示:

spring.datasource.url=jdbc:mysql://localhost:3306/minhasenha?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
Run Code Online (Sandbox Code Playgroud)

我有带有 DDL的src/main/resources/Schema.sql来创建数据库架构。我确实使用过飞行器来创建和维护表格。

我在这里创建了这个解决方案: 原始答案


小智 6

如果您的实体类与主类不在同一个包中,则可以@EntityScan在主类中使用注释,指定要保存或打包的实体.喜欢你的模型包.

关于:

spring.jpa.hibernate.ddl-auto = create
Run Code Online (Sandbox Code Playgroud)

您可以使用该选项update.它不会删除任何数据,并会以相同的方式创建表.


Taf*_*Taf 6

使用以下两个设置确实有效.

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
Run Code Online (Sandbox Code Playgroud)


Phi*_*lip 5

您需要考虑您的 Spring Boot 版本以及基于该版本下载的库的版本来提供配置。

我的设置:Spring Boot 1.5.x(我的例子是 1.5.10)下载 Hibernate v5.x

仅当您的 Spring Boot 设置已下载 Hibernate v4 时才使用以下内容。

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

Hibernate 5 不支持以上。

如果您的 Spring Boot 安装程序已下载 Hibernate v5.x,则首选以下定义:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

重要提示:在 Spring Boot 应用程序开发中,您应该更喜欢使用注释:@SpringBootApplication它已被超级注释:@SpringBootConfiguration and @EnableAutoConfiguration

现在如果您的实体类与主类所在的包位于不同的包中,Spring Boot 将不会扫描这些包。

因此,您需要显式定义注释:@EntityScan(basePackages = { "com.springboot.entities" })
此注释扫描基于 JPA 的注释实体类(以及其他类似 MongoDB、Cassandra 等)

注意: “com.springboot.entities”是自定义包名称。

以下是我在 application.properties 中定义基于 Hibernate 和 JPA 的属性来创建表的方法:-

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3333/development?useSSL=true spring.datasource.username=admin
spring.datasource.password =

spring.jpa.open-in-view=false
spring.jpa.hibernate.ddl-auto=创建
spring.jpa.generate-ddl=true
spring.jpa.hibernate.use-new-id-generator-mappings=true
弹簧。 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

我可以使用上述配置创建表。

请参考它并在适用的地方更改您的代码。


asi*_*b87 5

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
Run Code Online (Sandbox Code Playgroud)

MySQL5Dialect 是有技巧的,以前我使用的是“MySQLDialect”


Udi*_*tha 5

这是我在阅读上述所有答案后所做的。

  1. spring.jpa.hibernate.ddl-auto=update其他简单属性添加到application.properties
  2. 在控制台中,您可以看到错误。在错误的一处,您可以找到此软件生成的 SQL 代码来创建您的实体表。
  3. 复制该 SQL 代码并将其单独粘贴到您的 DBMS 中以创建表。
  4. 之后,再次运行该应用程序。


Abd*_*leh 5

我通过添加解决了这个问题

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.defer-datasource-initialization=true
Run Code Online (Sandbox Code Playgroud)