我的Spring Boot应用程序连接到PostgreSQL数据库,但是它没有创建我已经实现为实体的表。这就是为什么我自己用测试数据设置表的原因。
当我使用dataRepository.findAll()访问数据时,它返回一个空列表。
据我所知,我认为它已连接到我的数据库,因为我没有收到任何错误,但看起来这些架构无法正常工作。
我很确定这是一个“配置”问题,但我无法弄清楚它是什么。
PS:我有另一个项目,其设置和代码完全相同(只是同一服务器上的另一个数据库),效果很好。
我的gradle.build文件:
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
group = 'de.dari'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '8'
repositories {
mavenCentral()
maven { url "http://htmlunit.sourceforge.net" }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
//implementation 'org.springframework.boot:spring-boot-starter-data-rest'
compile("net.sourceforge.htmlunit:htmlunit:2.35.0")
compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
我的application.properties文件:
spring.datasource.url=jdbc:postgresql://webaddress.com:5432/b-one
spring.datasource.username=username
spring.datasource.password=secretPassword
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
示例实体:
@Entity
public class Data {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String value;
/*
* Blank Constructor + Getters and Setters down here
*/
}
简单的CRUD存储库:
public interface DataRepository extends CrudRepository<Data, Long> {
}
控制器:
@Autowired
DataRepository dataRepository;
@GetMapping("/test")
public String test() {
return dataRepository.findAll().toString();
}
是的,所以我没有收到任何错误消息,并且日志对我没有帮助,但是也许您想一窥它们。
控制台输出:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) 2019-08-26 16:23:53.225 INFO 10220 --- [ main] de.dari.bone.REST.Application : Starting Application on GamingPC with PID 10220 (started by Deniz Cuhadari in C:\Users\Deniz Cuhadari\OneDrive - inc\Projects\b-one\b-one.REST) 2019-08-26 16:23:53.230 INFO 10220 --- [ main] de.dari.bone.REST.Application : No active profile set, falling back to default profiles: default 2019-08-26 16:23:54.083 INFO 10220 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode. 2019-08-26 16:23:54.202 INFO 10220 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 110ms. Found 6 repository interfaces. 2019-08-26 16:23:54.568 INFO 10220 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$24d7df9e] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2019-08-26 16:23:54.878 INFO 10220 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-08-26 16:23:54.907 INFO 10220 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-08-26 16:23:54.908 INFO 10220 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21] 2019-08-26 16:23:55.057 INFO 10220 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-08-26 16:23:55.057 INFO 10220 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1757 ms 2019-08-26 16:23:55.226 INFO 10220 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2019-08-26 16:23:55.650 INFO 10220 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2019-08-26 16:23:55.711 INFO 10220 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ name: default ...] 2019-08-26 16:23:55.793 INFO 10220 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final} 2019-08-26 16:23:55.795 INFO 10220 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found 2019-08-26 16:23:55.947 INFO 10220 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final} 2019-08-26 16:23:56.098 INFO 10220 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect 2019-08-26 16:24:02.271 INFO 10220 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000421: Disabling contextual LOB creation as hibernate.jdbc.lob.non_contextual_creation is true 2019-08-26 16:24:02.278 INFO 10220 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@7e49ded 2019-08-26 16:24:03.329 INFO 10220 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2019-08-26 16:24:03.946 INFO 10220 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-08-26 16:24:03.987 WARN 10220 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : 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 2019-08-26 16:24:04.051 INFO 10220 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html] 2019-08-26 16:24:04.162 INFO 10220 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-08-26 16:24:04.165 INFO 10220 --- [ main] de.dari.bone.REST.Application : Started Application in 11.503 seconds (JVM running for 12.75) 2019-08-26 16:24:09.762 INFO 10220 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-08-26 16:24:09.762 INFO 10220 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-08-26 16:24:09.769 INFO 10220 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms 2019-08-26 16:24:09.905 INFO 10220 --- [nio-8080-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory