简体   繁体   中英

SQL Query with OPTION clause in Hibernate

I use SQL Server 2008 as DBMS and I want to query a view with an OPTION clause set to (ROBUST PLAN) in Hibernate.

I tried these solutions:

try {
    mylist = (List<MyObject>) session.createSQLQuery("SELECT * FROM MyTable "+WHERE+" OPTION(ROBUST PLAN)").list();
    session.getTransaction().commit();
}

with this error:

org.hibernate.MappingException: No Dialect mapping for JDBC type: -9

and

try {
    mylist = (List<MyObject>) session.createQuery("from MyTable " + WHERE + " OPTION(ROBUST PLAN)").list();
    session.getTransaction().commit();
}

with this error:

2020-06-11 17:14:23 ERROR PARSER:56 - line 1:66: unexpected token: OPTION java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy20.createQuery(Unknown Source)

Caused by: java.lang.reflect.InvocationTargetException

Caused by: java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V

I don't know how to make this work.

My config: Hibernate.cfg.xml

        <property name="connection.driver_class">
            com.microsoft.sqlserver.jdbc.SQLServerDriver
        </property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

You're combining MySQLDialect with MSSQLServerDriver which need to be corrected. Next, I assume you're using MS SQL server and make sure your query runs without any issue.

String sql = "SELECT * FROM MyTable WHERE OPTION(ROBUST PLAN)";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(MyObject.class);
List<MyObject> results = query.list();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM