Classes and Exceptions

SOSHibernateFactory

Java package : com.sos.hibernate.classes

Creates SOSHibernateSession.

Wrapper Class for the Hibernate SessionFactory: see https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html

SOSHibernateSession 

Java package : com.sos.hibernate.classes

Run-time interface between a Java application and Hibernate. 

Wrapper Class for the Hibernate Session and StatelessSession interfaces.

See:

SOSHibernateSQLExecutor

Java package : com.sos.hibernate.classes

Run-time interface between a Java application and Java Connection.

Use native java.sql methods. 

See SOSHibernateSession.getSQLExecutor().

SOSHibernateException

Java package : com.sos.hibernate.exceptions

The base exception type for SOS Hibernate exceptions.

Direct Known Subclasses:

  • SOSHibernateConfigurationException
  • SOSHibernateConnectionException
  • SOSHibernateConvertException
  • SOSHibernateFactoryBuildException
  • SOSHibernateInvalidSessionException
  • SOSHibernateLockAcquisitionException
  • SOSHibernateObjectOperationException
  • SOSHibernateObjectOperationStaleStateException
  • SOSHibernateOpenSessionException
  • SOSHibernateQueryException
  • SOSHibernateQueryNonUniqueResultException
  • SOSHibernateSessionException
  • SOSHibernateTransactionException
  • SOSHibernateSQLExecutorException

Configuration Items

SOSHibernate uses the standard Hibernate configuration to configure Hibernate and allows to set the configuration properties programmatically at runtime.

There are a number of additional SOSHibernate properties that control the behavior of Hibernate at runtime. All are optional and have reasonable default values:

Property nameDefaultPurpose
hibernate.sos.mssql_lock_timeout30000SQL Server: specifies the number of milliseconds a statement waits for a lock to be released.
hibernate.sos.show_configuration_propertiesfalseWrite all configured properties in the log.

See SOSHibernateFactory.setUseDefaultConfigurationProperties(boolean val).

Public Methods 

SOSHibernateFactory 

Constructors

public SOSHibernateFactory()

Use the mappings and properties specified in an application resource named hibernate.cfg.xml.

           

public SOSHibernateFactory(String hibernateConfigFile)

Use the mappings and properties specified in the given application file.

          

public SOSHibernateFactory(Path hibernateConfigFile)

Use the mappings and properties specified in the given application file.

 

Configuration methods

Configuration methods that can be applied, before Hibernate SessionFactory is created.

public void addClassMapping(Class<?> c)

Adds the @Entity annotated single class to the configuration mapping.

public void addClassMapping(ClassList list)

Adds the @Entity annotated classes to the configuration mapping.

import com.sos.hibernate.classes.ClassList;

...

ClassList cl = new ClassList();

cl.add(MyEntity1.class);

cl.add(MyEntity2.class);

cl.addClassIfExist("my.package.MyEntity2");

factory.addClassMapping(cl);

 

public void setIdentifier(String val)  

Sets the factory identifier used by logging.   

 

public void setUseDefaultConfigurationProperties(boolean val)

Sets use defaults configuration properties (default - true).

Following configuration properties wll be applied:

  • hibernate.connection.isolation                   =  String.valueOf(Connection.TRANSACTION_READ_COMMITTED) 
  • hibernate.connection.autocommit                 = false
  • hibernate.jdbc.use_scrollable_resultset = true
  • hibernate.current_session_context_class = jta
  • hibernate.id.new_generator_mappings     = false
  • hibernate.sos.mssql_lock_timeout        = 30000 
  • javax.persistence.validation.mode              = none

 

public void setAutoCommit(boolean commit)     

Sets Hibernate configuration property hibernate.connection.autocommit.   

   

public void setTransactionIsolation(int level)  

Sets Hibernate configuration property hibernate.connection.isolation.     

     

public void setConfigFile(String hibernateConfigFile)   

Sets application file.

Throws: SOSHibernateConfigurationException if file not exist        

 

public void setConfigFile(Path hibernateConfigFile)   

Sets application file.

Throws: SOSHibernateConfigurationException if file not exist

 

public void setConfigurationProperties(Properties properties)

Sets the configuration properties of the current application.

These properties override the properties defined in the application file.

Properties:

  • key - hibernate configuration property name
  • value  - hibernate configuration property name

