Serializing the hibernate sessionfactory & deserialize it to improve startup time


I have around 500 entities and taking around 30sec to initialize the Hibernate SessionFactory during service startup. I am just thinking of serializing the SessionFactory to a file during maven build process and just deserialize it during service startup.

I am able to serialize during maven build process but while deserializing, i am getting the following error:

Caused by: org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.internal.util.SerializationHelper.doDeserialize(
at org.hibernate.internal.util.SerializationHelper.deserialize(

During maven build time, serializing the session factory object:
Configuration config = new Configuration();
for (String entityName: EntityNameList) {
try {
} catch (ClassNotFoundException e) {
try (FileOutputStream file = new FileOutputStream(“c:\work\configuration2.ser”);
ObjectOutputStream out = new ObjectOutputStream(file)) {

config.setProperty("hibernate.dialect", "");
config.setProperty(AvailableSettings.SESSION_FACTORY_NAME, "mysf")
		.setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, "false"); // default is true
		SessionFactory factory = config.buildSessionFactory();
		String uuid = ((SessionFactoryImplementor) factory).getUuid();
		System.out.println("original uuid::" + uuid);
	} catch (IOException e) {


Deserialization process:
FileInputStream file = new FileInputStream(“c:\work\configuration2.ser”);
ObjectInputStream in = new ObjectInputStream(file);

SessionFactoryImplementor factory = (SessionFactoryImplementor) SerializationHelper.deserialize(in);

From the hibernate sessionfactory deserialization logic:

private static SessionFactory locateSessionFactoryOnDeserialization(String uuid, String name) throws InvalidObjectException{

final SessionFactory uuidResult = SessionFactoryRegistry.INSTANCE.getSessionFactory( uuid );

if ( uuidResult != null ) {
LOG.debugf( “Resolved SessionFactory by UUID [%s]”, 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 SessionFactory namedResult = SessionFactoryRegistry.INSTANCE.getNamedSessionFactory( name );

if( namedResult != null) {
LOG.debugf( “Resolved SessionFactory by name [%s]”, name );
return namedResult;

throw new InvalidObjectException( “Could not find a SessionFactory [uuid=” + uuid + “,name=” + name + “]” );

It seems like while deserializing the sessionfactory it is always trying to get from the local cache.

So, my question is

  1. Is it possible to serialize the hibernate session factory in JVM (maven build process) and use it in a different JVM?
  2. Is my approach correct? Am i missing anything here?
  3. Some forums saying that it is possible when we use JNDI context. Is this correct?

Thank in advance.

Any update on this question?