Parse HQL to AST Structure and convert AST back to HQL

I have a HQL query:

query = select item.itemNumber from items item where item.stock>0 and item.price<100.00

I like to parse this query and convert it to a tree structure:

AST queryTree = parse(query);

what I like to iterate over nodes, change some values ​​and convert the tree back to string representation:

Iterator<ASTNode> it = queryTree.nodeIterator();
while(it.hasNext())
{
  ASTNode node = it.next();
  System.out.println( node.text() + "->" + node.value() );
}
query = queryTree.toString();

it would be nice if the parse method selected Exceptions in case of violation of the HQL grammar, but this was not necessary. Does anyone know how to do this? Are there any API methods suggested by hibernation to perform this task?

Thank,

+5
source share

All Articles