2

I am using hibernate 4.x and want to set the "SQL_NO_CACHE" statement from MySQL inside a HQL query

@Query("FROM mytable t WHERE id=1");

currently produces

select * from mytable where id=1

but I want

select SQL_NO_CACHE * from mytable where id=1

but I cannot figure out how.

No problem using a native query, but all query are written in HQL and are much more complex than this example. So I am getting a QuerySyntaxException: unexpected token Exception, since this statement is not documented anyhow in the hibernate documentation.

http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch16.html

MatthiasLaug
  • 2,924
  • 6
  • 28
  • 43
  • 1
    Well it is obviosuly unrealistic for Hibernate to support every RDBMS specific satement so Native SQL will be the only option here. – Alan Hay Nov 01 '13 at 11:08
  • sure, but I even didn't find a documented approach to extend the HqlParser to add this on my own – MatthiasLaug Nov 01 '13 at 11:09
  • Further to previous maybe you can actually do this by means of a Custom Dialect. See here for an example: http://keyurj.blogspot.co.uk/2012/12/creating-custom-dialect-in-hibernate.html – Alan Hay Nov 01 '13 at 11:23

2 Answers2

1

It is possible create a view that has this clause SQL_NO_CACHE. Then when hibernate run your SQL query you can override the PhysicalNamingStrategyStandardImpl.toPhysicalTableName and change to get the name your view instead your table. For example, the default name is 'mytable' and your view name is 'vw_mytable', then concatenate 'vw_' before name your table.

Put this implementation from PhysicalNamingStrategyStandardImpl in your persistence file in property "hibernate.physical_naming_strategy" and yours querys will use the clause SQL_NO_CACHE.

Abdul Moeez
  • 1,331
  • 2
  • 13
  • 31
0

Use StatementInspector to alter generated SELECT statements. On how to use it see the related question.

izogfif
  • 6,000
  • 2
  • 35
  • 25