无法进行弹簧启动以自动创建数据库架构

当我启动数据库模式时,我无法获得 Spring 启动来自动加载它。

这是我的应用程序。属性:

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

下面是一个示例实体:

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

知道我哪里做错了吗?

428342 次浏览

你有没有试过用:

spring.jpa.generate-ddl=true

and then

spring.jpa.hibernate.ddl-auto = create

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

弹簧-靴子-特征

There are several possible causes:

  1. 您的实体类是在相同的或在子包相对的一个,您有您的类与 @EnableAutoConfiguration.如果没有,那么您的春天应用程序不会看到他们,因此将不会创建任何数据库

  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实例(参见控制台输出) ,并且在更新模式时失败。

我也有同样的问题。原来我在主 Application 类上设置了@PropertySource 注释来读取一个不同的基本属性文件,因此不再使用普通的“ Application.properties”。

我也遇到了同样的问题,只用下面这句话就解决了:

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

如果实体类与主类不在同一个包中,则可以在主类中使用 @EntityScan注释,同时指定要保存或打包的实体。就像你的模特套装。

关于:

spring.jpa.hibernate.ddl-auto = create

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

只要加上

spring.jpa.databaseplatform=org.hibernate.dialect.PostgreSQLDialect

这会解决你的问题。 Only this was missing

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

它应该自动工作,但如果它不,你可以进入基本包

@EntityScan(basePackages = {"com.project.ppaa.model"})  // scan JPA entities manually
Use this Sample code


application.properties
# DataSource settings: set here your own configurations for the database
# connection. In this example we have "dojsb" as database name and
# "root" as username and password.
spring.datasource.url =jdbc:postgresql://localhost:5432/usman
spring.datasource.username = postgres
spring.datasource.password = 12345


# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1


# Show or not log for each sql query
spring.jpa.show-sql = true


# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = create


# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy


# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)


# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect


server.port = 8963






Entity Class:






import java.sql.Timestamp;
import java.util.UUID;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


import org.hibernate.annotations.Type;




@Entity
@Table(name = "QUEUERECORDS")
public class QueuesRecords {
@Id
private UUID id;


@Column(name="payload", nullable = true)
@Type(type="text")
private String payload;




@Column(name="status", nullable = true)
@Type(type="text")
private String status;


private Timestamp starttime;


private Timestamp endtime;


@Column(name="queueid",nullable= true)
@Type(type="text")
private String queueid;


public UUID getId() {
return id;
}


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


public String getPayload() {
return payload;
}


public void setPayload(String payload) {
this.payload = payload;
}


public String getStatus() {
return status;
}


public void setStatus(String status) {
this.status = status;
}


public Timestamp getStarttime() {
return starttime;
}


public void setStarttime(Timestamp starttime) {
this.starttime = starttime;
}


public Timestamp getEndtime() {
return endtime;
}


public void setEndtime(Timestamp endtime) {
this.endtime = endtime;
}


public String getQueueid() {
return queueid;
}


public void setQueueid(String queueid) {
this.queueid = queueid;
}






}






Main class






import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;




@SpringBootApplication
public class Test{


public static void main(String[] args) {


SpringApplication.run(Test.class, args);




}
}

我以前也遇到过同样的问题。我的问题是我试图通过使用“ List”建立的 Entity 关系。我知道这是原因,因为程序运行良好,没有列表变量。就你而言,我认为问题在于:

private List<Question> _questions;

我想你们已经有一门叫做“问题”的课程了,所以,试试:

@OneToMany
private Question _questions;

But the thing is, in your method, you are going to handle it so it return a list. I used Spring Data JPA with CrudRepository. So, if you decide to use it, yours may look like this:

public List<Question> findById( Long _id );

您还需要做更多的更改,但这些更改非常简单和直接。请参考 这个 Java Brains 的视频,以便更好地掌握并查看还需要修改哪些内容。

在我的例子中,即使我使用 JParepository,也没有自动创建表。 在我的 springboot app application.properties 文件中添加了以下属性之后,这些表现在自动创建了。 Ddl-auto = update

很简单,我们在 < br > spring.jpa.hibernate.ddl-auto = create;后面添加分号
这是错误的 < br > spring.jpa.hibernate.ddl-auto = create < br > 足够

使用以下两种设置确实有效。

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create

您需要考虑到您的 Spring 引导版本以及它下载的基于该版本的库的版本,来提供配置。

我的安装程序: Spring Boot 1.5.x (对我来说是1.5.10)下载 Hibernate v5.x

只有当您的 SpringBoot 安装程序已经下载 Hibernatev4时,才可以使用下面的命令。

NamingStrategy = org.hibernate.cfg

Hibernate 5不支持以上内容。

如果您的 Spring 引导安装程序已经下载了 Hibernate v5.x,那么更喜欢下面的定义:

物理-策略 = org.hibernate.boot.mod.name.PhysicalNamingStrategies yStandardImpl

