Criteria stopped working after upgrading Hibernate ORM to 5.3.7


#1

I have been tasked with migrating a legacy enterprise application from Hibernate 4.3.5 to 5.3.7 and upgrading to Java 10. Our application uses org.hibernate.Criteria to query our database. This works fine and returns data in Hibernate 4.3.5 (Java 10) but when compiled and ran with Hibernate 5.3.7 (Java 10) our calls to Criteria.list() always returns an empty list (no exceptions are thrown). Here is a sample piece of code that returns data with Hibernate 4.3.5 but not 5.3.7. If someone could point out what I need to change that would be great. BTW due to the size of our application changing to use CriteriaBuilder and CriteriaQuery is beyond the scope of the project. Before someone asks, yes there is data in the table.

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class TestHibernate {

	private static String gHibernateCfgXMLFile = "hibernate.cfg.xml";
	private static ServiceRegistry serviceRegistry = null;
	private static SessionFactory gSessionFactory = null;

	public TestHibernate() throws Exception {
		Session fSession = null;
		try {
			Configuration fHibernateCfg = new Configuration();
			fHibernateCfg.configure(gHibernateCfgXMLFile);
		    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(fHibernateCfg.getProperties()).build();
		    gSessionFactory = fHibernateCfg.buildSessionFactory(serviceRegistry);
		    System.out.println("*****************  Users load starting.");
			fSession = gSessionFactory.openSession();
			Criteria fCriteria = fSession.createCriteria(Users.class);
			List<Users> fList = fCriteria.list();
			for (Users fSC : fList) {
				String fName = fSC.getLogon();
				System.out.println("*****************  Users loaded:  [" + fName + "].");
			}
			long fSize = fList.size();
			System.out.println("*********  Users read:  " + fSize + " POJOs.");
		}
		catch(Exception e) {
			System.out.println("Error testing DB connection");
			throw new Exception("Error testing DB connection", e);
		}
		finally {
			try { fSession.close(); } catch(Exception e) { System.out.println("Error closing conection while testing DB connection"); }
		}
	}

    public static void main(String[] args) throws Exception {
    	new TestHibernate();
    }
	
}

#2

Try to replicate it with this test case so we can see exactly what’s not working.


#3

Figured it out. changed how I create the SessionFactory to:

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
SessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();