当前位置: 首页 > 知识库问答 >
问题:

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

丌官盛
2023-03-14

我尝试用Spring Boot Jpa自动创建表,但它不起作用,它不执行任何sql查询。我怀疑这是由于一个人或多个人的关系。我查了一下网站,但没能找到一个有类似问题的问题。Spring不执行任何查询和创建任何表。

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

@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<>();

注释.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;
    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)

我错在哪里?谢谢你!

共有1个答案

谢嘉
2023-03-14

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

 类似资料:
  • Javers默认忽略而不是默认包含

  • 问题内容: 我目前正在阅读有关实体关联的Hibernate文档,但遇到一些困难却难以理解。它在本质上做的区别和联系。尽管我在实际项目中使用了它们,但是我无法完全理解它们之间的差异。据我了解,如果一个表/一个实体与另一个实体有关联,则该关联应来自另一侧。那么,我们应该如何根据具体情况决定选择哪个呢?它又如何影响数据库/查询/结果?到处都有很好的例子吗? PS:我认为这与问题相关,如果有人可以解释关联

  • 我试图定义两个类之间的双向关系。拥有方是类测验,相反方是用户。一个用户可以创建许多测验,而一个测验只能有一个创建它的用户。我在网上找到的每一个教程都指出,在owning方面,您指定了ManyToOne注释和JoinColumn,在相反的方面,您使用owners字段的名称指定了OneToMany和mappedBy。然而,当我这样做时,IDE给了我一个错误“找不到逆关系”。我在这个概念上哪里出错了?如

  • 我在将带有@ManyToOne关系的实体(雇员)映射到带有@OneToMany关系的实体(社区)时遇到了问题。 当我将一个实体(社区)分配给一个实体(员工),其中社区的值为空时,它工作正常。 问题出现了,如果雇员已经为社区分配了价值。当我更改该值并保存更改时,该员工为社区获得了新的值,并且这个新社区在集合中获得了该员工。唯一的问题是,老社区仍然有这个员工在收集,但它应该被删除。这只有在数据库重新启

  • 我正试图阻止我的关系重新开始。我尝试了多种方法来解决这个问题,但似乎每次都有一个错误。例如,当我尝试以下代码时: 我得到以下错误: 我已经检查了数据库中的所有表和索引,但在任何地方都找不到此约束。我该如何移除它。我基本上希望我的模式是这样的: 今年将有一份所有学生、老师的名单。当学生注册时,他们将被添加到该学年中 如果我不添加连接列,我只是得到另一个表说 Year.students 我如何将这些结

  • 我有三个班,站点,GoupIP和IP 一个站点有一个或多个抱怨。GroupIP有一个或多个IP。 代码如下: 地点 群居 IP 在GroupIp课堂上,我得到: 在属性“ips”中,“映射者”值“groupip”无法解析为目标实体上的属性。 我的代码出了什么问题??