Hibernate刷新物化视图

时间:2015-12-10 12:37:49

标签: hibernate

我遇到了使用以下代码刷新物化视图(POSGRESQL)的麻烦:

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

例外:

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]

我似乎无法以这种方式更新我的观点,但我找不到任何其他机会在代码中执行此操作。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

使用本机查询而不是hql。

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