简体   繁体   中英

JPA doesn't auto- create tables with OneToMany and ManyToOne relationships

I try to create tables automatically with Spring Boot Jpa but it doesn't work, it doesn't execute any sql queries. I suspect it's due to the OneToMany or ManytoOne relationships. I looked up the website but failed to find a question with a similar problem. Spring doesn't execute any queries and create any tables.

Users can create many posts and each post has many comments etc.

Here are my entities:

User.java

@Entity
@Table
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long userId;
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Topic> topics = new ArrayList<>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Post> posts = new ArrayList<>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Comment> comments = new ArrayList<>();

Post.java

@Entity
@Table
public class Post {

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long postId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId", referencedColumnName = "userId")
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", referencedColumnName = "id")
    private Topic topic;

    @OneToMany(mappedBy = "post", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Comment> comments = new ArrayList<>();

Comment.java

@Entity
@Table
public class Comment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "postId", referencedColumnName = "postId")
    private Post post;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId", referencedColumnName = "userId")
    private User user;

Topic.java

@Entity
@Table
public class Topic {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "topic", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Post> posts = new ArrayList<>();

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId", referencedColumnName = "userId")
    private User user;

application.properties

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/portfolio2?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=ecommerceapp
    spring.datasource.password=ecommerceapp
    spring.jpa.generate-ddl=true
    
    spring.jpa.hibernate.ddl-

auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true

application start logs:

2020-10-17 18:56:17.694  INFO 2196 --- [           main] c.j.s.SpringPortfolioApplication         : Starting SpringPortfolioApplication on Laptop with PID 2196 (C:\Users\UserOld.Laptop\IdeaProjects\spring-portfolio\target\classes started by UserOld in C:\Users\UserOld.Laptop\IdeaProjects\spring-portfolio)
2020-10-17 18:56:17.694  INFO 2196 --- [           main] c.j.s.SpringPortfolioApplication         : No active profile set, falling back to default profiles: default
2020-10-17 18:56:18.147  WARN 2196 --- [kground-preinit] o.s.h.c.j.Jackson2ObjectMapperBuilder    : For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
2020-10-17 18:56:18.553  INFO 2196 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-10-17 18:56:18.585  INFO 2196 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23ms. Found 0 JPA repository interfaces.
2020-10-17 18:56:19.303  INFO 2196 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-10-17 18:56:19.319  INFO 2196 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-17 18:56:19.319  INFO 2196 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-17 18:56:19.491  INFO 2196 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-17 18:56:19.491  INFO 2196 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1735 ms
2020-10-17 18:56:19.663  INFO 2196 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-17 18:56:19.699  INFO 2196 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-10-17 18:56:19.730  WARN 2196 --- [           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-10-17 18:56:19.730  INFO 2196 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-10-17 18:56:19.855  INFO 2196 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-10-17 18:56:19.949  INFO 2196 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-10-17 18:56:20.011  WARN 2196 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2020-10-17 18:56:20.105  INFO 2196 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-10-17 18:56:20.121  INFO 2196 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-10-17 18:56:20.168  INFO 2196 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-17 18:56:20.168  INFO 2196 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-10-17 18:56:20.168  INFO 2196 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-10-17 18:56:20.183  INFO 2196 --- [           main] c.j.s.SpringPortfolioApplication         : Started SpringPortfolioApplication in 2.932 seconds (JVM running for 3.878)

What is my mistake? Thank you!

原因是 dll-auto 在没有 Jpa 或 Crud 存储库的情况下无法工作,所以我创建了它们并开始工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM