StringBuffer sql = new StringBuffer("SELECT count(*) as maxId FROM " +tableName+" where fk_compay_id="+companyId+"");
Integer maxId = (Integer) (getSessionFactory().getCurrentSession().createSQLQuery(sql.toString()).addScalar("maxId", StandardBasicTypes.INTEGER)).uniqueResult();
return maxId;