简体   繁体   English

使用MS SQL进行Hibernate

[英]Hibernate with MS SQL

I'm trying to integrate hibernate with MS SQL, below is the sql query I get from hibernate 我正在尝试将hibernate与MS SQL集成,下面是我从hibernate获取的sql查询

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 MS SQL管理工作室也会抛出同样的错误

while this command runs fine 虽然这个命令运行正常

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

below is my hibernate cfg 下面是我的冬眠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 (从microsoft网站下载的sqljdbc4.jar)似乎hibernate正在生成MS SQL无法理解的查询

RULE is a SQL Server reserved keyword . RULE是SQL Server保留的关键字

If you need to stick to that name you need to escape it with: 如果你需要坚持这个名字,你需要逃避:

@Table(name="`rule`")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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