Hibernate不使用Dockerized Tomcat在Postgres中创建表

ergxz8rk  于 2023-11-17  发布在  Docker
关注(0)|答案(2)|浏览(116)

我目前正在使用PostgreSQL作为数据库和Hibernate作为ORM的JAX-RS应用程序。该应用程序使用Docker进行容器化,为Postgres数据库和Tomcat服务器提供单独的服务。虽然我的应用程序可以通过Postman启动和访问,但Hibernate不会在数据库中创建任何表,也没有日志表明尝试这样做。
下面是我的persistence.xml:

<persistence version="2.2"
         xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">

<persistence-unit name="todoAppPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.jchaaban.javaeerestapp.entity.User</class>
    <class>com.jchaaban.javaeerestapp.entity.Todo</class>

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
        <property name="jakarta.persistence.schema-generation.database.action" value="create"/>
        <property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://db:5432/postgres"/>
        <property name="jakarta.persistence.jdbc.user" value="postgres"/>
        <property name="jakarta.persistence.jdbc.password" value="admin"/>
        <property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.use_sql_comments" value="true"/>
    </properties>

</persistence-unit>

字符串
这是我的docker-compose

db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: admin
      POSTGRES_DB: postgres
    ports:
      - "5432:5432"
    volumes:
      - ./volumes/postgres/db/data:/var/lib/postgresql/data
    networks:
      - rest-app

  tomcat:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      - db
    networks:
      - rest-app


我的Tomcat服务的Dockerfile:

RUN rm -rf /usr/local/tomcat/webapps/ROOT
COPY target/restapp.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["catalina.sh", "run"]


我已经检查过persistence.xml文件位于类路径中的META-INF目录中。数据库可以从localhost上的intellij访问,并且我在persistence.xml文件中使用了它的服务名称,因为两个容器在docker中位于同一个网络中。Hibernate的show_sql属性设置为true,但没有SQL日志出现。
我已经在应用程序中添加了一个获取路线,看看它是否可以使用 Postman 访问,我可以访问它没有任何问题。
以下是我在部署应用程序时在tomcat中获得的一些日志:

java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.817 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.817 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.817 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.817 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.817 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.817 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.820 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.35] using APR version [1.7.0].
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.820 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.820 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.821 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1n  15 Mar 2022]
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.964 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:07.977 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [269] milliseconds
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:08.003 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:08.003 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.65]
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:08.010 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/usr/local/tomcat/webapps/ROOT.war]
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:08.208 WARNING [main] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [5.0]. Default version will be used.
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:08.680 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:08.975 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/usr/local/tomcat/webapps/ROOT.war] has finished in [965] ms
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:08.978 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
java-ee-rest-app-tomcat-1  | 07-Nov-2023 03:01:08.983 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1006] milliseconds


我怀疑在Dockerized环境中可能存在Hibernate实体扫描或配置问题,但我不确定。有人对如何解决这个问题有建议吗?

yzxexxkh

yzxexxkh1#

看起来你也需要更新spring.jpa.hibernate.ddl-auto属性。你可以根据你的用例使用值create-dropupdate
更多关于spring.jpa.hibernate.ddl-auto的信息。
如果没有Spring,hibernate属性将是hibernate.hbm2ddl.auto
选项有create-dropupdate等。This article explained the property and how to use it.

rta7y2nd

rta7y2nd2#

设置似乎是正确的,但Tomcat不会在数据库中创建表,除非以某种方式调用实体管理器。谢谢你的帮助。

相关问题