CodexBloom - Programming Q&A Platform

Maven scenarios to include transitive dependencies when using the 'runtime' scope with JPA 2.2

👀 Views: 1324 💬 Answers: 1 📅 Created: 2025-07-02
maven hibernate jpa dependencies Java

I'm not sure how to approach I've been banging my head against this for hours... I'm sure I'm missing something obvious here, but I'm stuck on something that should probably be simple... I'm experiencing an scenario where my Maven build does not include transitive dependencies when I set them to 'runtime' scope, which is causing runtime errors in my JPA 2.2 application. I've defined a dependency on Hibernate ORM as follows: ```xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.30.Final</version> <scope>runtime</scope> </dependency> ``` However, when I run my application, I get `java.lang.NoClassDefFoundError: org/hibernate/HibernateException`. I've verified that Hibernate itself is included in my final artifact but it seems that some of its transitive dependencies, like `javax.persistence:javax.persistence-api`, are missing. To troubleshoot this, I’ve tried changing the scope to 'compile', which resolves the scenario, but I want to understand why 'runtime' is not working as expected. I also checked the dependency tree using `mvn dependency:tree` and noticed that `javax.persistence` is not included under Hibernate when 'runtime' is used. Additionally, I attempted to explicitly add the JPA API dependency like this: ```xml <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> <scope>runtime</scope> </dependency> ``` But it still does not resolve the scenario. Can someone explain why transitive dependencies are not being loaded with 'runtime' scope and if there is a specific configuration I might be missing that could help rectify this? My development environment is Ubuntu. Is there a better approach? Thanks in advance! I'm working on a application that needs to handle this. Is there a better approach? I'm on Debian using the latest version of Java. I'm on CentOS using the latest version of Java.