# Hibernate jar loading

**URL:** <https://discourse.hibernate.org/t/hibernate-jar-loading/11086>\
**Category:** Hibernate ORM\
**Created:** [February 19, 2025, 7:19am UTC](https://discourse.hibernate.org/t/hibernate-jar-loading/11086 "2025-02-19T07:19:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ansh\_Jain](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ansh_jain/32/3848_2.png) [@Ansh\_Jain](https://discourse.hibernate.org/u/Ansh_Jain)\
**Post date:** [February 19, 2025, 7:19am UTC](https://discourse.hibernate.org/t/hibernate-jar-loading/11086/1 "2025-02-19T07:19:03Z")

</div>

Hi, I am using persistence.xml in which i am explicitly specifying the jar, is there a way I can move out the persistence.xml from my project, since i am uploading my project jar onto a server and it is difficult to replace the jar at runtime, or can we give the jar via program while creating entityManagerFactory

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [February 19, 2025, 11:13am UTC](https://discourse.hibernate.org/t/hibernate-jar-loading/11086/2 "2025-02-19T11:13:44Z")

</div>

You can build an `EntityManagerFactory` manually as well through e.g. `PersistenceProviderResolverHolder.getPersistenceProviderResolver().getPersistenceProviders()` and `createContainerEntityManagerFactory()`, where you can pass a custom `PersistenceUnitInfo` essentially representing that persistence.xml.

---

<div class="post-metadata">

**Author:** ![Ansh\_Jain](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ansh_jain/32/3848_2.png) [@Ansh\_Jain](https://discourse.hibernate.org/u/Ansh_Jain)\
**Post date:** [February 19, 2025, 12:08pm UTC](https://discourse.hibernate.org/t/hibernate-jar-loading/11086/3 "2025-02-19T12:08:25Z")

</div>

Hi thanks for the reply,  
can you give me a sample code that works because I tried but it is kinda not working  
below is the implementation that i tried

```auto

File jarFile = new File("jar-path");
		URL jarUrl = jarFile.toURI().toURL();  
		logger.info("Loading JAR URL: " + jarUrl);
		JarPersistenceUnitInfo persistenceUnitInfo = new JarPersistenceUnitInfo("dynamicPU", jarUrl, properties);
		EntityManagerFactory emf = new HibernatePersistenceProvider()
                .createContainerEntityManagerFactory(persistenceUnitInfo, properties);
SessionFactory sessionFactory = emf.unwrap(SessionFactory.class);

```

and this is the custom persistentunitinfo

```auto

package com.bancbox.v4.config;

import jakarta.persistence.SharedCacheMode;
import jakarta.persistence.ValidationMode;
import jakarta.persistence.spi.ClassTransformer;
import jakarta.persistence.spi.PersistenceUnitInfo;
import jakarta.persistence.spi.PersistenceUnitTransactionType;
import javax.sql.DataSource;
import java.net.URL;
import java.util.*;

public class JarPersistenceUnitInfo implements PersistenceUnitInfo {

    private final String persistenceUnitName;
    private final URL jarUrl;
    private final Properties properties;

    public JarPersistenceUnitInfo(String persistenceUnitName, URL jarUrl, Properties properties) {
        this.persistenceUnitName = persistenceUnitName;
        this.jarUrl = jarUrl;
        this.properties = properties;
    }

    @Override
    public String getPersistenceUnitName() {
        return persistenceUnitName;
    }

    @Override
    public String getPersistenceProviderClassName() {
        return "org.hibernate.jpa.HibernatePersistenceProvider";
    }

    @Override
    public PersistenceUnitTransactionType getTransactionType() {
        return PersistenceUnitTransactionType.RESOURCE_LOCAL;
    }

    @Override
    public DataSource getJtaDataSource() {
        return null;
    }

    @Override
    public DataSource getNonJtaDataSource() {
        return null;
    }

    @Override
    public List<String> getMappingFileNames() {
        return Collections.emptyList();
    }

    @Override
    public List<URL> getJarFileUrls() {
        return Collections.singletonList(jarUrl);
    }

    @Override
    public URL getPersistenceUnitRootUrl() {
        return jarUrl;
    }

    @Override
    public List<String> getManagedClassNames() {
        return Collections.emptyList(); // Hibernate will scan the JAR
    }

    @Override
    public boolean excludeUnlistedClasses() {
        return true; 
    }

    @Override
    public Properties getProperties() {
        return properties;
    }

    @Override
    public ClassLoader getClassLoader() {
        return Thread.currentThread().getContextClassLoader();
    }

    @Override
    public ClassLoader getNewTempClassLoader() {
        return getClassLoader();
    }

    @Override
    public SharedCacheMode getSharedCacheMode() {
        return SharedCacheMode.UNSPECIFIED; // ✅ Default safe value
    }

    @Override
    public ValidationMode getValidationMode() {
        return ValidationMode.AUTO; // ✅ Ensures validation runs correctly
    }

    @Override
    public String getPersistenceXMLSchemaVersion() {
        return "2.1"; // ✅ Default to latest JPA version
    }

    @Override
    public void addTransformer(ClassTransformer transformer) {
        // No implementation needed unless you require bytecode transformation
    }
}

```

and this is the error that i am facing

```auto
2025-02-19 17:31:10 [fibernate-API(1)] ERROR - exception -> java.lang.IllegalStateException: ArchiveDescriptor reused; can URLs be processed multiple times?

2025-02-19 17:31:10 [fibernate-API(1)] ERROR - exception -> java.lang.IllegalStateException: ArchiveDescriptor reused; can URLs be processed multiple times?

```

can you tell me what I am doing wrong here?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [February 19, 2025, 1:39pm UTC](https://discourse.hibernate.org/t/hibernate-jar-loading/11086/4 "2025-02-19T13:39:46Z")

</div>

It looks like the JAR might be scanned multiple times, which happens because you’re returning the JAR URL also in `getPersistenceUnitRootUrl()`. Return `null` in `getPersistenceUnitRootUrl()` to make this work.  
If you’re willing to work with Hibernate APIs though, why not use the [`Configuration` API](https://docs.jboss.org/hibernate/orm/6.6/introduction/html_single/Hibernate_Introduction.html#configuration-api)? That should be way easier if you want programmatic configuration.

---

<div class="post-metadata">

**Author:** ![Ansh\_Jain](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ansh_jain/32/3848_2.png) [@Ansh\_Jain](https://discourse.hibernate.org/u/Ansh_Jain)\
**Post date:** [February 19, 2025, 3:21pm UTC](https://discourse.hibernate.org/t/hibernate-jar-loading/11086/5 "2025-02-19T15:21:35Z")

</div>

Well ArchiveDescriptor error is not coming but still when i am trying to persists any entity, I am getting unable to locate persister…

and if I will be using configuration api then how can I send the jar path for entities then?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [February 19, 2025, 3:27pm UTC](https://discourse.hibernate.org/t/hibernate-jar-loading/11086/6 "2025-02-19T15:27:01Z")

</div>

Don’t get me wrong, but you will have to read into the code a bit and read java docs. The `Configuration` class has a `addJar` method which is pretty self-explanatory, but also has good javadoc.

---

<div class="post-metadata">

**Author:** ![Ansh\_Jain](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ansh_jain/32/3848_2.png) [@Ansh\_Jain](https://discourse.hibernate.org/u/Ansh_Jain)\
**Post date:** [February 19, 2025, 3:55pm UTC](https://discourse.hibernate.org/t/hibernate-jar-loading/11086/7 "2025-02-19T15:55:32Z")

</div>

ok Thanks for the feedback, I will look into it again