...

Properties p = new Properties();

p.put("hibernate.show_sql","true");

p.put("hibernate.connection.autocommit","true");

factory.setConfigurationProperties(p);

...

 

public Enum<SOSHibernateFactory.Dbms> getDbmsBeforeBuild()

Returns: Return the current SOSHibernateFactory.Dbms before SessionFactory is created.

Possible values: see SOSHibernateFactory.getDbms

Throws: SOSHibernateConfigurationException

 

Create Hibernate SessionFactory

public void build()

Create a Hibernate SessionFactory using the properties and mappings in this configuration. 

Throws: SOSHibernateFactoryBuildException

 

Methods after Hibernate SessionFactory is created

public SOSHibernateSession openSession()

Open a Session.

Implement a wrapper method for the Hibernate SessionFactory.openSession()

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html#openSession--

Returns: SOSHibernateSession as a warpper class for the Session.

Throws: SOSHibernateOpenSessionException

 

public SOSHibernateSession openSession(String identifier)

Open a Session.

Sets the session identifier used by logging.

Implement a wrapper method for the Hibernate SessionFactory.openSession()

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html#openSession--       

Returns: SOSHibernateSession as a warpper class for the Session.

Throws: SOSHibernateOpenSessionException

 

public SOSHibernateSession openStatelessSession()            

Open a new stateless session.

Implement a wrapper method for the Hibernate SessionFactory.openStatelessSession().

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html#openStatelessSession--

Returns: SOSHibernateSession as a warpper class for the StatelessSession.

Throws: SOSHibernateOpenSessionException

 

public SOSHibernateSession openStatelessSession(String identifier)      

Open a new stateless session.

Sets the session identifier used by logging.

Implement a wrapper method for the Hibernate SessionFactory.openStatelessSession().

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html#openStatelessSession--

Returns: SOSHibernateSession as a warpper class for the StatelessSession.

Throws: SOSHibernateOpenSessionException

       

public SOSHibernateSession getCurrentSession()       

Obtains the current session.

Implement a wrapper method for the Hibernate SessionFactory.getCurrentSession().

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html#getCurrentSession--

Returns: SOSHibernateSession as a warpper class for the Session.

Throws: SOSHibernateOpenSessionException

   

public SOSHibernateSession getCurrentSession(String identifier)   

Obtains the current session.

Sets the session identifier used by logging.

Implement a wrapper method for the Hibernate SessionFactory.getCurrentSession().

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html#getCurrentSession--

Returns: SOSHibernateSession as a warpper class for the Session.

Throws: SOSHibernateOpenSessionException

                

public String quote(Type type, Object value)      

Quotes the field value dependent of the hibernate type.

Returns: value as String

Throws: SOSHibernateConvertException

...

import org.hibernate.type.*;

