Hibernate search 6.2 not able to open nested Jars

I’m trying to run a Spring boot 2.7.9 application, with Hibernate search 6.2.0.Final.

Dependencies:

		<dependency>
			<groupId>org.hibernate.search</groupId>
			<artifactId>hibernate-search-mapper-orm</artifactId>
			<version>6.2.0.Final</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate.search</groupId>
			<artifactId>hibernate-search-backend-elasticsearch</artifactId>
			<version>6.2.0.Final</version>
		</dependency>

But i’m getting this exception after packaging the application and try to run it.

Caused by: org.hibernate.search.util.common.SearchException: HSEARCH900014: Exception while building Jandex index for 'jar:file:/C:/Users/paulo/a/repo/app/paulo-app-application/target/caps-paulo-app-application.jar!/BOOT-INF/lib/paulo-app-repository-aurora-0.0.1-SNAPSHOT.jar!/': HSEARCH900016: Cannot open filesystem for code source at 'jar:file:/C:/Users/paulo/a/repo/app/paulo-app-application/target/caps-paulo-app-application.jar!/BOOT-INF/lib/paulo-app-repository-aurora-0.0.1-SNAPSHOT.jar!/': HSEARCH900018: Cannot open a ZIP filesystem for code source at 'jar:file:/C:/Users/paulo/a/repo/app/paulo-app-application/target/caps-paulo-app-application.jar!/BOOT-INF/lib/paulo-app-repository-aurora-0.0.1-SNAPSHOT.jar!/', because the URI points to content inside a nested JAR.
        at org.hibernate.search.util.common.jar.impl.JandexUtils.readOrBuildIndex(JandexUtils.java:109)
        ... 50 common frames omitted
Caused by: java.io.IOException: HSEARCH900016: Cannot open filesystem for code source at 'jar:file:/C:/Users/paulo/a/repo/app/paulo-app-application/target/caps-paulo-app-application.jar!/BOOT-INF/lib/paulo-app-repository-aurora-0.0.1-SNAPSHOT.jar!/': HSEARCH900018: Cannot open a ZIP filesystem for code source at 'jar:file:/C:/Users/paulo/a/repo/app/paulo-app-application/target/caps-paulo-app-application.jar!/BOOT-INF/lib/paulo-app-repository-aurora-0.0.1-SNAPSHOT.jar!/', because the URI points to content inside a nested JAR.
        at org.hibernate.search.util.common.jar.impl.CodeSource.initFileSystem(CodeSource.java:131)
        at org.hibernate.search.util.common.jar.impl.CodeSource.classesPathOrFail(CodeSource.java:97)
        at org.hibernate.search.util.common.jar.impl.JandexUtils.readOrBuildIndex(JandexUtils.java:106)
        ... 50 common frames omitted
Caused by: java.io.IOException: HSEARCH900018: Cannot open a ZIP filesystem for code source at 'jar:file:/C:/Users/paulo/a/repo/app/paulo-app-application/target/caps-paulo-app-application.jar!/BOOT-INF/lib/paulo-app-repository-aurora-0.0.1-SNAPSHOT.jar!/', because the URI points to content inside a nested JAR.
        at org.hibernate.search.util.common.jar.impl.CodeSource.tryInitJarFileSystem(CodeSource.java:146)
        at org.hibernate.search.util.common.jar.impl.CodeSource.initFileSystem(CodeSource.java:108)
        ... 52 common frames omitted

Seaching the Hibernate search code, i found inside CodeSource class, the code:

private void tryInitJarFileSystem(URI jarUri) throws IOException {
		try {
			nonDefaultFileSystem = FileSystems.newFileSystem( jarUri, Collections.emptyMap() );
			classesPathInFileSystem = nonDefaultFileSystem.getRootDirectories().iterator().next();
			// The ZipFileSystemProvider ignores the "path inside the JAR",
			// so we need to take care of that ourselves.
			String nestedPath = extractedJarNestedPath( jarUri );
			if ( nestedPath != null ) {
				Path nestedPathInFileSystem = classesPathInFileSystem.resolve( nestedPath );
				if ( Files.isRegularFile( nestedPathInFileSystem ) ) {
					// TODO HSEARCH-4744 support reading the content of nested JARs
					throw log.cannotOpenNestedJar( jarUri );
				}
				classesPathInFileSystem = nestedPathInFileSystem;
			}
		}
		catch (RuntimeException | IOException e) {
			new SuppressingCloser( e )
					.push( nonDefaultFileSystem );
			nonDefaultFileSystem = null;
			classesPathInFileSystem = null;
			throw e;
		}
	}

