3

I'm trying to integrate hibernate with MS SQL, below is the sql query I get from hibernate

12:27:44,172 DEBUG [AbstractSaveEventListener] Executing identity-insert immediately
Hibernate: 
    insert 
    into
        aide.dbo.rule
        (appId, ruleName) 
    values
        (?, ?)

causes error

12:27:44,229 DEBUG [SqlExceptionHelper] Incorrect syntax near the keyword 'rule'. [n/a]
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'rule'.

same error is thrown in MS SQL management studio too

while this command runs fine

 insert 
    into
        [aide].[dbo].[rule]
        (appId, ruleName) 
    values
        ('rf', 'wfw')

below is my hibernate cfg

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- <property name="hibernate.bytecode.use_reflection_optimizer">false</property> -->
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433</property>
        <property name="hibernate.default_catalog">aide</property>
        <property name="hibernate.default_schema">dbo</property>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.username">aide</property>
        <property name="hibernate.connection.password">aide</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
        <property name="hibernate.show_sql">true</property><!-- JDBC connection pool (use the 
            built-in) -->
        <property name="hibernate.connection.pool_size">1</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.format_sql">true</property>

        <mapping class="com.****.Rule" />

        <!-- <mapping resource="com/****/Rules.hbm.xml"></mapping> -->
    </session-factory>
</hibernate-configuration>

(sqljdbc4.jar downloaded from microsoft website) seems hibernate is generating a query not understandable by MS SQL

2
  • 3
    No, -you- are. By using the keyword 'rule' as a name. The error is already telling you that: "Incorrect syntax near the KEYWORD 'rule'". But you're not listening. Commented Jun 5, 2014 at 7:22
  • OP's phrasing is legitimate. There are situations where there is very good reason for using a reserved keyword as a table name, and pretty much every DBMS allows it with some form of escaping. The problem that led me, and probably OP, here was how to best do it in the context of hibernate. I have a hard requirement of supporting multiple DBMS's, must add SQL Server support to an existing entity structure, and need a form of escaping that is understood by all of the ones I must support. The table name is not a reserved keyword in any of the other DBMSs I must support. Commented Feb 15, 2019 at 19:53

1 Answer 1

2

RULE is a SQL Server reserved keyword.

If you need to stick to that name you need to escape it with:

@Table(name="`rule`")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.