...

  • factory.quote(NumericBooleanType,new Boolean(true)));
    • return 1
  • factory.quote(LongType,new Long(100));   
    • return 100
  • factory.quote(StringType,"my_''value");
    • return 'my_''''value'
  • factory.quote(TimestampType,new Date());
    • return
      • Oracle
        • to_date('2017-01-01 12:00:00','yyyy-mm-dd HH24:MI:SS');
      • MS SQL Server
        • '2017-01-01T12:00:00.123'
      • Others
        • '2017-01-01 12:00:00.123'

...

 

public String quoteColumn(String columnName)   

Quotes the column.

Returns: quoted column name

...

factory.quoteColumn("MY_COLUMN");

e.g. return

  • MS SQL Server
    • [MY_COLUMN]
  • MySQL
    • `MY_COLUMN`

...

 

public Dialect getDialect()        

Returns: Hibernate Dialect. See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/dialect/Dialect.html   

 

public SessionFactory getSessionFactory()         

Returns: Hibernate SessionFactory. See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html

 

public Enum<SOSHibernateFactory.Dbms> getDbms()    

Returns: Return the current SOSHibernateFactory.Dbms.

Possible values:

UNKNOWN, DB2, FBSQL, MSSQL, MYSQL, ORACLE, PGSQL, SYBASE       

 

public String getSequenceLastValString(String sequenceName)      

Returns the most recent statement of a sequence generator.

Note:  Hibernate Dialect does not provide the functions to identify the last inserted sequence value.

Only for the next value: e.g. dialiect.getSelectSequenceNextValString(sequenceName), dialect.getSequenceNextValString(sequenceName)  

Returns: Return dbms dependent statement to get the last inserted sequence value.

  • MS SQL Server
    • SELECT @@IDENTITY
  • MySQL
    • SELECT LAST_INSERT_ID();
  • Oracle
    • SELECT <sequenceName>.currval FROM DUAL;
  • PostreSQL
    • SELECT currval('<sequenceName>');
  • DB2
    • SELECT IDENTITY_VAL_LOCAL() AS INSERT_ID FROM SYSIBM.SYSDUMMY1
  • Sybase
    • SELECT @@IDENTITY

 

public void close()

Destroy this SessionFactory and release all resources (caches, connection pools, etc). 

Implement a wrapper method for the Hibernate SessionFactory.close().      

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SessionFactory.html#close--

 

Others

public boolean getAutoCommit()     

Returns: Hibernate configuration property hibernate.connection.autocommit.

Throws: SOSHibernateConfigurationException - if not configured

 

public int getTransactionIsolation()   

Returns: Hibernate configuration property hibernate.connection.isolation.

Throws: SOSHibernateConfigurationException - if not configured      

 

public static String getTransactionIsolationName(int isolationLevel)    

Returns: Transaction isolation name.

Possible values:

  • TRANSACTION_NONE
  • TRANSACTION_READ_UNCOMMITTED
  • TRANSACTION_READ_COMMITTED
  • TRANSACTION_REPEATABLE_READ
  • TRANSACTION_SERIALIZABLE

Throws: SOSHibernateConfigurationException - if invalid isolationLevel    

public static String toString(Object dbItem)    

Returns: dumping a java object's public properties.

Note: starting with JobScheduler 1.11.5


 

public Optional<Path> getConfigFile()       

Returns: application file.    

 

public Configuration getConfiguration()   

Returns: Hibernate Configuration. See  https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/cfg/Configuration.html    

    

public Properties getConfigurationProperties()

Returns: configuration properties defined by the current application. 


public ClassList getClassMapping()

Returns: mapping entities defined by the current application. 


public boolean isUseDefaultConfigurationProperties()

Returns: default configuration properties using.

 

public Properties getDefaultConfigurationProperties()   

Returns: default configuration properties.

See SOSHibernateFactory.setUseDefaultConfigurationProperties

 

public String getIdentifier()         

Returns: gets the factory identifier.  

 

public Optional<Integer> getJdbcFetchSize()

Returns: Hibernate configuration property hibernate.jdbc.fetch_size.

 

SOSHibernateSession

Constructors

No public constructors available.

See:

  • SOSHibernateFactory.openSession
  • SOSHibernateFactory.openStatelessSession
  • SOSHibernateFactory.getCurrentSession

Set

public void setAutoCommit(boolean val)

Sets the session autocommit.

Default - SOSHibernateFactory.getAutoCommit()

Note: starting with JobScheduler 1.11.4

 

public void setIdentifier(String val)

Sets the session identifier used by logging.   

 

public void setCacheMode(CacheMode cacheMode) 

Implement a wrapper method for the Hibernate Session.setCacheMode(CacheMode cashMode).                                                        

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/CacheMode.html                                                        

Note: works only with Hibernate Session interface, not with StatelessSession interface.  

      

public void setHibernateFlushMode(FlushMode flushMode)

Represents a flushing strategy.                                                      

Implement a wrapper method for the Hibernate Session.setHibernateFlushMode(FlushMode flushMode).       

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/FlushMode.html                                                 

Note: works only with Hibernate Session interface, not with StatelessSession interface.

 

Get

public boolean isAutoCommit()  

Returns: session autocommit mode.

Note: starting with JobScheduler 1.11.4

 

public boolean isStatelessSession()  

Returns: Check if the SOSHibernateSession was created by SessionFactory.openStatelessSession().   

 

public boolean isGetCurrentSession()   

Returns: Check if the SOSHibernateSession was created by SessionFactory.getCurrentSession().     

  

public boolean isOpen() 

Implement a wrapper method for the Hibernate Session/StatelessSession isOpen.

Returns: Check if the session is still open.

 

public boolean isConnected() 

Implement a wrapper method for the Hibernate Session/StatelessSession isConnected.

Returns: Check if the session is currently connected.   

 

public SOSHibernateFactory getFactory()        

Returns: SOSHibernateFactory.

 

public SOSHibernateSQLExecutor getSQLExecutor()        

Returns: SOSHibernateSQLExecutor.

 

public String getIdentifier()     

Returns: session identifier. 

 

public FlushMode getHibernateFlushMode()  

Returns: Hibernate FlushMode. See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/FlushMode.html

Note: works only with Hibernate Session interface, not with StatelessSession interface.

 

public CacheMode getCacheMode()        

Returns: Hibernate CashMode. See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/FlushMode.html    

Note: works only with Hibernate Session interface, not with StatelessSession interface.  

 

public Object getCurrentSession()   

Returns:current Session or StatelessSession object.

 

public Connection getConnection()      

Returns: java.sql.Connection.

 

Session

public void close()     

Close the Hibernate Session / StatelessSession.

 

public void clearSession()

Completely clear the session.

Implement a wrapper method for the Hibernate Session.clear().

See: https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Session.html#clear--

Note: works only with Hibernate Session interface, not with StatelessSession interface.

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateSessionException

Transaction

public void beginTransaction() 

Implement a wrapper method for the Hibernate Session / StatelessSession beginTransaction().

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateTransactionException

       

public void commit() 

Implement a wrapper method for the Hibernate Session / StatelessSession commit().

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateTransactionException

       

public void rollback() 

Implement a wrapper method for the Hibernate Session / StatelessSession rollback().

ThrowsSOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateTransactionException     

 

public Transaction getTransaction() 

Implement a wrapper method for the Hibernate Session / StatelessSession getTransaction().

Returns:Hibernate Transaction.

ThrowsSOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateTransactionException  

 

Hibernate object/entity

public void save(Object item) 

Persist the given transient instance, first assigning a generated identifier.

Implement a wrapper method for the Hibernate Session.save(Object object) / StatelessSession.insert(Object entity).        

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateObjectOperationException

 

public void update(Object item) 

Update the persistent instance with the identifier of the given detached instance.

Implement a wrapper method for the Hibernate Session / StatelessSession update(Object object).       

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateObjectOperationException

 

public void saveOrUpdate(Object item) 

Persist or update the persistent instance with the identifier of the given detached instance.

Implement a wrapper method for the Hibernate Session.saveOrUpdate / StatelessSession insert/update(Object object).       

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateObjectOperationException

 

public void delete(Object item) 

Remove a persistent instance from the datastore.

Implement a wrapper method for the Hibernate Session / StatelessSession delete(Object object).    

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateObjectOperationException

    

public void refresh(Object object) 

Re-read the state of the given instance from the underlying database.

Implement a wrapper method for the Hibernate Session / StatelessSession refresh(Object object).     

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateObjectOperationException

   

public void refresh(String entityName, Object object) 

Re-read the state of the given instance from the underlying database.

Implement a wrapper method for the Hibernate Session / StatelessSession refresh(String entityName, Object object).       

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateObjectOperationException

 

public <T> T get(Class<?> entityClass, Serializable id) 

Returns: persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.

Implement a wrapper method for the Hibernate Session / StatelessSession get(Class entityClass, Serializable id).

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateObjectOperationException

       

Native Queries

Create

public <T> NativeQuery<T> createNativeQuery(String sql)         

Create a NativeQuery instance for the given native (SQL) query string.

Implement a wrapper method for the Hibernate createNativeQuery.

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/query/QueryProducer.html#createNativeQuery-java.lang.String-

Returns: The NativeQuery instance for manipulation and execution. 

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

 

public <T> NativeQuery<T> createNativeQuery(String sql, Class<T> entityClass)    

Create a NativeQuery instance for the given native (SQL) query string using implicit mapping to the specified Java type.

Implement a wrapper method for the Hibernate createNativeQuery.

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/query/QueryProducer.html#createNativeQuery-java.lang.String-

Returns: The NativeQuery instance for manipulation and execution.  

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

 

Execute


public int executeUpdateNativeQuery(String sql)      

The method executes statements as UPDATE, INSERT, DELETE..

Implement a wrapper method for the Hibernate executeUpdate.

Returns: The number of entities updated or deleted.

Throws: SOSHibernateInvalidSessionException,SOSHibernateLockAcquisitionException, SOSHibernateQueryException

 

public int executeUpdate(Query<?> query)      

The method executes statements as UPDATE, INSERT, DELETE..

Implement a wrapper method for the Hibernate executeUpdate.

To execute the native statements Query param must be a NativeQuery.

Returns: The number of entities updated or deleted.

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

 

Select  

public <T> String getSingleValueNativeQueryAsString(String sql)    

Creates and get the result of the Hibernate NativeQuery.

The method reads a scalar value – usually a field – of the first record from the result set of a NativeQuery.

Returns: String or null in the event of an empty result set. 

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

               Note: difference to Query.getSingleResult - not throw NoResultException.

 

public <T> T getSingleValueNativeQuery(String sql)    

Creates and get the result of the Hibernate NativeQuery.

The method reads a scalar value – usually a field – of the first record from the result set of a NativeQuery.

Returns: T Object or null in the event of an empty result set. 

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

               Note: difference to Query.getSingleResult - not throw NoResultException. 

                  

 

public <T> T getSingleValue(Query<T> query)    

 Get the result of the Hibernate Query or NativeQuery.

 The method reads a scalar value – usually a field – of the first record from the result set of a Query or NativeQuery.

 Returns: T Object or null in the event of an empty result set. 

 Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

               Note: difference to Query.getSingleResult - not throw NoResultException. 

 

public <T> String getSingleValueAsString(Query<T> query)    

 Get the result of the Hibernate Query or NativeQuery.

 The method reads a scalar value – usually a field – of the first record from the result set of a Query or NativeQuery.

 Returns: String or null in the event of an empty result set. 

 Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

                Note: difference to Query.getSingleResult - not throw NoResultException.


public <T> T getSingleResultNativeQuery(String sql)     

Creates a NativeQuery from sql query string and execute <T> T getSingleResult(Query<T> query)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

 

 

public <T> Map<String, Object> getSingleResultNativeQueryAsMap(String sql)     

Creates a NativeQuery from sql query string and execute <T> Map<String, Object> getSingleResultAsMap(NativeQuery<T> nativeQuery)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4


public <T> Map<String, String> getSingleResultNativeQueryAsStringMap(String sql)     

Creates a NativeQuery from sql query string and execute <T> Map<String, String> getSingleResultAsStringMap(NativeQuery<T> nativeQuery, String dateTimeFormat)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4

   

public <T> Map<String, String> getSingleResultNativeQueryAsStringMap(String sql, String dateTimeFormat)

Creates a NativeQuery from sql query string and execute <T> Map<String, String> getSingleResultAsStringMap(NativeQuery<T> nativeQuery, String dateTimeFormat)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4

 

public <T> T getSingleResult(Query<T> query)    

 The method reads one record from a query (Query or NativeQuery).

 Throws: SOSHibernateInvalidSessionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

               Note: difference to Query.getSingleResult - not throw NoResultException.    

public <T> Map<String, Object> getSingleResultAsMap(NativeQuery<T> nativeQuery)    

The method reads one record from a query.

Returns: Map consisting of column names and values of the first record – null for empty result sets.

  • Map key     - column name in lower case.
  • Map value  - object (null value as NULL)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

               Note: difference to Query.getSingleResult - not throw NoResultException.          

Note: starting with JobScheduler 1.11.4


public <T> Map<String, String> getSingleResultAsStringMap(NativeQuery<T> nativeQuery, String dateTimeFormat)   

The method reads one record from a query and format the Date fileds. 

Returns: Map consisting of column names and values of the first record – null for empty result sets.

  • Map key     - column name in lower case.
  • Map value  - string representation (null value as empty string)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

              Note: difference to Query.getSingleResult - not throw NoResultException.                   

Note: starting with JobScheduler 1.11.4


public <T> List<T> getResultListNativeQuery(String sql)   

 Creates a NativeQuery from sql query string and execute <T> List<T> getResultList(Query<T> query)

  Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException


public <T> List<Map<String, Object>> getResultListNativeQueryAsMaps(String sql)   

Creates a NativeQuery from sql query string and execute <T> List<Map<String, Object>> getResultListAsMaps(NativeQuery<T> nativeQuery)

 Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4

 

public <T> List<Map<String, String>> getResultListNativeQueryAsStringMaps(String sql)     

 Creates a NativeQuery from sql query string and execute <T> List<Map<String, String>> getResultListAsStringMaps(NativeQuery<T> nativeQuery, String dateTimeFormat)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4


public <T> List<Map<String, String>> getResultListNativeQueryAsStringMaps(String sql, String dateTimeFormat)     

Creates a NativeQuery from sql query string and execute <T> List<Map<String, String>> getResultListAsStringMaps(NativeQuery<T> nativeQuery, String dateTimeFormat)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4


public <T> List<T> getResultList(Query<T> query)      

The method reads all records from a query (Query or NativeQuery). 

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

 

public <T> List<Map<String, Object>> getResultListAsMaps(NativeQuery<T> nativeQuery)      

 The method reads all records from a query. 

 Returns: A List of the Maps consisting of column names and values – null for empty result sets.

  • Map key     - column name in lower case.
  • Map value  - object (null value as NULL)

 Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4


public <T> List<Map<String, String>> getResultListAsStringMaps(NativeQuery<T> query)      

The method reads all records from a query. 

Returns: A List of the Maps consisting of column names and values – null for empty result sets.

  • Map key     - column name in lower case.
  • Map value  - string representation (null value as empty string)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4

     

public <T> List<Map<String, String>> getResultListAsStringMaps(NativeQuery<T> query, String dateTimeFormat) 

The method reads all records from a query and format the Date fileds.  

Returns: A List of the Maps consisting of column names and values – null for empty result sets. 

  • Map key     - column name in lower case.
  • Map value  - string representation (null value as empty string)

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4

 

public <T> ScrollableResults scroll(Query<T> query)

The method executes the scroll method with the ScrollMode.FORWARD_ONLY

See ScrollableResults scroll(Query<T> query, ScrollMode scrollMode);

Note: starting with JobScheduler 1.11.4

 

public <T> ScrollableResults scroll(Query<T> query, ScrollMode scrollMode)

The method executes a sql query statements and preserves the result set.

This method can be used with subsequent calls to the next(ScrollableResults sr) method in order to subsequently fetch records from a result set.

See Samples.

Returns: ScrollableResults

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4

 

public String getLastSequenceValue(String sequenceName)   

Returns: the most recent value of a sequence generator.

See SOSHibernateFactory.getSequenceLastValString(String sequenceName).

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

      

SOSHibernateSQLExecutor

Called by session.getSQLExecutor(). 

public void setDefaults()   

The following default settings are applied: 

  • MS SQL Server
    • set DATEFORMAT ymd
    • set LANGUAGE British
    • set LOCK_TIMEOUT 3000
  • MySQL
    • SET SESSION SQL_MODE='ANSI_QUOTES'
  • Oracle
    • ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,'
    • ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
    • ALTER SESSION SET NLS_SORT='BINARY'
    • begin dbms_output.enable(10000); end;
  • PostgreSQL
    • SELECT set_config('lc_numeric', '', true)
    • SELECT set_config('datestyle', 'ISO, YMD', true)
    • SELECT set_config('default_transaction_isolation', 'repeatable read', true)
  • Sybase
    • set TRANSACTION ISOLATION LEVEL READ COMMITTED
    • set CHAINED ON
    • set QUOTED_IDENTIFIER ON
    • set LOCK WAIT 3
    • set CLOSE ON ENDTRAN ON
    • set DATEFIRST 1
    • set DATEFORMAT 'ymd'
    • set LANGUAGE us_english
    • set TEXTSIZE 2048000

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

public List<String> getStatements(Path file)       

Returns: List of the individual SQL query statements from multiple statements that are specified in the file.  

Throws: SOSHibernateSQLCommandExtractorException, SOSHibernateSQLExecutorException

 

public List<String> getStatements(String content)    

Returns: List of the individual sql query statements from multiple statements that are specified in the method argument. This method implements a parser that isolates statements and respects DDL statements (create procedure etc.) and DML statements.      

Throws: SOSHibernateSQLCommandExtractorException

 

public void executeStatements(Path file)     

Execute individual SQL query statements from multiple statements that are specified in the file.

ThrowsSOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLCommandExtractorException, SOSHibernateSQLExecutorException     

 

public void executeStatements(String content)  

Execute individual SQL query statements from multiple statements that are specified in the method argument. 

ThrowsSOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLCommandExtractorException, SOSHibernateSQLExecutorException

 

public boolean execute(String... sqls)      

The method executes one or more statements, e.g. calls to stored procedures.

Implement a wrapper method for the java.sql.Statement.execute(String sql)

Returns:

  • true  - if the first result of the last statement is a ResultSet object
  • false - if it is an update count or there are no results.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

     

 

public int executeUpdate(String... sqls)      

The method executes DML statements as UPDATE, INSERT, DELETE..

Implement a wrapper method for the java.sql.Statement.executeUpdate(String sql)

Returns: The number of entities updated or deleted.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

 

public void executeQuery(String sql)       

The method executes a sql query statement without preserves the result set.

Note: see getResultSet to execute query statement with a result set.

Implement a wrapper method for the java.sql.Statement.executeQuery(String sql)

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

 

 

public int[] executeBatch(String... sqls)  

The method executes one or more statements as batch.

Implement a wrapper method for the java.sql.Statement.executeBatch().

Returns: an array of update counts containing one element for each command in the batch. The elements of the array are ordered according to the order in which commands were added to the batch.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

      

public int executeUpdateCallableStatement(String sql)  

Implement a wrapper method for the java.sql.CallableStatement.executeUpdate().

Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

 

public ResultSet getResultSet(String sql)

The method executes a sql query statements and preserves the result set.

This method can be used with subsequent calls to the next(ResultSet rs) method in order to subsequently fetch records from a result set.

See Samples.

Returns: ResultSet

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

 

public Map<String, Object> next(ResultSet rs)

The method reads the next record from the result set returned by a query.

This method is typically used in while() constructs that subsequently fetch records from a result set.

See Samples.

Returns: Map of column names (lower case) and values (objects) of the next record.

Throws: SOSHibernateSQLExecutorException

 

public Map<String, String> nextAsStringMap(ResultSet rs)

The method reads the next record from the result set returned by a query.

This method is typically used in while() constructs that subsequently fetch records from a result set.

See Samples.

Returns: Map of column names (lower case) and values (string representation, null value as empty string) of the next record.

Throws: SOSHibernateSQLExecutorException

 

public void close(ResultSet rs)

The method closes the result set and statement objects.

See Samples.

 

BLOB/CLOB
public byte[] getBlob(String sql)   

This method retrieves the content of a BLOB database field.

Returns: content of the BLOB as byte array.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

             

public long getBlob(String sql, Path path)       

This method saves the content of a BLOB database field in a new file.

Returns: number of bytes written to the file.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

 

public String getClob(String sql)      

This method retrieves the content of a CLOB database field.  

Returns: content of the CLOB as string.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException


public long getClob(String sql, Path path)

This method retrieves the content of a CLOB database field to a file.

Returns: number of bytes written to the file.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

 

public int updateBlob(Path path, String tableName, String columnName, String condition)  

This method saves the content of a file in a BLOB database field.

Returns: number of bytes that were updated.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException


public int updateBlob(byte[] data, String tableName, String columnName, String condition)    

This method saves the content of a byte array in a BLOB database field. 

Returns: number of bytes that were updated.

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

           

public void updateBlob(InputStream inputStream, int dataLength, String tableName, String columnName, String condition)     

This method saves the InputStream in a BLOB database field. 

ThrowsSOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException     

            

public int updateClob(Path path, String tableName, String columnName, String condition)        

This method saves the content of a file in a CLOB database field. 

Returns: number of bytes that were updated.

ThrowsSOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException  


public int updateClob(String data, String tableName, String columnName, String condition)            

This method saves the string data in a CLOB database field.  

Returns: number of bytes that were updated.

ThrowsSOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException  

   

public void updateClob(Reader reader, int dataLength, String tableName, String columnName, String condition)    

This method saves the Reader in a CLOB database field.   

Throws: SOSHibernateInvalidSessionException, SOSHibernateConnectionException, SOSHibernateSQLExecutorException

 

HQL/JPQL Queries

Wrapper methods using Hibernate Query interface.

Create 

public <T> Query<T> createQuery(String hql)  

Create a Query instance for the given HQL/JPQL query string.

Implement a wrapper method for the Hibernate createQuery.

See https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/query/QueryProducer.html#createQuery-java.lang.String-

Returns: The Query instance for manipulation and execution. 

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

 

Execute

 

public int executeUpdate(String hql)      

Creates a Query from HQL/JPQL query string and execute int executeUpdate(Query<?> query).

Returns: See int executeUpdate(Query<?> query).

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

 

public int executeUpdate(Query<?> query)      

The method executes statements as UPDATE, INSERT, DELETE..

Implement a wrapper method for the Hibernate executeUpdate.

Returns: The number of entities updated or deleted.

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException

 

Select

public <T> T getSingleValue(String hql)  

Creates a Query from HQL/JPQL query string and execute <T> T getSingleValue(Query<T> query).

Returns:See <T> T getSingleValue(Query<T> query).
Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

 

public String getSingleValueAsString(String hql)  

 Creates a Query from HQL/JPQL query string and execute <T> T getSingleValue(Query<T> query).

 Returns: value as String or null in the event of an empty result set.
Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException


public <T> T getSingleValue(Query<T> query)     

The method reads a value of the first record from the result set of a Query.

Returns: value as T object or null in the event of an empty result set.

ThrowsSOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

               Note: difference to Query.getSingleResult - not throw NoResultException.

                  

 

public <T> String getSingleValueAsString(Query<T> query)     

 The method reads a value of the first record from the result set of a Query.

 Returns: value as String or null in the event of an empty result set.

 ThrowsSOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

                Note: difference to Query.getSingleResult - not throw NoResultException.


public <T> T getSingleResult(String hql) 

Creates a Query from HQL/JPQL query string and execute <T> T getSingleResult(Query<T> query).

Returns: See <T> T getSingleResult(Query<T> query).

ThrowsSOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException

         

public <T> T getSingleResult(Query<T> query) 

The method reads one record from a query.

Returns:  T Object or null

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryNonUniqueResultException, SOSHibernateQueryException
               Note: difference to Query.getSingleResult - not throw NoResultException.

 

public <T> List<T> getResultList(String hql) 

Creates a Query from HQL/JPQL query string and execute <T> T getResultList(Query<T> query).

See <T> T getResultList(Query<T> query).

ThrowsSOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException
 

public <T> List<T> getResultList(Query<T> query) 

The method reads all records from a query.

Returns:  List of the T Objects

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, SOSHibernateQueryException


public <T> ScrollableResults scroll(Query<T> query)

The method executes the scroll method with the ScrollMode.FORWARD_ONLY

See ScrollableResults scroll(Query<T> query, ScrollMode scrollMode);

Note: starting with JobScheduler 1.11.4

 

public <T> ScrollableResults scroll(Query<T> query, ScrollMode scrollMode)

The method executes a sql query statements and preserves the result set.

This method can be used with subsequent calls to the next(ScrollableResults sr) method in order to subsequently fetch records from a result set.

See Samples.

Returns: ScrollableResults

Throws: SOSHibernateInvalidSessionException, SOSHibernateLockAcquisitionException, , SOSHibernateQueryException

Note: starting with JobScheduler 1.11.4

SOSHibernateException

Additional public methods:

public SQLException getSQLException

Returns: java.sql.SQLException or null

 

public String getStatement()

Returns: sql/hql statement, [n/a] for not available or null.

 

public String toString()

Returns: provides additionally the sql/hql statement if possible.

History of SOSHibernate with JobScheduler Releases

Release 1.11.1 - new methods

  • SOSHibernateFactory
    • addClassMapping(Class<?> c)

Release 1.11.4 - new methods

  • SOSHibernateException
    • SOSHibernateLockAcquisitionException
    • SOSHibernateObjectOperationException
  • SOSHibernateSession
    • setAutoCommit
    • isAutoCommit
    • getSingleResultNativeQueryAsMap
    • getSingleResultNativeQueryAsStringMap
    • getSingleResultAsMap
    • getSingleResultAsStringMap
    • getResultListNativeQueryAsMaps
    • getResultListNativeQueryAsStringMaps
    • getResultListAsMaps
    • getResultListAsStringMaps
    • scroll

Release 1.11.5 - new methods

  • SOSHibernateException
    • SOSHibernateObjectOperationStaleStateException
  • SOSHibernateFactory
    • toString(Object dbItem)