hibernate/hibernate-orm · error · HibernateException

No ServiceRegistry was passed to Configuration#buildSessionF

Error message

No ServiceRegistry was passed to Configuration#buildSessionFactory and could not determine how to locate BootstrapServiceRegistry from Configuration instantiation

What it means

When Configuration#buildSessionFactory is used, Hibernate needs a BootstrapServiceRegistry as the anchor. It accepts one directly or unwraps it from a StandardServiceRegistry's parent chain; if the supplied ServiceRegistry is any other implementation, getBootstrapRegistry throws this HibernateException. The registry passed in cannot be traced back to a bootstrap registry, so factory construction is refused.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java:224

	 * Create a new instance, using the given {@link MetadataSources}, and a
	 * {@link BootstrapServiceRegistry} obtained from the {@link MetadataSources}.
	 */
	public Configuration(MetadataSources metadataSources) {
		this.metadataSources = metadataSources;
		bootstrapServiceRegistry = getBootstrapRegistry( metadataSources.getServiceRegistry() );
		standardServiceRegistryBuilder = new StandardServiceRegistryBuilder( bootstrapServiceRegistry );
		properties.putAll( standardServiceRegistryBuilder.getSettings() );
	}

	private static BootstrapServiceRegistry getBootstrapRegistry(ServiceRegistry serviceRegistry) {
		if ( serviceRegistry instanceof BootstrapServiceRegistry bootstrapServiceRegistry ) {
			return bootstrapServiceRegistry;
		}
		else if ( serviceRegistry instanceof StandardServiceRegistry ssr ) {
			return (BootstrapServiceRegistry) ssr.getParentServiceRegistry();
		}

		throw new HibernateException(
				"No ServiceRegistry was passed to Configuration#buildSessionFactory " +
						"and could not determine how to locate BootstrapServiceRegistry " +
						"from Configuration instantiation"
		);
	}


	// properties/settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	/**
	 * Get all properties
	 *
	 * @return all properties
	 */
	public Properties getProperties() {
		return properties;
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Pass a StandardServiceRegistry built via new StandardServiceRegistryBuilder().configure().build()
  2. Or pass the BootstrapServiceRegistry itself when that is what you hold
  3. Do not implement or wrap ServiceRegistry yourself; compose via StandardServiceRegistryBuilder instead
  4. Prefer the modern bootstrap: new MetadataSources(ssr)...buildMetadata().buildSessionFactory()

Example fix

// before
Configuration cfg = new Configuration();
ServiceRegistry myRegistry = customWrapper(); // not a StandardServiceRegistry -> throws
SessionFactory sf = cfg.buildSessionFactory(myRegistry);

// after
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sf = new MetadataSources(ssr).addAnnotatedClass(Item.class)
        .buildMetadata().buildSessionFactory();
Defensive patterns

Strategy: validation

Validate before calling

if (!(registry instanceof StandardServiceRegistry)
        && !(registry instanceof BootstrapServiceRegistry)) {
    throw new IllegalArgumentException(
            "Pass a StandardServiceRegistry or BootstrapServiceRegistry to buildSessionFactory");
}
return cfg.buildSessionFactory(registry);

Type guard

static boolean isRegistryHibernateAccepts(ServiceRegistry registry) {
    return registry instanceof StandardServiceRegistry
            || registry instanceof BootstrapServiceRegistry;
}

Prevention

When it happens

Trigger: Calling cfg.buildSessionFactory(customRegistry) where customRegistry is a wrapper or non-standard ServiceRegistry implementation; passing a framework-produced registry that is neither a BootstrapServiceRegistry nor a StandardServiceRegistry; bootstrap glue that rebuilds Configurations around existing registries.

Common situations: Custom or wrapped registries in framework integration code; legacy Configuration-based bootstrap mixed with new-style registry code; partial migration to StandardServiceRegistryBuilder.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/b5700150b91986b1. Report an issue: GitHub.