Search code examples
javaregexhibernatehibernate-criteria

Get a specific value in Criteria hibernate String


I need extract some values on criteria String. The concept is to get the real SQL sentence and his parameters (no log)

For the moment, I used this for get the Select:

CriteriaImpl c = (CriteriaImpl) criteria;
       SessionImpl s = (SessionImpl)c.getSession();
       SessionFactoryImplementor factory = s.getFactory();
       CriteriaQueryTranslator translator=new CriteriaQueryTranslator(factory,c,c.getEntityOrClassName(),CriteriaQueryTranslator.ROOT_SQL_ALIAS);
       String[] implementors = factory.getImplementors( c.getEntityOrClassName() );

       CriteriaJoinWalker walker = new CriteriaJoinWalker((OuterJoinLoadable)factory.getEntityPersister(implementors[0]), 
                               translator,
                               factory, 
                               c, 
                               c.getEntityOrClassName(), 
                               s.getLoadQueryInfluencers());


       String sql=walker.getSQLString();

And the result:

select this_.ID_ELEMENTO as ID1_72_32_, this_2_.ACTIVO as[....]where this_2_.FECHA_BAJA is null and this_2_.ID_TIPO_ELEMENTO=? and this_2_.ACTIVO=? order by this_.ID_ELEMENTO asc

The closest thing to obtain the parameters:

CriteriaQueryTranslator subCriteria= new CriteriaQueryTranslator(factory, c, c.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);


       String root =subCriteria.getRootCriteria().toString();

And the result:

CriteriaImpl(com.itym.pojo.elementos.antenas.AntenaAnalogicaPojo:elemento[Subcriteria(tipoElemento:tipoElemento)][fechaBaja is null, tipoElemento=ANTENA, activo=true])

I need the "TipoElemento" and "activo" values, to replace on the first string.

I tried regular expresion, but not leave me.

If anyone knows how, or another way to get the real select, would be very grateful!


Solution

  • I think this is what you want, if it isn't, correct me: Both values are awaylable with this regex

    /tipoElemento=([A-Za-z]*).*activo=([A-Za-z]*)/