hibernate/hibernate-orm · error · InvalidObjectException

No SessionFactory with uuid [{uuid}] and name [{name}]

Error message

No SessionFactory with uuid [{uuid}] and name [{name}]

What it means

Sessions (and the factory handle) serialize only a uuid+name reference; on deserialization readResolve() looks the SessionFactory up in SessionFactoryRegistry, first by uuid, then by name. If no factory with that uuid/name exists in the current JVM, java.io.InvalidObjectException is thrown. The design assumes the original factory is still alive wherever the Session is deserialized.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java:1547

			if ( SESSION_FACTORY_LOGGER.isTraceEnabled() ) {
				SESSION_FACTORY_LOGGER.resolvedFactoryByUuid( uuid );
			}
			return uuidResult;
		}

		// in case we were deserialized in a different JVM, look for an instance with the same name
		// (provided we were given a name)
		if ( name != null ) {
			final var namedResult = SessionFactoryRegistry.INSTANCE.getNamedSessionFactory( name );
			if ( namedResult != null ) {
				if ( SESSION_FACTORY_LOGGER.isTraceEnabled() ) {
					SESSION_FACTORY_LOGGER.resolvedFactoryByName( name );
				}
				return namedResult;
			}
		}

		throw new InvalidObjectException( "No SessionFactory with uuid [" + uuid + "] and name [" + name + "]" );
	}

	/**
	 * Custom serialization hook used during {@code Session} serialization.
	 *
	 * @param oos The stream to which to write the factory
	 * @throws IOException Indicates problems writing out the serial data stream
	 */
	void serialize(ObjectOutputStream oos) throws IOException {
		oos.writeUTF( getUuid() );
		oos.writeBoolean( name != null );
		if ( name != null ) {
			oos.writeUTF( name );
		}
	}

	/**
	 * Custom deserialization hook used during {@code Session} deserialization.

View on GitHub (pinned to fad1729dce)

Solutions

  1. Never serialize live Sessions — detach/clear and serialize plain data or DTOs instead
  2. If cross-JVM deserialization is required, boot a factory with the same name in the target JVM first so the name-based lookup succeeds
  3. Keep the originating factory open in the same JVM while deserialization happens (close it after, not before)
  4. For proxies/collections, serialize detached and reattach via session.merge/lock in a freshly opened session of a live factory

Example fix

// before
byte[] blob = serialize(activeSession); // stores only uuid+name reference
// ... later, in a JVM without that factory:
Session s = (Session) deserialize(blob); // InvalidObjectException

// after
// detach and move data, not infrastructure:
List<Order> dto = session.createQuery("select o from Order o", Order.class).getResultList();
byte[] blob = serialize(dto);
// in the other JVM, open a NEW session on a live factory:
try (Session s = localFactory.openSession()) { ... }
Defensive patterns

Strategy: try-catch

Validate before calling

// Before deserializing, ensure the original factory is alive under the same uuid/name
SessionFactory known = SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(factoryName);
if (known == null) {
    throw new IllegalStateException(
        "Start the SessionFactory named '" + factoryName + "' before deserializing sessions");
}

Try / catch

try (ObjectInputStream in = new ObjectInputStream(bytes)) {
    return (Session) in.readObject();
} catch (InvalidObjectException e) {
    // originating factory absent in this JVM — reattach against a live factory instead
    throw new IllegalStateException(
        "SessionFactory referenced by the stream is not present in this JVM", e);
}

Prevention

When it happens

Trigger: Deserializing a Session (or a detached graph holding a serialized session reference) in a JVM where the originating SessionFactory was closed, never started, or has a different uuid — e.g. passing sessions between processes, or restarting the JVM then reading a cached blob.

Common situations: Distributed caches (Hibernate 2nd-level cache of detached objects, custom serialization in Redis/Hazelcast) that accidentally serialize a Session; session replication features in servlet containers touching Hibernate objects; dev-time serialization tests; serializing entities whose lazy proxies reference the factory.

Related errors


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