DiF Model - Hibernate Util

Access the class JavaDoc here

This utility provides the integration with Hibernate Session Factory. All workings are internal and need not be accessed by a developer.
The only method we need to understand is getSessionFactory(String datasourceName)

getSessionFactory(String datasourceName) Method

This method manages all Hibernate factories for all DiF managed datasources.
We pass the datasource name to DiF and it loads the associated dataSource.cfg.xml configuration file, initializes the session factory configuration and customizes it through the Configuration Repository custom properties if defined.

All this is automatically managed for us. DiF Model keeps all configured session factories cached in an inner SesisonFactory Map so this happens only the first time.

The usage cannot be simpler:

    Session session = HibernateUtil.getSessionFactory("databaseName").getCurrentSession();
    
    Transaction tx = session.beginTransaction();
    
    List<Client> clients = session.createQuery("select c.* from Client as c").list();
    System.out.println("Full client name list:");
    System.out.println("--------------------------\n");
    
    for (Client client: clients) {
        System.out.println("Client name: " + client.getName());
    }

    tx.commit();

Default Configuration file

The commonly named hibernate.cfg.xml file must exist on the class path and be named following the convention "datasourceName".cfg.xml.

This is how we can have several datasources on a single project and DiF Model can distinguish between their individual configurations.

As we have stated before, these files are used for the default configuration only, since DiF Model reads customized properties from the Configuration Repository that override these default ones.

Custom configurations

The custom configurations are kept in the Configurations Repository under the key:
dif2/Model/databaseName

These can be any specific hibernate property or one of the following general properties that we encourage the usage since we translate them to the Hibernate properties shielding the user from it's inner knowledge and future releases that could break name compatibility.

A full list of the properties can be viewed in the following table:

username Username used to connect to the datasource
password Password used to connect to the datasource
url The URL that identifies the server and database instance to connect to
default.schema The default database schema where tables will be searched
driver The driver class canonical name
dialect The dialect class canonical name
pool.size The size of the connection pool that will be created for this datasource
debug If the SQL debug logging will be active or not

ORM Generator integration

The ORM Generator plugin generates code that uses the HibernateUtil session factory integration.

Has you can see in the project's documentation page a factory class is created for each datasource which uses the HibernateUtil.getSessionFactory method.
This class is uses by all the DAOs that ORM Generator creates.
The previous code in the example above is therefore not necessary since we can call the generated finders from the DAO and they in turn already initialize the session factory themselves.