-->
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.  [ 1 post ] 
Author Message
 Post subject: quoting reserved column names using hibernate annotations
PostPosted: Fri Jul 18, 2008 7:01 am 
Newbie

Joined: Fri Jul 18, 2008 6:48 am
Posts: 5
Hibernate version: latest (trunk) in subversion

Mapping documents: using annotation-based configuration

Name and version of the database you are using: mysql 5, innodb

Debug level Hibernate log excerpt:
Code:
18 Jul 2008 11:35:30 ERROR [hbm2ddl.SchemaUpdate] - Unsuccessful: create table forum_category (id bigint not null auto_increment, description varchar(255), hidden bit not null, name varchar(255), order integer not null, primary key (id)) ENGINE=InnoDB
18 Jul 2008 11:35:30 ERROR [hbm2ddl.SchemaUpdate] - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order integer not null, primary key (id)) ENGINE=InnoDB' at line 1


we are trying to use annotation based config with hibernate, avoiding the use of @Column wherever necessary. here is the relevant java code in the domain object.

Code:
@Entity
@Table(name = "forum_category")
public class Category {
...etc...
   private int order = 0;
...etc...
   /**
    * @return the order
    */
   public int getOrder() {
      return order;
   }

   /**
    * @param order
    *            the order to set
    */
   public void setOrder(int order) {
      this.order = order;
   }
}


obviously, the word 'order' is a reserved word in mysql, which is causing a problem. if we were using XML based configuration, then we'd simply surround the column name with backticks (`) which would cause Dialect.quote() to add db-specific quotes.

i can see no logical reason why we WOULDN'T want to add backticks to all column names (for example, it's just one additional level of security against sql injection attacks). however, because Dialect.quote() is final, i cannot override the chosen dialect (MySQL5InnoDBDialect) with one that automatically quotes everything like this :

Code:
public final String quote(String column) {
  if ( column.charAt( 0 ) == '`' ) {
    return openQuote() + column.substring( 1, column.length() - 1 ) + closeQuote();
  } else {
    return openQuote() + column + closeQuote();
  }
}


is there any reason why a) quoting backticks aren't added automatically regardless, and b) we can't override Dialect.quote() to provide this functionality in a custom dialect? as i mentioned, using annotation-based configuration, this means we need to add a @Column(name="`order`") directive to each reserved word, which is terribly annoying.

thanks for any help and assistance :)


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

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.