无法使Spring Boot自动创建数据库模式。
无法使Spring Boot自动创建数据库模式。
我无法让Spring Boot在启动时自动加载我的数据库模式。\n这是我的application.properties文件内容:\n
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
\n这是我的Application.java文件内容:\n
@EnableAutoConfiguration @ComponentScan public class Application { public static void main(final String[] args){ SpringApplication.run(Application.class, args); } }
\n这是一个示例实体:\n
@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; } }
\n有任何想法我做错了什么吗?
无法使Spring Boot自动创建数据库模式的问题原因可能是以下几点:
1. 默认情况下,DDL执行(或验证)被推迟到ApplicationContext启动之后。虽然有一个spring.jpa.generate-ddl标志,但如果Hibernate自动配置处于活动状态,则不会使用它,因为ddl-auto设置更加细粒度。
2. 可能是执行查询的用户没有创建表的权限。
3. 在Rest Controller访问数据库之前没有出现错误。
针对这个问题,可以尝试以下解决方法:
1. 在应用程序的配置文件中添加以下属性:
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
2. 使用以下属性:
<property name="hibernate.hbm2ddl.auto">create</property>
无法使Spring Boot自动创建数据库模式的原因可能有以下几点:
1. 实体类没有处于与包含主类的同一包或子包中,导致Spring应用程序无法识别到它们,因此不会在数据库中创建任何内容。
2. 配置文件可能存在问题,可能使用了一些特定于Hibernate的选项,可以尝试将它们替换为以下配置:
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=
需要注意的是,手动加载驱动类是不必要的,因为它会自动注册,所以不需要处理这个问题。
3. `application.properties`文件必须位于`src/main/resources`文件夹中。
如果未正确指定方言,可能会尝试默认使用与Spring Boot捆绑在一起的内存数据库,并且(就像我遇到的情况)可能会尝试连接到本地的HSQL实例并在更新模式时失败。
将方言更改为`org.hibernate.dialect.MySQL5InnoDBDialect`可以解决此问题。
以下是其他用户对解决方法的评论:
- "第一种方法对我有效。如果我不想将模型和主类放在同一个项目中,我应该怎么办?我在模型包中添加了`@ComponentScan`注解,但没有起作用。"
- "Badr,你的评论是正确的。很可能我同时粘贴了多个配置文件的行。方言和驱动程序应该与目标数据库相匹配。"
- "所以在你的解决方案中,我们应该使用`org.hibernate.dialect.MySQL5InnoDBDialect`,因为问题是关于MySQL的!"
- "Badr是正确的,这里的正确属性值是`org.hibernate.dialect.MySQL5InnoDBDialect`。我尝试编辑答案,但被管理员拒绝了。"
- "第二种方法对我有效,但是`spring.datasource.driverClassName=com.mysql.jdbc.Driver`是不需要的,而且会出现警告:加载类`com.mysql.jdbc.Driver'。这已被弃用。新的驱动程序类是`com.mysql.cj.jdbc.Driver'。驱动程序通过SPI自动注册,手动加载驱动程序类通常是不必要的。"
- "如果你有自定义的数据库模式文件,可能是因为你需要支持多个模式或数据库,更新属性文件可能不起作用,因为你可能已经在`LocalContainerEntityManagerFactoryBean`中显式设置了你的JPA属性。此外,如果你使用了Hibernate特定的属性,`hibernate.hbm2ddl.auto`将起作用。"
- "当我将方言从`org.hibernate.dialect.MySQLDialect`更改为`org.hibernate.dialect.MySQL8Dialect`时,它起作用了。"
- "我的配置如下:`spring.jpa.hibernate.ddl-auto=update` `spring.datasource.url=jdbc:mysql://localhost:3306/littleNotes` `spring.datasource.username=root` `spring.datasource.password=******` `spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver` `spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect`"
以上是一些用户对于解决该问题的评论和解决方案。