Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate with MS SQL

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

like image 798
ir2pid Avatar asked Sep 17 '26 09:09

ir2pid


1 Answers

RULE is a SQL Server reserved keyword.

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

@Table(name="`rule`")
like image 56
Vlad Mihalcea Avatar answered Sep 18 '26 23:09

Vlad Mihalcea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!