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
- Pass a StandardServiceRegistry built via new StandardServiceRegistryBuilder().configure().build()
- Or pass the BootstrapServiceRegistry itself when that is what you hold
- Do not implement or wrap ServiceRegistry yourself; compose via StandardServiceRegistryBuilder instead
- 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
- Use StandardServiceRegistryBuilder as the single registry entry point
- Never wrap or reimplement ServiceRegistry interfaces
- Keep one registry per application lifecycle and close it after the SessionFactory
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
- The {storageEngine} storage engine is not supported
- Could not instantiate event listener '{}'
- Unable to instantiate StatementObserver - {}
- Bootstrap registry should only contain provided services
- illegal value for configuration setting 'hibernate.connectio
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/b5700150b91986b1.
Report an issue: GitHub.