Unable to create tables in MySQL database automatically using Spring Boot.

huangapple go评论88阅读模式
英文:

Unable to create tables in MySQL database automatically using Spring Boot

问题

我是新手学习Spring Boot,尝试从我的实体中创建MySQL数据库中的表格,但不幸的是在运行我的应用程序后什么都没有创建。以下是我的代码:

User.java

@Entity
public class User {
    @Id
    @GeneratedValue
    private Long id;

    private String firstName;

    private String lastName;

    @Column(unique = true, nullable = false)
    private String username;

    @Column(nullable = false)
    private String password;

    private String prevPass;

    @Column(nullable = false)
    private Organization organization;

    @ManyToMany
    @Column(nullable = false)
    private Set<Role> roles = new HashSet<>();

    @Column(nullable = false)
    private Boolean enabled = true;

    @Column(nullable = false, unique = true)
    private String email;

    private String position;

    public User() {
    }
}

Role.java

@Entity
public class Role {
    @Id
    @GeneratedValue
    private Long id;

    @Column(unique = true, nullable = false)
    private String code;

    @Column(unique = true, nullable = false)
    private String title;
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=sth
spring.jpa.hibernate.ddl-auto=update

pom.xml 依赖项

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<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-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

我还在我的application.properties文件中使用了以下配置:

spring.jpa.hibernate.ddl-auto=create

和

spring.jpa.hibernate.ddl-auto=create-drop

我必须提到我的实体与包含@SpringBootApplication注解的类位于同一个包中。以下是日志:

2020-08-10 22:52:14.139  INFO 10011 --- [           main] a.b.c.abc          : Starting abc on user with PID 10011 
2020-08-10 22:52:14.139  INFO 10011 --- [           main] a.b.c.abc          : No active profile set, falling back to default profiles: default
2020-08-10 22:52:15.339  INFO 10011 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-08-10 22:52:15.370  INFO 10011 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23ms. Found 0 JPA repository interfaces.
2020-08-10 22:52:16.292  INFO 10011 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-10 22:52:16.307  INFO 10011 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-10 22:52:16.307  INFO 10011 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-10 22:52:16.495  INFO 10011 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-10 22:52:16.495  INFO 10011 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2180 ms
2020-08-10 22:52:16.886  INFO 10011 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-10 22:52:16.948  INFO 10011 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-08-10 22:52:17.011  WARN 10011 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-08-10 22:52:17.042  INFO 10011 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.18.Final
2020-08-10 22:52:17.378  INFO 10011 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-08-10 22:52:17.724  INFO 10011 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-08-10 22:52:18.251  INFO 10011 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-10 22:52:18.258  INFO 10011 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…


<details>
<summary>英文:</summary>

I`m new to Spring Boot and trying to create tables from my entities in MySQL database, but unfortunately nothing is created after running my application. Here is my code:

**User.java**

@Entity
public class User {
@Id
@GeneratedValue
private Long id;

private String firstName;

private String lastName;

@Column(unique = true, nullable = false)
private String username;

@Column(nullable = false)
private String password;

private String prevPass;

@Column(nullable = false)
private Organization organization;

@ManyToMany
@Column(nullable = false)
private Set&lt;Role&gt; roles = new HashSet&lt;&gt;();

@Column(nullable = false)
private Boolean enabled = true;

@Column(nullable = false, unique = true)
private String email;

private String position;

public User() {
}

}


**Role.java**

@Entity
public class Role {
@Id
@GeneratedValue
private Long id;

@Column(unique = true, nullable = false)
private String code;

@Column(unique = true, nullable = false)
private String title;

}


**application.properties**

spring.datasource.url= jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=sth
spring.jpa.hibernate.ddl-auto=update


**pom.xml dependencies**

&lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
    &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;mysql&lt;/groupId&gt;
        &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
        &lt;scope&gt;runtime&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
        &lt;artifactId&gt;lombok&lt;/artifactId&gt;
        &lt;optional&gt;true&lt;/optional&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
        &lt;exclusions&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
            &lt;/exclusion&gt;
        &lt;/exclusions&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

I have also used

spring.jpa.hibernate.ddl-auto=create

and

spring.jpa.hibernate.ddl-auto=create-drop

in my application.properties file. I have to mention that my entities are in the same package with the class containing **@SpringBootApplication** annotaion.
Here is the log:

2020-08-10 22:52:14.139 INFO 10011 --- [ main] a.b.c.abc : Starting abc on user with PID 10011
2020-08-10 22:52:14.139 INFO 10011 --- [ main] a.b.c.abc : No active profile set, falling back to default profiles: default
2020-08-10 22:52:15.339 INFO 10011 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-08-10 22:52:15.370 INFO 10011 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23ms. Found 0 JPA repository interfaces.
2020-08-10 22:52:16.292 INFO 10011 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-10 22:52:16.307 INFO 10011 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-10 22:52:16.307 INFO 10011 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-10 22:52:16.495 INFO 10011 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-10 22:52:16.495 INFO 10011 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2180 ms
2020-08-10 22:52:16.886 INFO 10011 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-10 22:52:16.948 INFO 10011 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-08-10 22:52:17.011 WARN 10011 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-08-10 22:52:17.042 INFO 10011 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.18.Final
2020-08-10 22:52:17.378 INFO 10011 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-08-10 22:52:17.724 INFO 10011 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-08-10 22:52:18.251 INFO 10011 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-10 22:52:18.258 INFO 10011 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-08-10 22:52:18.259 INFO 10011 --- [ main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-08-10 22:52:18.299 INFO 10011 --- [ main] a.b.c.abc : Started abc in 4.794 seconds (JVM running for 9.263)
2020-08-10 22:52:18.786 INFO 10011 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-08-10 22:52:18.817 INFO 10011 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect

Can anyone help me please?

</details>


# 答案1
**得分**: 1

以下是翻译好的内容:

1. 运行类应该具有`@SpringBootApplication`,或者至少其中一个配置类应该具有`@EnableAutoConfiguration`。
2. 您不需要将tomcat作为依赖项。
3. 您可以更新您的pom文件以显示Spring Boot的父级。

<details>
<summary>英文:</summary>

it might be helpful to do the following:


 1. Running class should have `@SpringBootApplication` or at least one
    of your Configuration classes should have `@EnableAutoConfiguration`
    over it.
 2. You don&#39;t need tomcat as a dependency.
 3. Can you update your pom file to show us Spring Boot parent as well?





</details>



# 答案2
**得分**: 0

我终于解决了。无法创建表格,因为我没有使用一对一关系来设置外键。

<details>
<summary>英文:</summary>

I finally solve it. Tables could not be created because I hadn`t used One-to-One relationship for foreign key.

</details>



huangapple
  • 本文由 发表于 2020年8月11日 02:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63345780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定