Hibernate + Spring Boot + PostgreSQL在启动时不自动创建表,没有错误。

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

Hibernate + Spring boot + PostgreSQL not auto Creating table on startup, no errors

问题

我已经开始学习Springboot,但在启动应用程序时遇到了一个问题,Hibernate没有根据我的实体创建表。我正在使用Postgres作为SQL数据库。

我也包含了迄今为止尝试排除故障的步骤。应用程序启动了,但没有创建我的表。

在启动时我没有收到任何错误。

实体类

  1. @Entity
  2. @Table
  3. public class Student {
  4. // ... (你的实体类代码)
  5. }

主应用程序类

  1. @SpringBootApplication
  2. public class SpringbootApplication {
  3. // ... (你的主应用程序类代码)
  4. }

控制器

  1. @RestController
  2. @RequestMapping(path = "api/v1/student")
  3. public class StudentController {
  4. // ... (你的控制器类代码)
  5. }

服务

  1. @Service
  2. public class StudentService {
  3. // ... (你的服务类代码)
  4. }

application.properties

  1. spring.datasource.url=jdbc:postgresql://localhost:5432/student
  2. spring.datasource.username=postgres
  3. spring.datasource.password=admin
  4. spring.jpa.hibernate.ddl-auto=create
  5. spring.jpa.show-sql=true
  6. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
  7. spring.jpa.properties.hibernate.format_sql=true

pom.xml

  1. <!-- 你的pom.xml文件内容 -->

控制台输出

  1. // 你的控制台输出
  1. 我将spring.jpa.hibernate.ddl-autocreate-drop更改为create
  2. 我已确认在IDE中成功连接到我的数据库。
  3. 我更新了项目包的groupIdartifactId,从使用Spring initialzr构建项目时默认的值。
  4. 我已在Stack Overflow深入搜索,解决问题的所有建议似乎已经在我的代码/文件中了。
  5. application.properties文件中包含了logging.level.org.hibernate.SQL=DEBUG,但Hibernate没有输出任何日志。
英文:

I've started learning Springboot and I'm running into an issue when starting my application where Hibernate isn't creating a table from my entity. I'm using Postgres for SQL

I've also included the steps I have taken so far to try and troubleshoot. The application starts up, however it doesn't create my table

I'm not receiving any errors on startup.

Entity class

  1. package com.example.springboot.student;
  2. import javax.persistence.*;
  3. import java.time.LocalDate;
  4. @Entity
  5. @Table
  6. public class Student {
  7. @Id
  8. @SequenceGenerator(
  9. name = &quot;student_sequence&quot;,
  10. sequenceName = &quot;student_sequence&quot;,
  11. allocationSize = 1
  12. )
  13. @GeneratedValue(
  14. strategy = GenerationType.SEQUENCE,
  15. generator = &quot;student_sequence&quot;
  16. )
  17. private Long id;
  18. private String name;
  19. private String email;
  20. private LocalDate dob;
  21. private Integer age;
  22. public Student() {
  23. }
  24. public Student(Long id, String name, String email, LocalDate dob, Integer age) {
  25. this.id = id;
  26. this.name = name;
  27. this.email = email;
  28. this.dob = dob;
  29. this.age = age;
  30. }
  31. public Student(String name, String email, LocalDate dob, Integer age) {
  32. this.name = name;
  33. this.email = email;
  34. this.dob = dob;
  35. this.age = age;
  36. }
  37. public Long getId() {
  38. return id;
  39. }
  40. public void setId(Long id) {
  41. this.id = id;
  42. }
  43. public String getName() {
  44. return name;
  45. }
  46. public void setName(String name) {
  47. this.name = name;
  48. }
  49. public String getEmail() {
  50. return email;
  51. }
  52. public void setEmail(String email) {
  53. this.email = email;
  54. }
  55. public LocalDate getDob() {
  56. return dob;
  57. }
  58. public void setDob(LocalDate dob) {
  59. this.dob = dob;
  60. }
  61. public Integer getAge() {
  62. return age;
  63. }
  64. public void setAge(Integer age) {
  65. this.age = age;
  66. }
  67. @Override
  68. public String toString() {
  69. return &quot;Student{&quot; +
  70. &quot;id=&quot; + id +
  71. &quot;, name=&#39;&quot; + name + &#39;\&#39;&#39; +
  72. &quot;, email=&#39;&quot; + email + &#39;\&#39;&#39; +
  73. &quot;, dob=&quot; + dob +
  74. &quot;, age=&quot; + age +
  75. &#39;}&#39;;
  76. }
  77. }

main application Class

  1. package com.example.springboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class SpringbootApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(SpringbootApplication.class, args);
  8. }
  9. }

Controller

  1. package com.example.springboot.student;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.util.List;
  7. @RestController
  8. @RequestMapping(path = &quot;api/v1/student&quot;)
  9. public class StudentController {
  10. private final StudentService studentService;
  11. @Autowired
  12. public StudentController(StudentService studentService) {
  13. this.studentService = studentService;
  14. }
  15. @GetMapping
  16. public List&lt;Student&gt; getStudents() {
  17. return studentService.getStudents();
  18. }
  19. }

Service

  1. package com.example.springboot.student;
  2. import org.springframework.stereotype.Service;
  3. import java.time.LocalDate;
  4. import java.time.Month;
  5. import java.util.List;
  6. @Service
  7. public class StudentService {
  8. public List&lt;Student&gt; getStudents() {
  9. return List.of(
  10. new Student(
  11. 1L,
  12. &quot;Mariam&quot;,
  13. &quot;mariam.jamal@gmail.com&quot;,
  14. LocalDate.of(2000, Month.JANUARY, 5),
  15. 21)
  16. );
  17. }
  18. }