There is a TODO:
// TODO HSEARCH-4744 support reading the content of nested JARs

Linked to a backlog Story:
https://hibernate.atlassian.net/browse/HSEARCH-4744

Is there a way to use Hibernate search 6.2 with nested JARs?
Or is there any predictions of when this issue is going to be solved?

Hey @pauloconsoni thanks for reaching out. Please see the answers below:

the linked ticket suggests that to be able to implement it we’d need to move to Java 13+. based on the current sate/plans – Hibernate Search 6.2 is Java 8 based, Hibernate Search 7 will be based on Java 11, and only 8 will be targeting Java 17 as a baseline. So it is unlikely that it’ll be ready soon.

You could try to move away from the Spring’s plugin for packaging and use for example, a maven-shade-plugin to include the dependencies.

You could disable that part of the code that tries to read the jars. To do so, you’d need to set hibernate.search.mapping.process_annotations=false (see Hibernate Search 6.2.2.Final: Reference Documentation and Hibernate Search 6.2.2.Final: Reference Documentation) but then you might need to do some programmatic mapping depending on what particular search model you are planning to use.
Alternatively, you could provide your mapping configurer and only disable the discoverAnnotatedTypesFromRootMappingAnnotations (see the example here Hibernate Search 6.2.2.Final: Reference Documentation)

Use solve by doing like this :

  • properties yml file :
    hibernate.mapping.process_annotations: false
    hibernate.mapping.configurer: com.fallphenix.persistance.common.HibernateSearchMappingConfigurer

  • Implementation

import org.hibernate.search.mapper.orm.mapping.HibernateOrmMappingConfigurationContext;
import org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer;
import org.springframework.stereotype.Component;

public class HibernateSearchMappingConfigurer implements HibernateOrmSearchMappingConfigurer {

    @Override
    public void configure(HibernateOrmMappingConfigurationContext context) {
        context.annotationMapping()
                .add(Object1Entity.class)
                .add(Object2Entity.class)
                .add(ObjectNEntity.class)
                .discoverAnnotationsFromReferencedTypes(true)
                .discoverAnnotatedTypesFromRootMappingAnnotations(true);
    }
}

Verions bellow :

<java.version>17</java.version>
<spring.boot.version>3.1.1</spring.boot.version>
 <hibernate.version>6.2.6.Final</hibernate.version>
<hibernate.search.version>6.2.0.Final</hibernate.search.version>

Hey,

FWIW this should only affect applications packaged as nested JARs. HSEARCH-4744 should eventually address the problem, but the solution requires JDK 13+.

By the way, there’s no need to completely disable annotation processing. This should be enough:

  • properties.yml file:
hibernate.mapping.configurer: com.fallphenix.persistance.common.HibernateSearchMappingConfigurer
    • Implementation
import org.hibernate.search.mapper.orm.mapping.HibernateOrmMappingConfigurationContext;
import org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer;

@Component
public class HibernateSearchMappingConfigurer implements HibernateOrmSearchMappingConfigurer {

    @Override
    public void configure(HibernateOrmMappingConfigurationContext context) {
        context.annotationMapping()
                .discoverAnnotatedTypesFromRootMappingAnnotations(false);
    }
}

See also Hibernate Search 6.2.0.Final: Reference Documentation

Hi,

We have run into the same issue, the above fix does not seem to work. ALthough the configurer as specified by @yrodiere is run. Do I still need to explicitly add the entities that are inside the nested JAR ?

many thanks

Hi,

[HSEARCH-4744] - Hibernate JIRA was fixed in the recently released version 6.2.1.Final, as long as you use Java 13+ (Spring Boot 3 requires Java 17+ anyway).

If you still see this bug on Java 13+ with Hibernate Search version 6.2.1.Final or later, please open an issue on Jira with a reproducer.

2 Likes

Many thanks - it is indeed working with 6.2.1.Final !

2 Likes