-->
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: Escaping Postgres' reserved words
PostPosted: Sat Aug 09, 2008 1:05 pm 
Newbie

Joined: Mon May 01, 2006 6:49 pm
Posts: 4
I'm using JPA with Hibernate EntityManager. I have an entity called user and I have hbm2ddl enabled. The thing is that user is a Postgres' reversed word so if I'm to use it, say, as my table name, I have to quote it.

I didn't find anyway to do this. Will I have to extend Postgre dialect? Shouldn't the built-in dialectic quote reserved words by default?

Hibernate's SQL:

Code:
create table user (id int8 not null, name varchar(40), password varchar(12), username varchar(20), version int4, primary key (id))

To be SQL:

Code:
create table "user" (id int8 not null, name varchar(40), password varchar(12), username varchar(20), version int4, primary key (id))


Hibernate version: 3.2.4.sp1
Dialect: org.hibernate.dialect.PostgreSQLDialect


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 1:24 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Use backticks (`) around table names in your mappings files. Eg.

Code:
<class table="`user`" ...>


Now, Hibernate will quote the table name whenever it is used in SQL. Here is more info: http://www.hibernate.org/hib_docs/v3/re ... dentifiers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 8:10 pm 
Newbie

Joined: Mon May 01, 2006 6:49 pm
Posts: 4
Thanks, but this is a workaround.. Shouldn't it be built-in the dialect?

I did put surround the table name with \", but then JPA auto-generates broken names like the DDL:

Code:
create table "user"_Role ("user"_i" int8 not null, roles_id int8 not null, id int8, primary key (id))


So I guess now I have to explicitly declare every name of tables related to User, right?.. =(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 12, 2008 1:56 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Personally, I wouldn't mind if Hibernate always did put the correct quotes around table and column names. Or at least, if there was a global setting that could turn this on. However, I think the documentation is quite clear on this issue. No backticks -> no quotes. One reason for this behavior may be that quotes changes case-sensitivity in some databases. Eg. in PostgreSQL unquoted names are converted to lower-case.


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.