无法获得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 _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 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 questions) { _questions = questions; } } 

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

有几种可能的原因:

  1. 您的实体类与您使用@EnableAutoConfiguration.类相关的实体类相同或位于子包中@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文件夹中。

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

你尝试过运行它:

 spring.jpa.generate-ddl=true 

接着

 spring.jpa.hibernate.ddl-auto = create 

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

看看spring-boot-features

 @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 

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

关于:

 spring.jpa.hibernate.ddl-auto = create 

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

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

在我的情况下,即使我使用JPArepository,也没有自动创建表。 在我的springboot app application.properties文件中添加以下属性后,现在将自动创建表。 spring.jpa.hibernate.ddl-AUTO =更新

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

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

您需要提供考虑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策略= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

重要信息:在Spring Boot应用程序开发中,您应该更喜欢使用注释: @SpringBootApplication ,它已经过以下注释: @SpringBootConfiguration and @EnableAutoConfiguration

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

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

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

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

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

spring.jpa.open式视=假
spring.jpa.hibernate.ddl-AUTO =创建
spring.jpa.generate-DDL =真
spring.jpa.hibernate.use新-ID-发电机映射=真
spring.jpa.hibernate.naming.physical策略= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-SQL =真
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql =真

我可以使用上面提到的配置创建表。

如果适用,请参考并更改您的代码。

我有同样的问题,只用这个添加解决了它:

 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect 
 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变量,程序运行正常。 在你的情况下,我认为问题是:

 private List _questions; 

我假设你已经有一个名为Question的课程。 所以,试着:

 @OneToMany private Question _questions; 

但事实是,在你的方法中,你将处理它,所以它返回一个列表。 我将Spring Data JPA与CrudRepository一起使用。 所以,如果您决定使用它,您的可能如下所示:

 public List findById( Long _id ); 

您将需要做更多更改,但这些更改非常简单明了。 请参阅此Java Brainsvideo以更好地掌握并查看还需要修改的内容。

很简单,我们之后添加分号
spring.jpa.hibernate.ddl-auto = create;
这是错的
spring.jpa.hibernate.ddl-auto = create
足够

你只需添加createdataBasIfNoExist = ture就像这样

 spring.datasource.url=jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true 

到您的application.properties文件

只需添加

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

最后。 这将解决您的问题。 只有这个丢失了