如何让springjpa自动创建关系/表?

8gsdolmq  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(168)

我试图让spring为我的实体类生成表。我不确定我遗漏了什么,这是我第一次使用集装箱数据库。
这是我的 application.yml 文件:

spring.datasource:
  url: jdbc:postgresql://localhost:8300/fishgame
  username: admin
  password: admin
  driver-class-name: org.postgresql.Driver
jpa:
  show-sql: true
  hibernate:
    ddl-auto: create
  generate-ddl: true
  database-platform: org.hibernate.dialect.PostgreSQLDialect

我的 docker-compose 文件:

version: "3.8"
services:
  database:
    image: "postgres:13"
    ports:
    - "8300:5432"
    volumes:
      - /tmp/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=admin
      - POSTGRES_USER=admin
      - POSTGRES_DB=fishgame

我的实体类:

@Data
@Entity
@Builder
@Table(name = "fishes")
@NoArgsConstructor
@AllArgsConstructor
public class Fish  extends BaseEntity {

    private Species species;
    /**
     * Pixel percentage / second ?
     */
    private double speed;
    @Embedded
    private Position position;

}

当我试图拯救一个 Fish 实体i出现以下错误:
路径为[]的上下文中servlet[dispatcherservlet]的servlet.service()引发异常[请求处理失败;嵌套异常为org.springframework.dao.invaliddataaccessresourceusageexception:无法执行语句;sql[不适用];嵌套异常为org.hibernate.exception.sqlgrammareexception:无法执行具有根本原因的[statement]
org.postgresql.util.psqlexception:错误:关系“fishes”不存在位置:13位于org.postgresql.core.v3.queryexecutorimpl.receiveerrorresponse(queryexecutorimpl)。java:2553)~[postgresql-42.2.18。jar:42.2.18]在org.postgresql.core.v3.queryexecutorimpl.processresults(queryexecutorimpl。java:2285)~[postgresql-42.2.18。jar:42.2.18]在org.postgresql.core.v3.queryexecutorimpl.execute(queryexecutorimpl。java:323)~[postgresql-42.2.18。jar:42.2.18]位于org.postgresql.jdbc.pgstatement.executeinternal(pgstatement。java:473)~[postgresql-42.2.18。jar:42.2.18]在org.postgresql.jdbc.pgstatement.execute(pgstatement。java:393)~[postgresql-42.2.18。jar:42.2.18]在org.postgresql.jdbc.pgpreparedstatement.executewithflags(pgpreparedstatement。java:164)~[postgresql-42.2.18。jar:42.2.18]位于org.postgresql.jdbc.pgpreparedstatement.executeupdate(pgpreparedstatement)。java:130)~[postgresql-42.2.18。jar:42.2.18]在com.zaxxer.hikari.pool.proxypreparedstatement.executeupdate(proxypreparedstatement。java:61) [hikaricp-3.4.5。jar:na]在com.zaxxer.hikari.pool.hikariproxypreparedstatement.executeupdate(hikariproxypreparedstatement.java)~[hikaricp-3.4.5。jar:na]在org.hibernate.engine.jdbc.internal.resultsetreturnimpl.executeupdate(resultsetreturnimpl。java:197)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister。java:3301)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister。java:3828)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.action.internal.entityinsertaction.execute(entityinsertaction)。java:107)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.engine.spi.actionqueue.executeactions(actionqueue。java:604)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.engine.spi.actionqueue.lambda$executeactions$1(actionqueue。java:478) ~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在java.util.linkedhashmap.foreach(linkedhashmap。java:684)~[na:1.8.0\u232]在org.hibernate.engine.spi.actionqueue.executeactions(actionqueue。java:475)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.event.internal.abstractflushingeventlistener.performexecutions(abstractflushingeventlistener。java:348)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.event.internal.defaultflusheventlistener.onflush(defaultflusheventlistener。java:40)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.event.service.internal.eventlistenergroupimpl.fireeventoneachlistener(eventlistenergroupimpl。java:102)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.internal.sessionimpl.doflush(sessionimpl。java:1362)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.internal.sessionimpl.managedflush(sessionimpl。java:453)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.internal.sessionimpl.flushbeforetransactioncompletion(sessionimpl。java:3212)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.internal.sessionimpl.beforetransactioncompletion(sessionimpl。java:2380) ~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.engine.jdbc.internal.jdbcoordinatorimpl.beforetransactioncompletion(jdbcoordinatorimpl。java:447)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl.beforecompletioncallback(jdbcresourcelocaltransactioncoordinatorimpl。java:183)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl.access$300(jdbcresourcelocaltransactioncoordinatorimpl。java:40)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl$transactiondrivercontrolimpl.commit(jdbcresourcelocaltransactioncoordinatorimpl。java:281)~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.hibernate.engine.transaction.internal.transactionimpl.commit(transactionimpl。java:101) ~[hibernate-core-5.4.25.最终版本。jar:5.4.25.final]在org.springframework.orm.jpa.jpatransactionmanager.docommit(jpatransactionmanager。java:562)~[spring-orm-5.3.2。jar:5.3.2]位于org.springframework.transaction.support.abstractplatformtransactionmanager.processcommit(abstractplatformtransactionmanager)。java:743)~[spring-tx-5.3.2。jar:5.3.2]在org.springframework.transaction.support.abstractplatformtransactionmanager.commit(abstractplatformtransactionmanager)。java:711)~[spring-tx-5.3.2。jar:5.3.2]位于org.springframework.transaction.interceptor.transactionspectsupport.committransactionafterreturning(transactionspectsupport)。java:654)~[spring-tx-5.3.2。jar:5.3.2]在org.springframework.transaction.interceptor.transactionspectsupport.invokewithintransaction(transactionspectsupport。java:407)~[spring-tx-5.3.2。jar:5.3.2]在org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor。java:119)~[spring-tx-5.3.2。jar:5.3.2]在org.springframework.aop.framework.reflectivemethodinvocation.procedue(reflectivemethodinvocation。java:186)~[spring-aop-5.3.2。jar:5.3.2]在org.springframework.aop.framework.cglibaopproxy$cglibmethodinvocation.procedue(cglibaopproxy。java:749)~[spring-aop-5.3.2。jar:5.3.2]在org.springframework.aop.framework.cglibaopproxy$dynamicadvisedinterceptor.intercept(cglibaopproxy。java:691)~[spring-aop-5.3.2。jar:5.3.2]在com.vargaerik.fishgame.fish.service.fishservice$$enhancerbyspringcglib$$ef554122.save()~[classes/:na]在com.vargaerik.fishgame.fish.controller.fishcontroller.streamfish(fishcontroller。java:33) ~[classes/:na]位于sun.reflect.nativemethodaccessorimpl.invoke0(本机方法)~[na:1.8.0\u 232]位于sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl)。java:62)~[na:1.8.0232]在sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)~[na:1.8.0\u232]位于java.lang.reflect.method.invoke(method。java:498) ~[na:1.8.0\u232]位于org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod)。java:197)~[spring-web-5.3.2。jar:5.3.2]在org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod。java:141)~[spring-web-5.3.2。jar:5.3.2]在org.springframework.web.servlet.mvc.method.annotation.servletinvacableHandlerMethod.invokeandhandle(servletinvacableHandlerMethod)。java:106)~[spring-webmvc-5.3.2。jar:5.3.2]在org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter。java:894) ~[spring-webmvc-5.3.2。jar:5.3.2]位于org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter。java:808)~[spring-webmvc-5.3.2。jar:5.3.2]位于org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter)。java:87) ~[spring-webmvc-5.3.2。jar:5.3.2]在org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet。java:1061)~[spring-webmvc-5.3.2。jar:5.3.2]位于org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet。java:961)~[spring-webmvc-5.3.2。jar:5.3.2]在org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet。java:1006)~[spring-webmvc-5.3.2。jar:5.3.2]位于org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet。java:898)~[spring-webmvc-5.3.2。jar:5.3.2]在javax.servlet.http.httpservlet.service(httpservlet。java:626)~[tomcat-embed-core-9.0.41。jar:4.0.fr]在org.springframework.web.servlet.frameworkservlet.service(frameworkservlet。java:883)~[spring-webmvc-5.3.2。jar:5.3.2]在javax.servlet.http.httpservlet.service(httpservlet。java:733)~[tomcat-embed-core-9.0.41。jar:4.0.fr]位于org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:231) ~[tomcat-embed-core-9.0.41。jar:9.0.41]在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:166)~[tomcat-embed-core-9.0.41。jar:9.0.41]在org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter。java:53)~[tomcat-embed-websocket-9.0.41。jar:9.0.41]在org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:193)~[tomcat-embed-core-9.0.41。jar:9.0.41]在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:166)~[tomcat-embed-core-9.0.41。jar:9.0.41]在org.springframework.web.filter.requestcontextfilter.dofilterinternal(requestcontextfilter。java:100)~[spring-web-5.3.2。jar:5.3.2]在org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter。java:119)~[spring-web-5.3.2。jar:5.3.2]在org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:193)~[tomcat-embed-core-9.0.41。jar:9.0.41]在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:166)~[tomcat-embed-core-9.0.41。jar:9.0.41]在org.springframework.web.filter.formcontentfilter.dofilterinternal(formcontentfilter。java:93)~[spring-web-5.3.2。jar:5.3.2]在org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter。java:119)~[spring-web-5.3.2。jar:5.3.2]位于org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:193) ~[tomcat-embed-core-9.0.41。jar:9.0.41]位于org.apache.catalina.core.applicationfilterchain.dofilt

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题