Viktor M.
Viktor M.

Reputation: 4623

Hibernate refresh materialized view

I have faced with the trouble of refreshing materialized views(POSGRESQL) using the following piece of code:

String hql = "refresh materialized view mv_all_operations; refresh materialized view mv_all_members;";
createQuery(hql).executeUpdate();

Exception:

antlr.NoViableAltException: unexpected token: refresh
        at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:169) ~[hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:268) ~[hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182) ~[hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138) ~[hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105) [hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) [hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) [hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221) [hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199) [hibernate-core-4.2.7.Final.jar:4.2.7.Final]
        at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1784) [hibernate-core-4.2.7.Final.jar:4.2.7.Final]

It seems I cannot update my views this way but I cannot find any other opportunity to do this in code. Any suggestions?

Upvotes: 4

Views: 7533

Answers (1)

Si mo
Si mo

Reputation: 979

Use a native query instead of hql.

String sql = "refresh materialized view mv_all_operations; refresh materialized view mv_all_members;";
createNativeQuery(sql).executeUpdate();

Upvotes: 8

Related Questions