Java Basic Samples

Sample: main construct
String configFile           = "./hibernate.cfg.xml";
SOSHibernateFactory factory = null;
SOSHibernateSession session = null;

try
{
    factory = new SOSHibernateFactory(configFile);
    factory.build();
     
    // session = factory.openSession();     
    // session = factory.getCurrentSession();     
    session = factory.openStatelessSession();
     
    session.beginTransaction();
    ....
    session.commit();
} 
 
catch( Exception e ) 
{
    if ( session != null )
    {
        try
        {
            session.rollback();
        } 
 
		catch( Exception ex ) {}
	} 
    throw e;
}
finally
{
    if ( session != null )
    {
        session.close();
    }
    
    if ( factory != null )
    {
        factory.close();
    }
}
Sample: session.scroll()
...
ScrollableResults sr = null;

try
{
    Query<MyDbItemEntity> query = session.createQuery("from "+MyDbItemEntity.class.getSimpleName());
    sr = session.scroll(query);             
    while (sr.next()) {
        MyDbItemEntity item = (MyDbItemEntity)sr.get(0);                 
        // do something with the current record            
    }
}
catch( Exception e )
{
    throw e;
}
finally
{
    if ( sr != null ){
       sr.close(); 
    }
}             
...
Sample: join
/** Entity */
public class MyJoinEntity {
   private Long stepId;
   private String orderId;
    public Long getStepId() {
      return stepId;
  }

  public void setStepId(Long val) {
      stepId = val;
  }

  public String getOrderId() {
     return orderId;
  }

  public void setOrderId(String val) {
      orderId = val;
   }
}

/** Usage */
...
StringBuilder hql = new StringBuilder("select ho.orderId as orderId "); // set aliases for all properties
hql.append(",hos.id as stepId ");
hql.append("from " + DBLayer.DBITEM_HISTORY_ORDER).append(" ho ");
hql.append(",").append(DBLayer.DBITEM_HISTORY_ORDER_STEP).append(" hos ");
hql.append("where ho.id=hos.orderId ");

// Query definition with JobScheduler 1.12.x, 1.13.x
Query<MyJoinEntity> query = session.createQuery(hql.toString()); 
query.setResultTransformer(Transformers.aliasToBean(MyJoinEntity.class)); // pass MyJoinEntity as resultType

// Query definition with JobScheduler JS7
// Query<MyJoinEntity> query = session.createQuery(hql.toString(), MyJoinEntity.class); // pass MyJoinEntity as resultType

List<MyJoinEntity> result = session.getResultList(query);
for (MyJoinEntity item : result) {
     LOGGER.info(SOSHibernate.toString(item));
}
...
Sample: session.getSQLExecutor().getResultSet(String sql)
...
executor = session.getSQLExecutor();
...    
ResultSet rs = null;

try
{
    rs = executor.getResultSet("select * from MY_TABLE");             
    Map<String, String> record = null;             
    while (!(record = executor.nextAsStringMap(rs)).isEmpty()) { //or ... executor.next(rs) ...               
        // do something with the current record            
    }
}
catch(Exception e)
{
    throw e;
}
finally
{
    executor.close(rs); 
}             
     
...

JavaScript Job Samples