@Override
 public boolean onFlushDirty(
     Object entity,
     Serializable id,
     Object[] currentState,
     Object[] previousState,
     String[] propertyNames,
     Type[] types) {
   try {
     logger.info("onFlushDirty: Detected dirty object " + entity + " with id " + id);
     final int length = currentState.length;
     logger.info("onFlushDirty: Object Details are as below: ");
     for (int i = 0; i < length; i++) {
       logger.info(
           "onFlushDirty: propertyName : "
               + propertyNames[i]
               + " ,type :  "
               + types[i]
               + " , previous state : "
               + previousState[i]
               + " , current state : "
               + currentState[i]);
     }
     return tryToCastToCurrentObject(entity, cl, InterceptorOperations.UPDATE);
   } catch (CallbackException e) {
     logger.error(e.getMessage(), e);
   }
   return false; // as no change made to object here
 }
  public Object inserirComCommit(Object objeto) throws ErroRepositorioException {

    Session session = HibernateUtil.getSession();

    Object retorno = null;

    try {

      // session.beginTransaction();
      session.persist(objeto);
      retorno = session.save(objeto);
      //	session.getTransaction().commit();
      session.flush();
      session.clear();

      return retorno;
    } catch (GenericJDBCException ex) {
      ex.printStackTrace();
      throw new ErroRepositorioException(ex, "Erro no Hibernate");
    } catch (CallbackException e) {
      e.printStackTrace();
      throw new ErroRepositorioException(e, e.getMessage());
    } catch (HibernateException e) {
      e.printStackTrace();
      throw new ErroRepositorioException(e, "Erro no Hibernate");
    } finally {
      HibernateUtil.closeSession(session);
    }
  }
  @SuppressWarnings("rawtypes")
  public void remover(
      int id,
      String pacoteNomeObjeto,
      OperacaoEfetuada operacaoEfetuada,
      Collection<UsuarioAcaoUsuarioHelper> acaoUsuarioHelper)
      throws ErroRepositorioException {

    Session session = HibernateUtil.getSession();

    try {

      Iterator iterator =
          session
              .createQuery("from " + pacoteNomeObjeto + " where id = :id")
              .setInteger("id", id)
              .iterate();

      if (!iterator.hasNext()) {
        throw new RemocaoRegistroNaoExistenteException();
      }

      while (iterator.hasNext()) {
        Object obj = iterator.next();
        if (obj instanceof ObjetoTransacao && operacaoEfetuada != null) {
          ObjetoTransacao objetoTransacao = (ObjetoTransacao) obj;
          objetoTransacao.setOperacaoEfetuada(operacaoEfetuada);
          Iterator it = acaoUsuarioHelper.iterator();
          while (it.hasNext()) {
            UsuarioAcaoUsuarioHelper helper = (UsuarioAcaoUsuarioHelper) it.next();
            objetoTransacao.adicionarUsuario(helper.getUsuario(), helper.getUsuarioAcao());
          }
        }
        iterator.remove();
      }
      session.flush();
    } catch (JDBCException e) {
      throw new RemocaoInvalidaException(e);

    } catch (CallbackException e) {
      throw new ErroRepositorioException(e, e.getMessage());

    } catch (HibernateException e) {
      throw new ErroRepositorioException(e, "Erro no Hibernate");

    } finally {
      HibernateUtil.closeSession(session);
    }
  }
  public void atualizar(Object objeto) throws ErroRepositorioException {
    Session session = HibernateUtil.getSession();

    try {
      session.update(objeto);
      session.flush();

    } catch (CallbackException e) {
      throw new ErroRepositorioException(e, e.getMessage());
    } catch (HibernateException e) {
      e.printStackTrace();
      throw new ErroRepositorioException("Erro no Hibernate");

    } finally {
      HibernateUtil.closeSession(session);
    }
  }
  public void remover(Object objeto) throws ErroRepositorioException {
    Session session = HibernateUtil.getSession();

    try {
      session.delete(objeto);
      session.flush();
    } catch (JDBCException e) {
      throw new RemocaoInvalidaException(e);
    } catch (CallbackException e) {
      throw new ErroRepositorioException(e, e.getMessage());

    } catch (HibernateException e) {
      e.printStackTrace();
      throw new ErroRepositorioException(e, "Erro no Hibernate");
    } finally {
      HibernateUtil.closeSession(session);
    }
  }
  public Object obterNextVal(Object objeto) throws ErroRepositorioException {

    Session session = HibernateUtil.getSession();
    Object retorno = null;
    String consulta = null;

    try {
      String dialect = HibernateUtil.getDialect();
      // verifica se o objeto é do tipo Imovel
      if (objeto instanceof Imovel) {
        if (dialect.toUpperCase().contains("ORACLE")) {
          consulta = "select cadastro.seq_imovel.nextval as id from dual ";
        } else {
          consulta = "select nextval('cadastro.seq_imovel') as id ";
        }
        // verifica se o objeto é do tipo Cliente
      } else if (objeto instanceof Cliente) {
        if (dialect.toUpperCase().contains("ORACLE")) {
          consulta = "select cadastro.seq_cliente.nextval as id from dual ";
        } else {
          consulta = "select nextval('cadastro.seq_cliente') as id ";
        }
      }

      retorno = session.createSQLQuery(consulta).addScalar("id", Hibernate.INTEGER).uniqueResult();

      return retorno;
    } catch (GenericJDBCException ex) {
      throw new ErroRepositorioException(ex, "Erro no Hibernate");
    } catch (CallbackException e) {
      throw new ErroRepositorioException(e, e.getMessage());
    } catch (HibernateException e) {
      throw new ErroRepositorioException(e, "Erro no Hibernate");
    } finally {
      HibernateUtil.closeSession(session);
    }
  }