与MS SQL休眠 [英] Hibernate with MS SQL

查看:95
本文介绍了与MS SQL休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将hibernate与MS SQL集成,下面是我从hibernate获得的sql查询

  12:27 :44,172 DEBUG [AbstractSaveEventListener]执行identity-insert立即
Hibernate:

插入
aide.dbo.rule
(appId,ruleName)

(?,?)

会导致错误

  12:27:44,229 DEBUG [SqlExceptionHelper]关键字'规则'附近的语法不正确。 [n / a] 
com.microsoft.sqlserver.jdbc.SQLServerException:关键字'规则'附近的语法不正确。

同样的错误也出现在MS SQL管理工作室中

while this command runs fine




 插入

[aide]。[dbo]。[rule]
(appId,ruleName)
values
('rf','wfw')




下面是我的hibernate cfg



 <?xml version =1.0encoding =utf-8?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置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>助手< / 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>助手< / property>
< property name =hibernate.connection.password>助手< / property>
< property name =hibernate.dialect> org.hibernate.dialect.SQLServer2008Dialect< / property>
< property name =hibernate.show_sql> true< / property><! - JDBC连接池(使用
内置) - >
< property name =hibernate.connection.pool_size> 1< / property>
<! - 在启动时删除并重新创建数据库模式 - >
< property name =hibernate.hbm2ddl.auto>更新< / property>
< property name =hibernate.format_sql> true< / property>

< mapping class =com。****。Rule/>

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

(从microsoft网站下载sqljdbc4.jar)
似乎hibernate正在生成一个不可理解的查询通过MS SQL

解决方案 RULE 是SQL Server预留的关键字



如果您需要坚持使用该名称,您需要使用以下名称进行转义:

  @Table(name =`rule`)


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

解决方案

RULE is a SQL Server reserved keyword.

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

@Table(name="`rule`")

这篇关于与MS SQL休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