【问题标题】:spring boot unable to generate tables in mysql databasespring boot 无法在mysql数据库中生成表
【发布时间】:2019-10-13 05:35:14
【问题描述】:

我正在尝试从defiend JpaEntities 更新数据库架构,但spring boot 无法生成数据库表。 日志文件:

application.properties 文件:

spring.datasource.url= jdbc:mysql://localhost:3306/banque_db
spring.datasource.username= root
spring.datasource.password=
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto= update
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

客户端实体类:

 @Entity(name="client")
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long code;
private String name;
private String email;
@OneToMany(mappedBy = "compte")
private Collection<Compte> comptes;
// + getters and setters

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.0.发布 com.Tuto

<artifactId>MaBanque</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MaBanque</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>       
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>       
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

【问题讨论】:

    标签: java mysql spring


    【解决方案1】:

    您必须为创建数据库设置spring.jpa.hibernate.ddl-auto = createcreate-drop

    更多详情请见docs

    【讨论】:

      【解决方案2】:

      第一个问题: application.properties 文件:

      1. 如果您设置属性spring.jpa.hibernate.ddl-auto = update,它将不会为您创建架构。相反,它会更新您目前没有的现有数据库。
      2. spring.jpa.hibernate.ddl-auto默认 值为 create-drop,但您正在覆盖它。
      3. 您的 application.properties 必须位于

      src/main/资源

      文件夹。

      ==> 你应该修正:

      spring.jpa.hibernate.ddl-auto = 创建 - 删除

      第二个问题:实体类。您应该在属性顶部添加 @Column,如下所示:

      `@Entity(name="client")
      public class Client implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @Column
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long code;
      @Column
      private String name;
      @Column
      private String email;
      @OneToMany(mappedBy = "compte")
      private Collection<Compte> comptes;
      // + getters and setters`
      

      【讨论】:

        【解决方案3】:

        实际上,问题在于实体类不在正确的包中。 org.example 包含主类和 org.entities 由于某种原因,spring boot 没有生成表,所以当我将实体包移动到像 org.example.entities 并运行应用程序时,所有表都成功生成了。 谢谢你们的帮助我appriciate :)

        【讨论】:

          猜你喜欢
          • 2017-05-28
          • 1970-01-01
          • 1970-01-01
          • 2020-01-11
          • 1970-01-01
          • 2016-10-11
          • 2016-12-02
          • 1970-01-01
          • 2021-10-30
          相关资源
          最近更新 更多