英文:
JPA doesn't auto- create tables with OneToMany and ManyToOne relationships
问题
我尝试使用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, 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
这是我翻译的内容。如果您有更多问题,请随时问。
英文:
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!
答案1
得分: 1
因为dll-auto在没有Jpa或Crud存储库的情况下无法正常工作,所以我创建了它们,然后它开始工作。
英文:
The reason was that dll-auto doesn't work without Jpa or Crud repository so i created them and it started to work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论