skip to Main Content

i am currentky working on a JAX-RS application using PostgreSQL as the database and Hibernate for ORM. The application is containerized using Docker, with separate services for the Postgres database and the Tomcat server. Although my application starts and is accessible via Postman, Hibernate does not create any tables in the database, and there are no logs indicating an attempt to do so.

Here is my 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>

Here’s my 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

And my Dockerfile for the Tomcat service:

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

I’ve checked that the persistence.xml file is located in the META-INF directory within the classpath. The database is accessible from intellij on localhost and i used the service name of it in the persistence.xml file since both containers are in the same network in docker. Hibernate’s show_sql property is set to true, but no SQL logs appear.

i have added a get route in the app to see if it is accessible using postman and i can access it without any problems.

Here are some of the logs i get in tomcat when i deploy the app:

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

I suspect there might be an issue with Hibernate entity scanning or configuration within a Dockerized environment, but I’m not sure about that. Does anyone have suggestions on how to solve this issue?

2

Answers


  1. Chosen as BEST ANSWER

    The setup appeared to be correct, but Tomcat would not create the tables in the database unless the entity manger was somehow invoked. Thank you for your assistance.


  2. It looks you need to update the spring.jpa.hibernate.ddl-auto property as well. You can use the value, create-drop or update depending on your use case.

    More info on the property spring.jpa.hibernate.ddl-auto.


    Without Spring, the hibernate property would be hibernate.hbm2ddl.auto.

    The options are create-drop, update etc.. This article explained the property and how to use it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search