These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: HQL query creates invalid SQL for MS SQL Server
PostPosted: Wed Aug 11, 2004 5:05 am 
Newbie

Joined: Wed Aug 11, 2004 3:48 am
Posts: 4
Location: UK
Hi,

The legacy MS SQL database I'm using has column names with all sorts of punctuation (even %). When I used hbm2java and Middlegen they seem to handle the punctuation by enclosing the field names in square brackets.

When I try to run a HQL query the SQL generated by Hibernate is not valid for MS SQL. The fields are not quoted within square brackets. I've checked I'm using the correct SQL Dialect. Is there anything else I've done wrong considering this is my first foray into Hibernate?

Hibernate version:2.1.4
Microsoft SQL Server 2000 SP3 Enterprise Edition
Mapping documents:
Code:
<hibernate-mapping>
    <class
        name="eg.StoCore"
        table="STO_CORE">

    <id
        name="stockId"
        type="java.lang.Integer"
        column="STOCK ID">

        <generator class="assigned" />
    </id>

    <property
        name="sedol"
        type="java.lang.String"
        column="REF (SEDOL) No."
        length="7"
    />

    ...........

    <joined-subclass
        name="eg.StoCustomer"
        table="STO_CUSTOMER">

        <key column="STOCK ID"/>

        ..........

        </joined-subclass>
    </class>
</hibernate-mapping>


Code:
<hibernate-configuration>
        <session-factory>
                <property name="dialect">
                        net.sf.hibernate.dialect.SQLServerDialect
                </property>
                <property name="connection.driver_class">
                        net.sourceforge.jtds.jdbc.Driver
                </property>
                <property name="connection.url">
                        jdbc:jtds:sqlserver://YY/XX               
               </property>
                <property name="connection.username">ZZ</property>
                <property name="connection.password">ZZ</property>
                <property name="connection.pool_size">1</property>
                <property name="show_sql">true</property>
                <property name="query.substitutions">yes 'Y', no 'N'</property>
                <property name="proxool.pool_alias">pool1</property>
                <property name="jdbc.batch_size">0</property>
                <property name="jdbc.use_streams_for_binary">true</property>
                <property name="max_fetch_depth">1</property>
                <property name="cache.region_prefix">hibernate.test</property>
                <property name="cache.use_query_cache">true</property>
                <property name="cache.provider_class">
                        net.sf.ehcache.hibernate.Provider
                </property>
                <mapping resource="Sto.hbm.xml" />
        </session-factory>
</hibernate-configuration>


Code between sessionFactory.openSession() and session.close():
Code:
Query query = session.createQuery("select s from StoCustomer as s where s.sedol = :sedol");
query.setString("sedol", "0048776");


Hibernate is generating the following query which is broken..
Code:
select
  stocustome0_.STOCK ID as x0_0_
from
  STO_CUSTOMER stocustome0_
inner join
  STO_CORE stocustome0__1_
on stocustome0_.STOCK ID=stocustome0__1_.STOCK ID
where (stocustome0__1_.REF (SEDOL) No.=? )


It should be something like:

Code:
SELECT     
  stocustome0_.[STOCK ID] AS x0_0_
FROM         
  STO_CUSTOMER stocustome0_
INNER JOIN
  STO_CORE stocustome0__1_
ON stocustome0_.[STOCK ID] = stocustome0__1_.[STOCK ID]
WHERE     (stocustome0__1_.[REF (SEDOL) No.] = ?)


Thanks
Andrew


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 5:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
enclose in backticks, as per the documentation


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 5:17 am 
Newbie

Joined: Wed Aug 11, 2004 3:48 am
Posts: 4
Location: UK
Sorry, have found it in the documentation as you said [end of chapter 5]. I generated the initial mappings using Middlegen so had incorrectly assumed they were ok.

Over 100 tables some with over 100 columns, most fields contain punctuation characters so it's going to be tricky..

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 5:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Adjust the velocity template used by middlegen probably, to include all column names in backticks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.