IMPORTANT: 在 Spring Boot 应用程序开发中,您应该更喜欢使用注释: @SpringBootApplication,它已经用: @SpringBootConfiguration and @EnableAutoConfiguration进行了超级注释

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

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

NOTE: “ com.springboot.tity”是自定义包的名称。 < br >

下面是我在 application.properties 中定义 Hibernate 和 JPA 属性以创建 Tables 的方法:-

Driver-class-name = com.mysql.jdbc. Driver
Url = jdbc: mysql://localhost: 3333/development? useSSL = true Username = admin
Password = < br >

Open-in-view = false
Ddl-auto = create
Generatedddl = true
spring.jpa.hibernate.use-new-id-generator-mappings=true
物理-策略 = org.hibernate.boot.mod.name.PhysicalNamingStrategies yStandardImpl
战略 = org.hibernate.cfg. 改进命名策略
spring.jpa.show-sql=true
方言 = org.hibernate.direct.MySQL5方言
spring.jpa.properties.hibernate.format_sql=true

我能够使用上述配置创建表。

引用它,并在适当的地方更改代码。

像这样加入 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

to your application.properties file

if your DB is MySQL:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=root
spring.datasource.password=root

如果你的数据库是 PostgreSQL:

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:postgresql://localhost:3306/your_database
spring.datasource.username=root
spring.datasource.password=root

Abderrahmane 响应正确: 在 url 属性中添加 ?createDatabaseIfNotExist=true。 看来 ddl-auto什么也做不了。

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update

MySQL5方言确实有用,之前我用的是‘ MySQLDiect’

This is what I did after reading all of the answers above.

  1. 将带有其他简单属性的 spring.jpa.hibernate.ddl-auto=update添加到 应用性能
  2. 快跑
  3. 在控制台中,您可以看到错误。在错误的一个位置,您可以找到由该软件生成的 SQL 代码,以创建您的实体表。
  4. 复制该 SQL 代码并将其分别粘贴到 DBMS 中以创建表。
  5. 之后,再次运行应用程序。

只需在 Spring 数据源 URL 中添加 CreateDatabaseIfNotExist = true参数

例如: 数据源. url = Mysql://localhost: 3306/test? createDatabaseIfNotExist = true

我用这个办法解决了我的案子。 刚刚在 应用性能文件的 数据来源.url属性上插入了一个新参数 CreateDatabaseIfNotExist = true,如下所示:

spring.datasource.url=jdbc:mysql://localhost:3306/minhasenha?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true

我使用带 DDL 的 Src/main/resources/Schema.sql来创建数据库模式。我确实使用了 飞走了来创建和维护这些表。

I founded this solution here: 原始答案

遗憾的是,上面给出的答案都没有用,因为我后来发现问题来自我的 pom文件。我使用弹簧启动器项目,我增加了另一种弹簧 jpa 不工作。 一开始我有这个,

    <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>

我换成了这个:

   <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

请注意 Spring-boot-starter-data-jpa。希望这能对某些人有所帮助。检查您的 pom 文件并确保您的依赖项匹配。

下面的配置适合我:

spring.jpa.properties.javax.persistence.schema-generation.database.action=create
spring.jpa.properties.javax.persistence.schema-generation.create-database-schemas=true
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.drop-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.connection=jdbc:mysql://localhost:3306/your_database

我也遇到过类似的问题。我正在使用 spring boot 2.x,但是我错过了在 spring 初始化器中添加 Postgres 依赖项。 我手动添加了依赖项

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

and here is what I was getting- INFO org.hibernate.dialect.Dialect - HHH000400: Using dialect: 方言 而不是

**INFO  org.hibernate.dialect.Dialect - HHH000400: Using
dialect:org.hibernate.dialect.PostgreSQL10Dialect**

这把我和尸体联系起来了

这并不奇怪,因为 Springboot 自己做版本依赖,减少了开发工作。另一方面,如果 Springboot 选择了不正确的依赖,它将浪费大量时间。

如果您在 Spring Boot 中遇到这个问题,请仔细检查您的包名,它应该完全类似于:

com.example.YOURPROJECTNAME - consists main application class
com.example.YOURPROJECTNAME.entity - consists entities

使用 springboot 连接 mysql,并自动将 table 创建到数据库中: spring.datasource.url=jdbc:mysql://localhost:3306/solace spring.datasource.username=root spring.datasource.password=root spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=update

在我的示例中,我必须将表重命名为 user。我重命名它为例 users和它的工作。

我也有同样的问题,但我加上

spring.jpa.hibernate.ddl-auto = create

and everthing it is worked now

这个帮了我

我假设你有 INNODB 引擎:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect


spring.jpa.properties.hibernate.dialect.storage_engine=innodb

使用 Mysql-8.x: Database-Platform = org.hibernate.direct.MySQL8方言代替 MySQL5方言

我用加法解出来的

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.defer-datasource-initialization=true