Hello,
My team and I are using v3.2.0.GA of Hibernate and Hibernate Annotations to persist the following classes:
Code:
@Entity
@Table(name = "`User`")
public class User implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToMany
private Set<Role> roles = new HashSet<Role>();
...
}
Code:
@Entity
@Table(name = "`Role`")
public class Role implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id;
...
}
Since "User" and "Role" are SQL keywords, we intended to use backticks to quote these identifiers according to chapter 5.4 of the Hibernate Reference Documentation.
Unfortunately, the SQL statements generated by Hibernate look like that (in this case for the HSQLDB dialect but that should not matter):
Code:
SchemaExport:303 - create table "Role" (id bigint generated by default as identity (start with 1), ... primary key (id))
SchemaExport:303 - create table "User" (id bigint generated by default as identity (start with 1), ... primary key (id))
SchemaExport:303 - create table "User`_`Role" ("User`_i" bigint not null, role_id bigint not null, primary key ("User`_i", role_id))
The problem are the SQL statements for the association table. The default table/column names generated by the EJB3 naming strategy contain backticks and get truncated (e.g. "User`_i" instead of "User_id").
We tried EJB3NamingStrategy and DefaultComponentSafeNamingStrategy but in both cases the default generation of table/column names seems to struggle with quoted identifiers. I found a related issue at
http://opensource.atlassian.com/projects/hibernate/browse/ANN-362 but that seems to have had another cause.
I am not quite familiar with the details of JPA but a key concept of the new specification was to provide sensible default values to ease the O/R-mapping. Even if quoted identifiers using backticks are not JPA conform, I would appreciate a further Hibernate extension to support this in order to automatically generate proper table/column names from quoted identifiers. Or am I just missing a configuration setting?
As a concrete suggestion, I could imagine the following naming strategy:
- prior to concatenation of table/colum names, discard any surrounding backticks
- after concatenation, re-add surrounding backticks if any part of the generated identifier was originally quoted
Thanks in advance to anybody sharing his/her thoughts.