JPA不会自动创建具有OneToMany和ManyToOne关系的表

问题描述

我尝试使用Spring Boot Jpa自动创建表,但是它不起作用,它不执行任何sql查询。我怀疑这是由于OneToMany或ManytoOne关系造成的。我查了一下网站,但找不到与类似问题有关的问题。 Spring不会执行任何查询或创建任何表。

用户可以创建许多帖子,每个帖子都有很多评论等。

这是我的实体:

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,orphanRemoval = true)
    private List<Post> posts = new ArrayList<>();

    @OneToMany(fetch = FetchType.LAZY,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,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",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

应用程序启动日志:

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)

我的错误是什么?谢谢!

解决方法

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...