-->
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: How to escape field in the request ?
PostPosted: Thu May 31, 2007 1:21 pm 
Newbie

Joined: Tue Dec 05, 2006 8:59 am
Posts: 8
Hibernate version:3.2.2.ga

Name and version of the database you are using: mysql


Hi, I try to use schema exporter in my test. The export don't work because one field name is a sql keyword and there is no quote in the generated sql :

GRAVE: Unsuccessful: create table mytable (id bigint not null, int integer, primary key (id))
27 mai 2007 16:48:46 org.hibernate.tool.hbm2ddl.SchemaExport create
GRAVE: Syntax error: Encountered "int" at line 1, column 411.

(I delete all other fields in the trace, so column 411 is not relevant)

The generated sql is incorrect, if it were :

create table mytable (`id` bigint not null, `int` integer, primary key (`id`))

It will work. Is there a way to force the quote in the hbm.xml ?

I precise I can't change the field name.

Edit :

For my test, I tried :

<property name="int_" type="java.lang.Integer">
<column name="`int`" precision="7" scale="0"></column>
</property>

It works for the export but get(MyClass.class, id); don't work because the generated sql is :

select .... myclass0_."int" as int21_0_0_... where myclass0_.id=?

(I delete some information to be more readable)

mysql don't like the " ^^


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 1:30 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Do you have the correct Dialect in your hibernate.cfg.xml?:
Code:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

cause the default translation for backticks are the double quotes.


By the way, you have to maintain your code as is:
Code:
<property name="int_" type="java.lang.Integer">
   <column name="`int`" precision="7" scale="0"></column>
</property>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 03, 2007 5:40 am 
Newbie

Joined: Tue Dec 05, 2006 8:59 am
Posts: 8
For my unit test, I use an embedded database : derby. So the dialect is DerbyDialect.
In production I use mysql dialect.

But you say I have to keep : `int` in my mapping files ? Is it correct for derbyDialect and mysql dialect ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 03, 2007 6:29 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Yes.
Hibernate Reference:
5.4. SQL quoted identifiers
Quote:
You may force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in
backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect (usually
double quotes, but brackets for SQL Server and backticks for MySQL).


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.