application.properties

  1. spring.datasource.url=jdbc:postgresql://localhost:5432/student
  2. spring.datasource.username=postgres
  3. spring.datasource.password=admin
  4. spring.jpa.hibernate.ddl-auto=create
  5. spring.jpa.show-sql=true
  6. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
  7. spring.jpa.properties.hibernate.format_sql=true

pom.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  4. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  5. &lt;parent&gt;
  6. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  7. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  8. &lt;version&gt;2.7.9&lt;/version&gt;
  9. &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  10. &lt;/parent&gt;
  11. &lt;groupId&gt;com.example&lt;/groupId&gt;
  12. &lt;artifactId&gt;springboot&lt;/artifactId&gt;
  13. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  14. &lt;name&gt;springboot&lt;/name&gt;
  15. &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  16. &lt;properties&gt;
  17. &lt;java.version&gt;11&lt;/java.version&gt;
  18. &lt;/properties&gt;
  19. &lt;dependencies&gt;
  20. &lt;dependency&gt;
  21. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  22. &lt;artifactId&gt;spring-boot-starter-data-jdbc&lt;/artifactId&gt;
  23. &lt;/dependency&gt;
  24. &lt;dependency&gt;
  25. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  26. &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  27. &lt;/dependency&gt;
  28. &lt;dependency&gt;
  29. &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
  30. &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
  31. &lt;scope&gt;runtime&lt;/scope&gt;
  32. &lt;/dependency&gt;
  33. &lt;dependency&gt;
  34. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  35. &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  36. &lt;scope&gt;test&lt;/scope&gt;
  37. &lt;/dependency&gt;
  38. &lt;dependency&gt;
  39. &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
  40. &lt;artifactId&gt;javax.persistence-api&lt;/artifactId&gt;
  41. &lt;version&gt;2.2&lt;/version&gt;
  42. &lt;scope&gt;provided&lt;/scope&gt;
  43. &lt;/dependency&gt;
  44. &lt;/dependencies&gt;
  45. &lt;build&gt;
  46. &lt;plugins&gt;
  47. &lt;plugin&gt;
  48. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  49. &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  50. &lt;/plugin&gt;
  51. &lt;/plugins&gt;
  52. &lt;/build&gt;
  53. &lt;repositories&gt;
  54. &lt;repository&gt;
  55. &lt;id&gt;clojars&lt;/id&gt;
  56. &lt;name&gt;Clojars&lt;/name&gt;
  57. &lt;url&gt;https://repo.clojars.org/&lt;/url&gt;
  58. &lt;/repository&gt;
  59. &lt;/repositories&gt;
  60. &lt;/project&gt;

Console

  1. . ____ _ __ _ _
  2. /\\ / ___&#39;_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | &#39;_ | &#39;_| | &#39;_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. &#39; |____| .__|_| |_|_| |_\__, | / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v2.7.9)
  8. 2023-03-09 06:30:19.292 INFO 14164 --- [ main] c.e.springboot.SpringbootApplication : Starting SpringbootApplication using Java 11.0.17 on DESKTOP-RL97UQV with PID 14164 (D:\springboot\target\classes started by byoun in D:\springboot)
  9. 2023-03-09 06:30:19.294 INFO 14164 --- [ main] c.e.springboot.SpringbootApplication : No active profile set, falling back to 1 default profile: &quot;default&quot;
  10. 2023-03-09 06:30:19.673 INFO 14164 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
  11. 2023-03-09 06:30:19.679 INFO 14164 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 3 ms. Found 0 JDBC repository interfaces.
  12. 2023-03-09 06:30:19.962 INFO 14164 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
  13. 2023-03-09 06:30:19.968 INFO 14164 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  14. 2023-03-09 06:30:19.968 INFO 14164 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.71]
  15. 2023-03-09 06:30:20.047 INFO 14164 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  16. 2023-03-09 06:30:20.048 INFO 14164 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 717 ms
  17. 2023-03-09 06:30:20.318 INFO 14164 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  18. 2023-03-09 06:30:20.428 INFO 14164 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
  19. 2023-03-09 06:30:20.514 INFO 14164 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path &#39;&#39;
  20. 2023-03-09 06:30:20.524 INFO 14164 --- [ main] c.e.springboot.SpringbootApplication : Started SpringbootApplication in 1.507 seconds (JVM running for 2.216)
  1. I changed my spring.jpa.hibernate.ddl-auto from create-drop to just create
  2. I've confirmed I have a successful connection to my DB from within my IDE
  3. I updated the groupId and artifactId for my project package from the default that came from building the project with Spring initialzr
  4. I have searched the depths of stack overflow and all of the recommendations to resolve issues appear to already be present in my code/files.
  5. Included logging.level.org.hibernate.SQL=DEBUG to application.properties file but no logging is output from Hibernate

答案1

得分: 1

请将以下设置添加到application.properties文件中:

  1. spring.jpa.generate-ddl=true

还请修改pom.xml文件的相应部分,以使用JPA而不是JDBC。

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  3. &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
  4. &lt;/dependency&gt;
英文:

Please add this setting to application.properties:

  1. spring.jpa.generate-ddl=true

Please also change the part of pom.xml for use of JPA instead of JDBC.

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  3. &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
  4. &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2023年3月9日 20:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684840.html
匿名

发表评论

匿名网友

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

确定